Hi all!
While working with an @RequestScoped EntityManager in Java EE 7 we have been running into the issue that currently an EntityManager joins an active transaction during EntityManagerFactory.createEntityManager() only. If you create an EntityManager without an transaction the EM will not join an transaction that is created afterwards.
I always had the impression that other XA resources, such as DB connections and the related statements, will check during every API interaction whether there is an running transaction or not and associate the connection with the newly created transaction if not enlisted already. While checking the related specs (JTA, JCA, ...) I was unable to approve my assumption (which is based on current App-server behaviour).
My questions:
1. Am I right that other XA Resources will force TX enlisting even if they are created prior to TX.begin()?
2. If true: wouldn't you expect that an EntityManager will be enlisted in the same manner too?
With JPA 2.1 a synchronized EntityManager will join during create (if there is an active transaction). An unsychronized EntityManager will never join automatically. However I am looking for an eagerly synchronized EntityManager, that joins as soon as an EntityManager API interaction happens within an transaction.
Simplified use case:
1. Write a CDI Producer Method that creates a @RequestScoped EntityManager (similar to OpenEntityManagerInViewFilter)
2. Use @Transactional business methods to mark TX boundaries.
3. Access the EntityManager prior to executing the @Transactional method. This happens for instance if you use JSF converters or validators that need EntityManager access before executing the business method.
4. Currently the @Transactional annotation is ignored by EntityManagers.
Jens