Transactions in Java EE application are a series of actions that all must be completed successfully, or else the changes in each action are backed out. If all the actions are successful then the changes from all actions are committed. If any one of the actions is unsuccessful then the changes from all the actions are rolled back.
Java Transaction API is an API specified in Java EE that provides applications a standard way to access transactions independent of any specific implementation. JTA transactions are controlled by Java EE transaction manager. Transaction are started, committed or rolled back by calling corresponding methods on the UserTransaction API.
Transactions in EJB application are either Container-Managed or Bean-Managed
Container-Managed Transactions - In Container-Managed transactions the transaction boundaries are set in the EJB container. Container-managed transactions can be set for both session beans as well as message-driven beans.
Bean-Managed Transactions - In Bean-Managed transactions the transaction boundaries are set specifically within the bean's code. Bean managed transactions can be set for session beans as well as message-driven beans. Bean manages transaction can either use JDBC transactions or JTA transactions.
Transaction attribute determines the scope of a transaction across beans. For example, if methodA() of Bean A starts a transaction and calls methodB() of bean b, then does methodB() run within the transaction started by methodA()? This is determined by the transaction attribute set on methodB()
A transaction attribute can have one of the following values -
1. Required
2. RequiresNew
3. Mandatory
4. NotSupported
5. Supports
6. Never
If a client has an associated transaction and calls a container-managed bean's method then the method executes within the client transaction.
If the client is not associated with a transaction then the container starts a new transaction before calling the bean's method.
If a client has an associated transaction and calls a container-managed bean's method then the container suspends the clients transaction, starts a new transaction, starts the call to the method and starts the clients transaction after method completes.
If the client is not associated with a transaction then the container starts a new transaction before calling the beans method.
If a client has an associated transaction and calls a container-managed bean's method then the method executes within the client transaction.
If a client is not associated with a transaction then the container throws a TransactionRequiredException
If a client has an associated transaction and calls a container-managed bean's method then the container suspends the clients transaction, calls the bean's method, and starts the clients transaction after method execution completes.
If a client is not associated with a transaction, then the container does not start a new transaction before calling the bean's method.
If a client has an associated transaction and calls a container-managed bean's method then the method executes within the client transaction.
If a client is not associated with a transaction, then the container does not start a new transaction before calling the bean's method.
If a client has an associated transaction and calls a container-managed bean's method then the container throws a RemoteException.
If a client is not associated with a transaction, then the container does not start a new transaction before calling the bean's method.
Container-Managed Transaction - In container managed transactions the transaction timeout is set at the container's administration console.
Bean-Managed Transactions - In Bean-managed transactions, the transaction timeout is set by calling the setTransactionTimeout() method of the UserTransaction interface.