2 Replies Latest reply on Sep 30, 2007 6:29 AM by franz77

    EntityManager must be access within a transaction -- Please

    sukar1205

      I am testing a session bean by a client.
      and I keep getting this error, I know my code should work because
      I wrote the same code last week.

      this is the error

      xception 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)
      Caused by: javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
       at org.jboss.ejb3.entity.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:150)
      


      This is my main ... all i want to do is add an object in an table
      
      public class KBClient {
      
       public static void main(String[] args) {
      
       KBClient c = new KBClient();
       ClassificationRemote r = c.lookupNewClassificationBean();
       KnowledgeBase first = new KnowledgeBase();
       first.setKnowledgeID(200);
       first.setKnowledgeName("AmerDemo");
       first.setKnowledgeCategory("Classification");
       first.setProjectID(new Project(88));
       first.setDeleted(0);
       r.addKnowledgeBase(first); //Exception occurs
       }
      
       private ClassificationRemote lookupNewClassificationBean() {
       try {
       Hashtable props = new Hashtable();
       props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
       props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
       props.put("java.naming.provider.url", "localhost");
       Context c = new InitialContext(props);
       return (ClassificationRemote) c.lookup("ClassificationBeanFinal/remote");
       }
       catch(NamingException ne) {
       Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
       throw new RuntimeException(ne);
       }
       }
      }
      
      


      and This is the function in the session bean

      
       @PersistenceContext private EntityManager em;
      
       public void addKnowledgeBase(KnowledgeBase kb){
       System.out.println("YAAAHOOOOOOO");
       em.persist(kb);
       }
      

      I checked the JNDI tree as well ... everything is fine
      the exception occurs when adding to the table in main.
      Please help me !!!
      Thanks