Hitachi

uCosminexus Application Server Compatibility Guide


8.5.3 Persistence context when the application-managed EntityManager is used

When you use the application-managed EntityManager, the application directly invokes EntityManagerFactory of the JPA provider and manages the EntityManager life cycle and the creation and destruction of the persistence context. The life cycle of the application-managed persistence context can manage life cycles across multiple transactions.

Organization of this subsection

(1) Managing the EntityManager life cycle

With the application, you use the close and isOpen methods of EntityManager to manage the life cycle of the application-managed EntityManager. If you invoke the close method of EntityManager, EntityManager, persistence context associated with EntityManager, and the other resources are released. After the close method is invoked, do not invoke methods other than EntityManager getTransaction method and isOpen method with the application. If other methods are invoked, IllegalStateException is thrown. If the close method is invoked when the transaction is active, the persistence context remains stored until the transaction is concluded. The isOpen method of EntityManager returns true until EntityManager is closed and false after EntityManager is closed.

(2) Life cycle of persistence context

The life cycle of the application-managed persistence context is as follows:

The application-managed persistence context is independent of the transaction. Therefore, this persistence context is not propagated along with the JTA transaction.

(3) Notes on using the JTA entity manager

When the JTA entity manager is used in the application-managed EntityManager, the invocation of joinTransaction of EntityManager when the application creates EntityManager outside the JTA transaction scope is the responsibility of the application. The application must report EntityManager that the transaction has started, so invoke the joinTransaction method of EntityManager after the transaction starts.