TransactionAttribute.REQUIRES_NEW in JPA

I checked ejb jpa transactions. I am using container-managed entityManager:

@PersistenceContext(unitName = "ParticularUnit") EntityManager em; 

So I got the transaction. The PersistanceContext transaction scope and container control each transaction. An entityManager is created for each instance of the beans beans session. When I mark the bean method with @TransactionAttribute (TransactionAttributeType.REQUIRED) and the two methods are called inside, I expected then the test1 method will have a different PersitenceContext, but I was surprised because it was the same.

 @TransactionAttribute(TransactionAttributeType.REQUIRED) public void test(Configuration config){ if (!em.contains(config)) { config = em.find(Configuration.class, config.getId()); } System.out.println("********************"); System.out.println("actiovation, em= "+ em); System.out.println("actiovation, config= "+ config); System.out.println("*********************"); test1(config); test2(config); } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void test1(Configuration config){ config = em.find(Configuration.class, config.getId()); System.out.println("////////"); System.out.println("requires_new"+ config); System.out.println("requires_new, em= "+ em); System.out.println("----------------"); } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void test2(Configuration config){ config = em.find(Configuration.class, config.getId()); System.out.println("////////"); System.out.println("required"+ config); System.out.println("required, em= "+ em); System.out.println("----------------"); } 

I call test and get this trace.

 ******************** actiovation, em= JPATxEntityManager@54515451 actiovation, config= com.profix.sc.db.configuration.Configuration@7f287f28 ********************* //////// requires_newcom.profix.sc.db.configuration.Configuration@7f287f2 8 requires_new, em= JPATxEntityManager@54515451 [PuId=SCApplication#SCApplication-ejb.jar#SCApplication, SCApplication#SCApplication-ejb.jar#PaymentConfigBean#com.profix.sc.ejb.PaymentConfigBean/em] ---------------- //////// requiredcom.profix.sc.db.configuration.Configuration@7f287f28 required, em= JPATxEntityManager@54515451 [PuId=SCApplication#SCApplication-ejb.jar#SCApplication, SCApplication#SCApplication-ejb.jar#PaymentConfigBean#com.profix.sc.ejb.PaymentConfigBean/em] 

Why is the persistenceContext inside test1() the same as test() ?

+2
source share
1 answer

I assume that you are not using the business method at all. You are calling a local call (implicit 'this') - not an EJB.

Try this call using the business interface .

Try changing the calls to test1 () and test2 () in the test () class:

 test1(config); test2(config); 

to

 getBusinessObject(YourEJB.class).test1(config); getBusinessObject(YourEJB.class).test2(config); 

PS. I am not sure about this, but the container can use a proxy object for EntityManager, so try to check if you are really in the same PersistenceContext (by doing some operations, and not just referring to the link)

+4
source

Source: https://habr.com/ru/post/1396587/


All Articles