1 Reply Latest reply on Feb 8, 2007 9:36 AM by Dalibor Kalna

    Hook onto Transaction Manager

    Brett Birschbach Newbie

      My company has code we need to run at the beginning and end of each database transaction in order to set and unset context variables in the database session.

      Is there a way to hook onto the beginning and end of each transaction? Perhaps using interceptors? Or can we override the default transaction manager?

      I've looked around and those are the ideas I've come up with, but I'm not sure if either is possible, and have yet to find an example of someone doing it.

      Any help would be greatly appreciated.

      -Brett Birschbach

        • 1. Re: Hook onto Transaction Manager
          Dalibor Kalna Newbie

          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.