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
Why is the persistenceContext inside test1() the same as test() ?
source share