We will show examples of all the transaction attributes starting with the default, REQUIRED
.
A REQUIRED
annotated method is required to execute in a transaction but not necessarily a new transaction. If a transaction is already active then the method will execute in that transaction. If no transaction is active, a new transaction is started.
Suppose we have a stateless session bean, BankServiceBean
, and we want to create an audit record every time we add a customer to the database by means of the addCustomer()
method. Suppose this method invokes a method, addAuditMessage()
, on another stateless session bean AuditServiceBean
. This method creates and then persists an Audit entity. The Audit entity attributes are simply an identifier and a message string.
The normal sequence of events is:
1. start
addCustomer()
method2. persist Customer entity
3. start
addAuditMessage()
method4. persist Audit entity
5. end
addAuditMessage()
method6. end
addCustomer...