[jboss-user] [EJB 3.0] - Re: Hook onto Transaction Manager

dkalna do-not-reply at jboss.com
Thu Feb 8 09:36:27 EST 2007


Hi Brett, if your beans can be Statefull Session Beans then you can implement SessionSynchronization interface:

package javax.ejb;

public interface javax.ejb.SessionSynchronization {
    public abstract void afterBegin( ) throws RemoteException;
    public abstract void beforeCompletion( ) throws RemoteException;
    public abstract void afterCompletion(boolean committed) 
          throws RemoteException;
}

When a method of the SessionSynchronization bean is invoked outside of a transaction scope, the method executes in the Method-Ready state. However, when a method is invoked within a transaction scope (or creates a new transaction), the EJB moves into the Transactional Method-Ready state.

When a transactional method is invoked on a SessionSynchronization bean, the stateful bean becomes part of the transaction, causing the afterBegin( ) callback method defined in the SessionSynchronization interface to be invoked. This method should take care of reading any data from the database and storing the data in the bean's instance fields. The afterBegin( ) method is called before the EJB object delegates the business-method invocation to the EJB instance.

When the afterBegin( ) callback method completes, the business method originally invoked by the client is executed on the EJB instance. Any subsequent business methods invoked within the same transaction will be delegated directly to the EJB instance.

Once a stateful session bean is a part of a transactionwhether it implements SessionSynchronization or notit cannot be accessed by any other transactional context. This is true regardless of whether the client tries to access the EJB with a different context or the EJB's own method creates a new context. If, for example, a method with a transaction attribute of RequiresNew is invoked, the new transactional context causes an error to be thrown. Since the NotSupported and Never attributes specify a different transactional context (no context), invoking a method with these attributes also causes an error. A stateful session bean cannot be removed while it is involved in a transaction. This means that invoking an @Remove annotated method while the SessionSynchronization bean is in the middle of a transaction will cause an error to be thrown.

At some point, the transaction in which the SessionSynchronization bean has been enrolled will come to an end. If the transaction is committed, the SessionSynchronization bean will be notified through its beforeCompletion( ) method. At this time, the EJB should write its cached data to the database. If the transaction is rolled back, the beforeCompletion( ) method will not be invoked, avoiding the pointless effort of writing changes that won't be committed to the database.

The afterCompletion( ) method is always invoked, whether the transaction ended successfully with a commit or unsuccessfully with a rollback. If the transaction was a successwhich means that beforeCompletion( ) was invokedthe committed parameter of the afterCompletion( ) method will be true. If the transaction was unsuccessful, then the committed parameter will be false.

It may be desirable to reset the stateful session bean's instance variables to some initial state if the afterCompletion( ) method indicates that the transaction was rolled back.

Bye
Dalibor

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4013049#4013049

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4013049



More information about the jboss-user mailing list