1 Reply Latest reply on Nov 7, 2005 8:28 AM by dunks80

    Container Managed Transaction Demarcation

    dunks80 Newbie

      JBoss 4.0.3SP1
      jdk 1.5.0_04

      I'm a little confused about container manager transaction demarcation. I have a stateless session bean with a method that is invoked by the ejb timer service. It can potentially be a long running method but doesn't require any transaction capability (I don't need anything to roll back if the method doesn't succeed). So to avoid transaction timeout I marked the method as @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED). In this method however I do need to query the database and of course the entityManager must be accessed from within a transaction. So I moved my access of the entityManager into another method in the same ejb and marked it as @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW). I assumed that the container would start a new transaction each time the method which accessed the entityManager was called. However, when I run the application the i get the following error

      Caused by: javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
       at org.jboss.ejb3.entity.ManagedEntityManagerFactory.getSession(ManagedEntityManagerFactory.java:124)
       at org.jboss.ejb3.entity.InjectedEntityManager.getSession(InjectedEntityManager.java:154)
       at org.jboss.ejb3.entity.InjectedEntityManager.createQuery(InjectedEntityManager.java:67)
      


      Could someone explain why there seems to be no transaction created for a method that is marked @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)? Is it because I'm calling that function from within the method that is marked as @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) on the same bean? How could you access the entity manager from a method that doesn't support transacitons?