The following are a few notes on J2EE Transactions and Attributes
- As per spec, deployer should define the transaction attribute.
- In Weblogic the default transaction attribute is SUPPORTS
- EJB Transaction is automatically rolled back only when a SystemException (or a subtype of it) is thrown.
- Your ApplicationException can extend from javax.ejb.EJBException, which is a sub class of RuntimeException.
- When an EJBException is encountered the container rolls back the transaction.
- EJB Specification does not mention anything about Application exceptions being sub-classes of EJBException.
- You can tell container to rollback the transaction, by using setRollBackOnly on SessionContext/EJBContext object as per type of bean you are using.
Transaction Attributes
A transaction attribute may have one of the following values:
* Required
* RequiresNew
* Mandatory
* NotSupported
* Supports
* Never
Required
If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container starts a new transaction before running the method.
The Required attribute will work for most transactions. Therefore, you may want to use it as a default, at least in the early phases of development. Because transaction attributes are declarative, you can easily change them at a later time.
RequiresNew
If the client is running within a transaction and invokes the enterprise bean's method, the container takes the following steps:
1. Suspends the client's transaction
2. Starts a new transaction
3. Delegates the call to the method
4. Resumes the client's transaction after the method completes
If the client is not associated with a transaction, the container starts a new transaction before running the method.
You should use the RequiresNew attribute when you want to ensure that the method always runs within a new transaction.
Mandatory
If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container throws the TransactionRequiredException.
Use the Mandatory attribute if the enterprise bean's method must use the transaction of the client.
NotSupported
If the client is running within a transaction and invokes the enterprise bean's method, the container suspends the client's transaction before invoking the method. After the method has completed, the container resumes the client's transaction.
If the client is not associated with a transaction, the container does not start a new transaction before running the method.
Use the NotSupported attribute for methods that don't need transactions. Because transactions involve overhead, this attribute may improve performance.
Supports
If the client is running within a transaction and invokes the enterprise bean's method, the method executes within the client's transaction. If the client is not associated with a transaction, the container does not start a new transaction before running the method.
Because the transactional behavior of the method may vary, you should use the Supports attribute with caution.
Never
If the client is running within a transaction and invokes the enterprise bean's method, the container throws a RemoteException. If the client is not associated with a transaction, the container does not start a new transaction before running the method.