One of the many benefits of EJB, is that transactions within the EJB container are generally managed entirely automatically. Any EJB component will, by default, partake in that transaction.
Here are some basic rules to understand about transactions. Keep note that this is the default behaviour, and the system can be configured to behave differently, depending on the needs of your system, bean, or individual methods of your beans.
Participants
Various components and parts of the EJB system can be part of a transaction. These include...
- Session bean
- Message Driven Bean
- EntityManager (a.k.a. Persistence context)
Behaviour
The basic default behaviours are
- A transaction starts at the beginning of the first EJB method call, in a chain of calls that are participating in the given transaction
- A transaction ends at the end of the first EJB method, in the same chain
- If a bean that has started a transaction, uses another bean, that bean will automatically use the same transaction as the calling bean.
Configuration
You can configure your beans in a variety of ways. Generally speaking, a transaction is started when a method is called, but can be configured using @TransactionAttribute(value = TransactionAttributeType.X), where X is one of...
- REQUIRED - the default, which is to start a transaction if one does not exist, but to use the existing one if it has already been started.
- REQUIRESNEW - the transaction is created on every call, and ends when the call is completed. Beans don't partake in transactions created by other parts of the system.
- MANDATORY - a transaction must always exist prior to the call, and it will be used. It is an error otherwise
- NOTSUPPORTED - component not included in the transaction
- SUPPORTS - transaction will be used if it exists, but will not be created if it does not exist
- NEVER - if a transaction exists, it is an error to call the method