Identify correct and incorrect statements or examples about the use of the EJB API for transaction management, including getRollbackOnly, setRollbackOnly and the SessionSynchronization interfaces.

[Note]
  • [EJB_3.0_CORE] 13.3.4.1; 4.3.7; 13.3.4.2; 13.3.4.3; 13.6.2.8; 13.6.2.9

javax.ejb.SessionSynchronization Interface

A stateful session bean with container-managed transaction (CMT) demarcation can optionally implement the javax.ejb.SessionSynchronization interface.

The SessionSynchronization interface is defined as follows:

package javax.ejb;

public interface javax.ejb.SessionSynchronization {

	void afterBegin() throws RemoteException;
	
	void beforeCompletion() throws RemoteException;
	
	void afterCompletion(boolean committed) throws RemoteException;

}
					

This interface provides the session bean instances with transaction synchronization notifications. The instances can use these notifications, for example, to manage database data they may cache within transactions - e.g., if the Java Persistence API is not used.

Stateful Session Bean implementing SessionSynchronization Interface

Begin transaction sequence:

Begin transaction sequence

Commit transaction sequence:

Commit transaction sequence

All container providers MUST support SessionSynchronization. It is optional only for the bean implementor. If a bean class implements SessionSynchronization, the container MUST invoke the afterBegin, beforeCompletion, and afterCompletion notifications as required by the specification.

Only a Stateful Session Bean with container-managed transaction (CMT) demarcation MAY implement the SessionSynchronization interface. A stateless session bean MUST NOT implement the SessionSynchronization interface.

There is no need for a session bean with bean-managed transaction (BMT) demarcation to rely on the synchronization call backs because the bean is in control of the commit - the bean knows when the transaction is about to be committed and it knows the outcome of the transaction commit.

javax.ejb.EJBContext.setRollbackOnly Method

An enterprise bean with container-managed transaction (CMT) demarcation can use the setRollbackOnly method of its EJBContext object to mark the transaction such that the transaction can NEVER COMMIT. Typically, an enterprise bean marks a transaction for rollback to protect data integrity before throwing an application exception, if the application exception class has not been specified to automatically cause the container to rollback the transaction.

For example, an AccountTransfer bean which debits one account and credits another account could mark a transaction for rollback if it successfully performs the debit operation, but encounters a failure during the credit operation.

The container must handle the EJBContext.setRollbackOnly method invoked from a business method executing with the REQUIRED, REQUIRES_NEW, or MANDATORY transaction attribute as follows:

The container MUST throw the java.lang.IllegalStateException if the EJBContext.setRollbackOnly method is invoked from a business method executing with the SUPPORTS, NOT_SUPPORTED, or NEVER transaction attribute.

javax.ejb.EJBContext.getRollbackOnly method

An enterprise bean with container-managed transaction demarcation can use the getRollbackOnly method of its EJBContext object to test if the current transaction has been marked for rollback. The transaction might have been marked for rollback by the enterprise bean itself, by other enterprise beans, or by other components (outside of the EJB specification scope) of the transaction processing infrastructure.

The container must handle the EJBContext.getRollbackOnly method invoked from a business method executing with the REQUIRED, REQUIRES_NEW, or MANDATORY transaction attribute.

The container MUST throw the java.lang.IllegalStateException if the EJBContext.getRollbackOnly method is invoked from a business method executing with the SUPPORTS, NOT_SUPPORTED, or NEVER transaction attribute.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine