0 Replies Latest reply on Sep 19, 2007 3:41 PM by Amer Ramamni

    transaction problem

    Amer Ramamni Newbie

      I am trying to run a client class to test the CustomerBean class that I wrote but its giving me an error which mentions EntityManager must be access within a transaction. I am new to EJB3 so bear with me please.
      This the code for my customerBean class.

      @Stateless
      public class CustomerBean implements CustomerRemote {
      
       @Resource(mappedName = "java:/SQL")
       private DataSource SQL;
      
       @PersistenceContext private EntityManager em;
      
       public void addCustomer(TempCustomer a){
       em.persist(a); //This is where the Exception occurs ..thanks
       }
      
       public TempCustomer findCustomerbyID(int id) {
       return em.find(TempCustomer.class, id);
       }
      
       public void deleteCustomer(TempCustomer a) {
       em.remove(a);
       }
      
       public void deleteID(int id){
       em.remove(id);
       }
      
      


      And this is my client class to test my CustomerBean class... I am just trying to add 2 customers in the table.

      public class Client {
       public static void main(String[] args) throws Exception
       {
       Connection conn;
       // Step 1: Load the JDBC driver.
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       System.out.println("Driver Loaded.");
       // Step 2: Establish the connection to the database.
       String url = "jdbc:sqlserver://localhost:1433;database=jdocxpt";
       conn = DriverManager.getConnection(url, "sa", "");
       System.out.println("Got Connection.");
      
       Context ctx = getInitialContext();
       CustomerRemote c = (CustomerRemote) ctx.lookup("CustomerBean/remote");
      
       TempCustomer first = new TempCustomer();
       TempCustomer second = new TempCustomer();
      
       System.out.println("adding Customers");
       first.setId(20);
       first.setName("aaaa");
       second.setId(21);
       second.setName("kkkk");
       c.addCustomer(first);
       c.addCustomer(second);
      }


      I would appreciate it if some could help me with this transaction problem.
      THANKS IN ADVANCE.

      the error is
      Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: EntityManager must be access within a transaction
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
       at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
       at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:278)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:734)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:560)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:369)
       at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:165)