3 Replies Latest reply on Nov 6, 2007 3:26 AM by Carlo de Wolf

    TransactionRequiredException: EntityManager must be access w

    i o Newbie

      it's somehow strange,because i said in the code that for my method i REQUIRE_NEW transaction...

      package bps.ejb;
      
      import javax.ejb.*;
      import javax.persistence.*;
      
      import java.util.*;
      import java.sql.*;
      
      
      @Stateless
      public class BpsProcessQueueBean implements BpsProcessQueue {
       public static final String RemoteJNDIName = BpsProcessQueueBean.class.getSimpleName() + "/remote";
      
       public static final String LocalJNDIName = BpsProcessQueueBean.class.getSimpleName() + "/local";
      
       @PersistenceContext
       private EntityManager em;
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       private Process fetchProcess_real(int subsystem) {
      
       String fetchQuery =
       "SELECT " +
       " p " +
       "FROM " +
       " Process AS p " +
       "WHERE " +
       " p.state = 0 AND " +
       " p.processQueue.id IN " +
       " (SELECT " +
       " pq.id " +
       " FROM " +
       " ProcessQueue AS pq " +
       " WHERE " +
       " (pq.subsystem.id = " +
       " (SELECT " +
       " s.id " +
       " FROM " +
       " Subsystem AS s " +
       " WHERE " +
       " (s.id = " + subsystem + ") AND " +
       " (s.connected = true) " +
       " ) " +
       " ) AND " +
       " (pq.connected = true) AND " +
       " (pq.curActiveProcesses < pq.maxActiveProcesses) " +
       " ) " +
       "ORDER BY p.priority ASC, p.id ASC";
      
       Query query = em.createQuery(fetchQuery);
       //Query query = em.createNamedQuery("fetchQuery");
       //query.setParameter("subsystem", subsystem);
       query.setMaxResults(1);
       Process p = (Process) query.getResultList().get(0);
      
       ProcessQueue pq = p.getProcessQueue();
      
       p.use();
       pq.incrementActive();
      
       em.persist(p);
       em.persist(pq);
      
       return p;
       }
      
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public Process fetchProcess(int subsystem) {
       boolean failed = true;
      
       while (failed == true) {
       failed = false;
       try {
       return fetchProcess_real(subsystem);
       } catch (Exception e) {
       e.printStackTrace();
       //failed = true;
       return null;
       }
       }
       return null;
       }
      
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       private void releaseProcess_real(int id) {
       Process p = em.find(Process.class,id);
      
       CompletedProcess cp = new CompletedProcess(p);
       em.remove(p);
       em.persist(cp);
       }
      
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void releaseProcess(int id) {
       releaseProcess_real(id);
       }
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void releaseProcess(Process p) {
       releaseProcess_real(p.getId());
       }
      
      
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       private void decreaseQueue_real(int id) {
       ProcessQueue pq = em.find(ProcessQueue.class, id);
       pq.decrementActive();
       em.persist(pq);
       }
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void decreaseQueue(int id) {
       boolean failed = true;
      
       while (failed == true) {
       failed = false;
       try {
       decreaseQueue_real(id);
       } catch (Exception e) {
       //failed = true;
       e.printStackTrace();
       }
       }
       }
      
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void completeProcess(int id) {
       releaseProcess(id);
       decreaseQueue(id);
       }
      
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void completeProcess(Process p) {
       completeProcess(p.getId());
       }
      
       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
       public void doProcess(int subsystem) {
       int i = 0;
       Process p = null;
       System.out.println("doProcess: before entering the while");
       while ((p = fetchProcess(subsystem)) != null) {
       i++;
      
       // do some work
       System.out.println("doProcess() - " + p);
      
       completeProcess(p.getId());
       }
       System.out.println("doProcess: " + i + " process dequeued");
       }
      
      }
      


      And the exception
      14:30:22,827 ERROR [STDERR] javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
      14:30:22,828 ERROR [STDERR] at org.jboss.ejb3.entity.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:150)
      14:30:22,828 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:181)
      14:30:22,828 ERROR [STDERR] at bps.ejb.BpsProcessQueueBean.fetchProcess_real(BpsProcessQueueBean.java:89)
      14:30:22,829 ERROR [STDERR] at bps.ejb.BpsProcessQueueBean.fetchProcess(BpsProcessQueueBean.java:102)
      14:30:22,829 ERROR [STDERR] at bps.ejb.BpsProcessQueueBean.doProcess(BpsProcessQueueBean.java:169)
      14:30:22,829 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      14:30:22,830 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      14:30:22,830 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      14:30:22,830 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
      14:30:22,830 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      14:30:22,830 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
      14:30:22,831 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
      14:30:22,831 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,831 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
      14:30:22,831 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,831 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
      14:30:22,831 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,831 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
      14:30:22,831 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:102)
      14:30:22,832 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,833 ERROR [STDERR] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      14:30:22,833 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,833 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
      14:30:22,833 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,833 ERROR [STDERR] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
      14:30:22,833 ERROR [STDERR] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
      14:30:22,834 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,834 ERROR [STDERR] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
      14:30:22,834 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,834 ERROR [STDERR] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      14:30:22,834 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,834 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:278)
      14:30:22,834 ERROR [STDERR] at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:79)
      14:30:22,834 ERROR [STDERR] at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:70)
      14:30:22,835 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,835 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:103)
      14:30:22,835 ERROR [STDERR] at $Proxy187.doProcess(Unknown Source)
      14:30:22,835 ERROR [STDERR] at bps.ejb.BpsAgent.onMessage(BpsAgent.java:34)
      14:30:22,835 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      14:30:22,835 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      14:30:22,835 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      14:30:22,835 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
      14:30:22,835 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      14:30:22,836 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
      14:30:22,836 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
      14:30:22,836 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,836 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
      14:30:22,836 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,836 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
      14:30:22,836 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,836 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
      14:30:22,837 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
      14:30:22,837 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,837 ERROR [STDERR] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
      14:30:22,837 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      14:30:22,837 ERROR [STDERR] at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:245)
      14:30:22,837 ERROR [STDERR] at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)
      14:30:22,837 ERROR [STDERR] at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)
      14:30:22,837 ERROR [STDERR] at $Proxy182.onMessage(Unknown Source)
      14:30:22,837 ERROR [STDERR] at org.jboss.resource.adapter.jms.inflow.JmsServerSession.onMessage(JmsServerSession.java:178)
      14:30:22,838 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:891)
      14:30:22,838 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:170)
      14:30:22,838 ERROR [STDERR] at org.jboss.mq.SpySession.run(SpySession.java:323)
      14:30:22,838 ERROR [STDERR] at org.jboss.resource.adapter.jms.inflow.JmsServerSession.run(JmsServerSession.java:237)
      14:30:22,838 ERROR [STDERR] at org.jboss.resource.work.WorkWrapper.execute(WorkWrapper.java:204)
      14:30:22,838 ERROR [STDERR] at org.jboss.util.threadpool.BasicTaskWrapper.run(BasicTaskWrapper.java:275)
      14:30:22,838 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)
      14:30:22,838 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
      


      before i tried to state the whole session bean as transactionattribute.NOT_SUPPORTED, and state REQUIRES_NEW only on the methods i wanted. but i got same error so i tried to specify for each method.