Skip to content

Transaction Listener

Jan Wiemer edited this page Jan 4, 2021 · 3 revisions

Transaction Listener

Transaction listeners can be registered to add some functionality before / after each prepare / commit / rollback call executed by the JACIS container. Every time the JACIS container will execute a transaction demarcation operation (prepare, commit or rollback) it will execute the following steps:

  • call the before<Operation> method for all registered transaction listeners;

  • execute the operation on each store hosted by the container;

  • call the after<Operation> method for all registered transaction listeners;

All transaction listeners have to implement the JacisTransactionListener interface declaring the following methods:

void beforePrepare(JacisContainer container, JacisTransactionHandle tx);

Called before preparing the transaction for commit.

void afterPrepare(JacisContainer container, JacisTransactionHandle tx);

Called after preparing the transaction for commit.

void beforeCommit(JacisContainer container, JacisTransactionHandle tx);

Called before committing the transaction.

void afterCommit(JacisContainer container, JacisTransactionHandle tx);

Called after committing the transaction.

void beforeRollback(JacisContainer container, JacisTransactionHandle tx);

Called before rolling the transaction back.

void afterRollback(JacisContainer container, JacisTransactionHandle tx);

Called after rolling the transaction back.

Note that not all the six declared methods have to be implemented. Since the methods are declared with an empty default implementation we only have to provide an implementation for the methods they want to overwrite. The following example shows a transaction listener logging after prepare, commit and rollback:

 public class ExampleJacisTransactionListener implements JacisTransactionListener {

    @Override
    public void afterPrepare(JacisContainer container, JacisTransactionHandle tx) {
      System.out.println("prepare finished");
    }

    @Override
    public void afterCommit(JacisContainer container, JacisTransactionHandle tx) {
      System.out.println("commit finished");
    }

    @Override
    public void afterRollback(JacisContainer container, JacisTransactionHandle tx) {
      System.out.println("rollback finished");
    }

  }

To register a transaction listener at the JACIS container it has to be passed as parameter to the registerTransactionListener at the JacisContainer. The following example shows how to register the transaction listener:

    JacisContainer container = new JacisContainer();
    // register an example transaction listener
    JacisTransactionListener listener = new ExampleJacisTransactionListener();
    container.registerTransactionListener(listener);
    ...

Next Chapter: Modification Listener