4.8. Transaction events

A transaction event listener can be registered to receive Neo4j database transaction events. Once it has been registered at a DatabaseManagementService instance, it receives transaction events for the database with which it was registered. Listeners get notified about transactions that have performed any write operation, and that will be committed. If Transaction#commit() has not been called, or the transaction was rolled back with Transaction#rollback(), it will be rolled back and no events are sent to the listener.

Before a transaction is committed, the listeners’s beforeCommit method is called with the entire diff of modifications made in the transaction. At this point the transaction is still running, so changes can still be made. The method may also throw an exception, which will prevent the transaction from being committed. If the transaction is rolled back, a call to the listener’s afterRollback method will follow.

The order in which listeners are executed is undefined — there is no guarantee that changes made by one listener will be seen by other listeners.

If beforeCommit is successfully executed in all registered listeners, the transaction is committed and the afterCommit method is called with the same transaction data. This call also includes the object returned from beforeCommit.

In afterCommit the transaction has been closed, and access to anything outside TransactionData requires a new transaction to be opened. A TransactionEventListener gets notified about transactions that have any changes accessible via TransactionData. Some indexing and schema changes will not trigger these events.

The following example shows how to register a listener for a specific database, and perform basic operations on top of the transaction change set.

Register a transaction event listener and inspect the change set: