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.
Various components and parts of the EJB system can be part of a transaction. Examples are
The basic default behaviours are 1. A transaction starts at the beginning of the first EJB method call, in a chain of calls that are participating in the given transaction 1. A transaction ends at the end of the first EJB method, in the same chain 1. If a bean that has started a transaction, uses another bean, that bean will automatically use the same transaction as the calling bean.
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...
@TransactionAttribute applies to both methods and entire beans. You may set one type of transaction behaviour (as seen above) on the bean, and a different one on a specific method of that same bean, which overrides the one configured for the overall bean. For instance, maybe you want to make an audit entry in the database that you are about to attempt a credit card payment. It really needs to be in it's own transaction so that it is IMMEDIATELY committed for audit purposes, if something goes wrong with the credit card payment. So, perhaps you use MANDATORY on the bean, and REQUIRES_NEW on the method for audit logging. As soon as the method that does the audit logging is complete, the transaction is committed, and the credit card payment transaction continues on it's way.
All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.