Required (REQUIRED @TransactionAttribute)
Required mode is used when you want your EJB to always execute in a transaction. This value specifies that the EJB must always be invoked within a transaction. It guarantees that the task executed by the method is will be executed within transaction context. Let us say that if the caller is already assigned a transaction context then the container does not initiate a new transaction and the callee will join the same transaction. But if the caller is not in a transaction context in that the container will initiate a new transaction and assign it to the callee.
In case of transactions propagated from the client, if our method indicates that the transaction should be rolled back, the container will not only roll back the whole transaction but will also throw a javax.transaction.RollbackException back to the client.
Requires New (NEVER @TransactionAttribute)
Requires New mode is used when the EJB method always has to be executed in a new transaction. This value specifies that whenever the EJB method is invoked with REQUIRES_NEW attribute, container must always invoke the EJB method with a new transaction. If the client already has a transaction, it is suspended until the invoked EJB method returns. Once the EJB method returns with a success or failure the container resumes the suspended transaction and client resumes its execution. The success or failure of the new transaction (that was created by the container) has no effect on the existing client transaction.
The REQUIRES_NEW attribute has limited uses in the real world. You should use it if you need a transaction but don’t want a rollback to affect the client.
Also use this value when you don’t want the client’s rollback to affect you. One good example would be online movie ticket booking. Our Movie ticket booking application sends both SMS and email confirmation separately. We want this so that the failure of one or both of these behaviours should not obstruct the normal flow of movie ticket booking and we are able to display the confirmation message on the screen. To achieve this we need to separate methods one for SMS confirmaton and another for email confirmation with transaction attribute as REQUIRES_NEW.
Supports (SUPPORTS @TransactionAttribute)
Supports mode is used when the EJB method is not required to be in any transaction. If the caller is in a transaction the EJB method will join the existing transaction and won’t cause the existing transaction to suspend. Hence this approach avoids the overhead of suspending and resuming transactions by the container. On the other hand if the caller is not in a transaction the EJB method will be invoked without transaction.
The SUPPORTS attribute is useful for functions that perform read only operations such as retrieving a record from a database table let us for a business validation.
Mandatory (MANDATORY @TransactionAttribute)
Mandatory mode is used when an existing transaction is a prerequisite for the invoked EJB method – that is, the client must be in a transaction prior to calling the EJB method. In this case the container will never create a transaction on behalf of the client. In case the client is not in a transaction before calling the EJB method, container will throw an EJBTransactionRequiredException. This is how it is different from Support attribute that Mandatory will make it mandate for the container to throw the exception.
This attribute should be used in scenarios where you want that the client should fail if the invoked EJB method calls for a rollback, very commonly used in business workflows.
Not Supported (NOT_SUPPORTED @TransactionAttribute)
Not Supported is used when you dont want that your EJB method be part of any transaction. If a caller with an associated transaction invokes the method, the container will suspend the transaction, invoke the method, and then resume the transaction when the method returns.
This should be used where you are not worried about isolating your bean’s operations from other concurrent operation or to maintain data consistency.
Never (NEVER @TransactionAttribute)
Never mode is used when you dont want the your EJB method can ever be invoked from a transactional client. If such an attempt is made, a javax.ejb.EJBException is thrown. Remember that in this case it will always throw the exception and never make an attempt to suspend the transaction if already exists. It is very rarely used but you would like to use it when you want to make sure that the client knows that the your ejb method is non-transactional and the client should not expect your bean to participate in the transaction to maintain data consistency.