Bean-managed transaction demarcation (BMT)
NOTE, only Session and Message-driven beans can be used with bean-managed transaction demarcation. A Bean Provider is not allowed to provide an Entity bean with bean-managed transaction demarcation.
The Container must make the javax.transaction.UserTransaction interface available to the enterprise bean's business method or onMessage method via the javax.ejb.EJBContext interface and under the environment entry java:comp/UserTransaction. When an instance uses the javax.transaction.UserTransaction interface to demarcate a transaction, the Container must enlist all the resource managers used by the instance between the begin() and commit() or rollback() methods with the transaction. When the instance attempts to commit the transaction, the Container is responsible for the global coordination of the transaction commit.
In the case of a STATEFUL session bean, it is POSSIBLE that the business method that started a transaction completes without committing or rolling back the transaction. In such a case, the Container must retain the association between the transaction and the instance across multiple client calls until the instance commits or rolls back the transaction. When the client invokes the next business method, the Container must invoke the business method in this transaction context.
If a STATELESS session bean instance starts a transaction in a business method, it MUST COMMIT the transaction before the business method returns. The Container must detect the case in which a transaction was started, but not completed, in the business method, and handle it as follows:
Log this as an application error to alert the system administrator.
Roll back the started transaction.
Discard the instance of the session bean.
Throw the java.rmi.RemoteException to the client if the client is a remote client, or throw the javax.ejb.EJBException if the client is a local client.
If a message-driven bean instance starts a transaction in the onMessage method, it must commit the transaction before the onMessage method returns. The Container must detect the case in which a transaction was started, but not completed, in the onMessage method, and handle it as follows:
Log this as an application error to alert the system administrator.
Roll back the started transaction.
Discard the instance of the session bean.
When an instance attempts to start a transaction using the begin() method of the javax.transaction.UserTransaction interface while the instance has not committed the previous transaction, the Container must throw the javax.transaction.NotSupportedException in the begin() method.
The Container must throw the java.lang.IllegalStateException if an instance of a bean with bean-managed transaction demarcation attempts to invoke the setRollbackOnly() or getRollbackOnly() method of the javax.ejb.EJBContext interface.
Container-managed transaction demarcation (CMT)
The Container must handle the EJBContext.setRollbackOnly() method invoked from a business method executing with the Required, RequiresNew, or Mandatory (for MDB - ONLY with Required) transaction attribute as follows:
The Container must ensure that the transaction will never commit. Typically, the Container instructs the transaction manager to mark the transaction for rollback.
If the Container initiated the transaction immediately before dispatching the business method to the instance (as opposed to the transaction being inherited from the caller), the Container must note that the instance has invoked the setRollbackOnly() method. When the business method invocation completes, the Container must roll back rather than commit the transaction. If the business method has returned normally or with an application exception, the Container must pass the method result or the application exception to the client after the Container performed the rollback.
The Container must handle the EJBContext.getRollbackOnly() method invoked from a business method executing with the Required, RequiresNew, or Mandatory (for MDB - ONLY with Required) transaction attribute.
The Container must throw (and log - MDB ONLY) the java.lang.IllegalStateException if the EJBContext.getRollbackOnly() method is invoked from a business method executing with the Supports, NotSupported, or Never (for MDB - ONLY with NotSupported) transaction attribute.
If an instance of an enterprise bean with container-managed transaction demarcation attempts to invoke the getUserTransaction() method of the EJBContext interface, the Container must throw (and log - MDB ONLY) the java.lang.IllegalStateException.
If a Session Bean (STATEFUL ONLY) class implements the javax.ejb.SessionSynchronization interface, the Container must invoke the afterBegin(), beforeCompletion(), and afterCompletion(...) callbacks on the instance as part of the transaction commit protocol.
The Container invokes the afterBegin() method on an instance BEFORE it invokes the first business method in a transaction.
The Container invokes the beforeCompletion() method to give the enterprise bean instance the last chance to cause the transaction to rollback. The instance may cause the transaction to roll back by invoking the EJBContext.setRollbackOnly() method.
The Container invokes the afterCompletion(boolean committed) method AFTER the completion of the transaction commit protocol to notify the enterprise bean instance of the transaction outcome.
Visit http://java.boot.by for the updates.