2 Replies Latest reply on Jun 3, 2007 3:07 PM by adamzrk

    tx attributte - supports doesn't work as expected

    adamzrk

      Hi

      I've got an EJB:

      
      @Stateless
      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
      public class AirportDaoBean implements AirportDaoLocal, AirportDaoRemote {
      
       @PersistenceContext
       private EntityManager manager;
      
       @Override
       public void createAirport(Airport airport) {
       manager.persist(airport);
       }
      
       @Override
       public Airport getAirportById(int id) {
       return manager.find(Airport.class, id);
       }
      
       @Override
       public List<Airport> getAll() {
       return manager.createQuery("from Airport order by city.country.name").getResultList();
       }
      
      }
      
      
      


      As you can see I configured it with SUPPORTS transaction attribute, so when there is a transaction all methods in my bean will use it, when there is no transaction context then bean wouldn't begin a new one. So my question is: why i get the exception:
      
      Caused by: javax.persistence.TransactionRequiredException: EntityManager must be access within a tra
      nsaction
       at org.jboss.ejb3.entity.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.
      java:150)
       at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManag
      er.java:174)
       at master.airport.logic.impl.AirportDaoBean.createAirport(AirportDaoBean.java:24)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor
      .java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedE
      ntityManagerIntercepto
      
      



      Regards
      Adam

        • 1. Re: tx attributte - supports doesn't work as expected
          jaikiran

           

          EntityManager must be access within a transaction


          Which means that when you are accessing the entity manager in your createAirport method, there should be an active transaction. As you already correctly mentioned:

          As you can see I configured it with SUPPORTS transaction attribute, so when there is a transaction all methods in my bean will use it, when there is no transaction context then bean wouldn't begin a new one


          I believe when the createAirport method is being called, there is no active transaction and since the transaction attribute that you have specified is SUPPORTS, the server wont create a new one. You might want to change the transaction attribute to REQUIRED instead of SUPPORTS. For transaction attribute REQUIRED, a new transaction is created when there is no active transaction. If there is a active transaction then the same will be used for that method. So try changing

          @TransactionAttribute(TransactionAttributeType.SUPPORTS)


          to

          @TransactionAttribute(TransactionAttributeType.REQUIRED)


          • 2. Re: tx attributte - supports doesn't work as expected
            adamzrk

            I know what all transaction attributes mean and I do not want the REQUIRED attribute. According to specification jboss have implemented tx support not very well.....

            Regards
            Adam