@TransactionAttribute is a Jakarta EE annotation (jakarta.ejb.TransactionAttribute, formerly javax.ejb) that sets how an enterprise bean method takes part in a container-managed transaction. Its value, a TransactionAttributeType, decides whether the method joins the caller's transaction, starts a new one, or runs without one.
If you omit it, the container applies REQUIRED to every business method of the bean. You add the annotation only when a method needs a different rule.
With container-managed transactions (CMT), the EJB container begins, commits and rolls back transactions for you. @TransactionAttribute tells the container what to do at each method boundary. You can place it on a class, which sets the default for all business methods, or on an individual method, which overrides the class setting.
| Type | Caller has a transaction | Caller has no transaction |
|---|---|---|
REQUIRED (default) | Joins it | Container starts a new one |
REQUIRES_NEW | Suspends it, starts a new one | Container starts a new one |
MANDATORY | Joins it | Throws EJBTransactionRequiredException |
SUPPORTS | Joins it | Runs without a transaction |
NOT_SUPPORTED | Suspends it, runs without | Runs without a transaction |
NEVER | Throws EJBException | Runs without a transaction |
Choose them by intent: REQUIRED for normal business operations, REQUIRES_NEW for work that must commit independently, MANDATORY for helpers that must never run on their own, and NOT_SUPPORTED for long or non-transactional work such as calling a slow external system.
The container rolls back automatically in these cases:
setRollbackOnly() on the EJBContext or SessionContext.An application exception, which is a checked exception that does not extend RuntimeException, does not roll back by default. To change that, annotate it:
@TransactionManagement(TransactionManagementType.BEAN)), you use UserTransaction yourself and the annotation is ignored.this skips the container and the attribute. Inject the bean or use SessionContext.getBusinessObject().REQUIRED and NOT_SUPPORTED are meaningful for the message listener method.jakarta.transaction.Transactional with TxType, which has equivalent values.Since Jakarta EE 9 the package prefix is jakarta.*. Code written for Java EE 8 or older uses javax.ejb.TransactionAttribute. On Jakarta EE 9 and later servers, update imports, otherwise the annotation is not recognised. Modern Jakarta EE also lets you use the CDI-based @Transactional on plain managed beans without EJB.
REQUIRED. Every business method without an explicit @TransactionAttribute joins or starts a transaction.
No. Spring uses its own @Transactional with Propagation values. Spring can also accept jakarta.transaction.Transactional, but not the EJB annotation.
REQUIRED uses the caller's transaction when there is one. REQUIRES_NEW always runs in a new transaction and suspends the caller's for the duration.
Not by default. Checked application exceptions commit unless declared with @ApplicationException(rollback = true).
@TransactionAttribute configures how EJB methods participate in container-managed transactions. It supports six attribute types, defaults to REQUIRED, and can be set per class or per method. Combine it with @ApplicationException to control rollback, remember that self-calls bypass it, and use the jakarta.* packages on current Jakarta EE servers.