If you don't want to use bean-managed transactions, but you do want to be notified when a transaction starts and when one completes. Being notified of transaction events allows your beans to manage their member state variables appropriately.

For instance, when a transaction starts, you want to make sure the EJB has initialized its state variables. When the transaction completes, you want to make sure you are preparing state variables for the next transaction. To get information about transaction status for stateful session beans, implement the javax.ejb.SessionSynchronization interface.

The session bean in this tip receives a callback for the beginning, successful completion, and postcompletion of a transaction.

public class SampleBean implements SessionBean, SessionSynchronization {
    private Object stateVariable;
    // to be used to prepare data for use inside a transaction:
    public void afterBegin() throws RemoteException {
        // transaction has started:
        stateVariable = initializeState();
    // to be used to save the state of data that was created from the
    // business method and needs to be preserved:
    public void beforeCompletion() throws RemoteException {
        // transaction is about to complete
    // provides a boolean parameter indicating whether the
    // transaction has been marked for a rollback:
    public void afterCompletion(boolean committedthrows RemoteException {
        // test for rollback
        if (! committed) {
            stateVariable = initializeState();