I am confused about the life cycle of the transactions, the entitymanagers and the persistence context in the EJB container.

I use the entitymanager this way:

 @PersistenceContext(unitName = "..")
 private EntityManager em;

in every stateless ejb.

My question is as simple as:

  1. When the transaction starts ?
  2. How the transaction is propagated ? ie when stateless ejbs call each others, does they keep using the same transaction ?
  3. When the transaction is committed ?
有帮助吗?

解决方案

For container-managed transactions:

  1. The transaction (TX) starts when the first transactional method is invoked. Per default, all EJB methods are transactional ( equivalent to TransactionAttributeType.REQUIRED, which is the default setting).

  2. The default TX propagation keeps the same TX over all local EJB calls. This is equivalent to an explicit TrasactionAttributeType.REQUIRED on all invoked methods

  3. The transaction is committed when the first method in the invocation chain (the one the TX has been created for) returns.

You can have a fine-grained control over the TX propagation by annotating your EJB methods with different TransactionAttributeTypes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top