イベントフレームワーク

Neo4j Wiki から

The Neo4j kernel includes a simple and specialized event framework for hooking in handlers for transaction life cycles and for the graph database life cycle itself.

目次

[edit] Transaction events

Transaction event handlers can be registered at the graph database instance. Once registered it will receive events with a summary of what has happened in each transaction before it will be committed as well as after it was committed or rolled back. There's no guarantee that a listener will receive events about a transaction if it's registered in the middle of that transaction.

If a handler has had its beforeCommit method successfully executed it will receive a call to afterCommit or afterRollback after the transaction has been committed (or rolled back).

[edit] Contents of an event

All modifications made in a transaction are summarized in a TransactionData object. Such an object will first be passed to a handler's beforeCommit method when the transaction is still open. This makes the handler able to still modify the graph in the same transaction, however since the order in which handlers receive events is unspecified there's no guarantee that other handlers will be able to see changes made by a handler.

The transaction data will contain the summary of modifications in a transaction, a summary of what has been modified compared to how those affected objects (nodes, relationships and properties) was prior to starting the transaction. Here are some examples of what will end up in the transaction data in different scenarios (assuming each code block will be executed in a transaction):

Node myNode = graphDb.createNode();
myNode.setProperty( "name", "Thomas Anderson" );
Node otherNode = graphDb.createNode();
Relationship relationship = myNode.createRelationshipTo( myNode, KNOWS );
relationship.setProperty( "date", new Date().getTime() );

List of changes:

  • Created myNode
  • Created otherNode
  • Created relationship
  • Assigned property name = Thomas Anderson on myNode
  • Assigned property date = 1234567890 on relationship

myNode.setProperty( "name", "Neo" );
myNode.setProperty( "name", "Thomas A. Anderson" );

List of changes:

  • Assigned property name = Thomas A. Anderson on myNode where the overwritten value was Thomas Anderson

NOTE the name = Neo value is ignore since it's overwritten (and hence discarded) in the same transaction.


myNode.setProperty( "alias", "Neo" );
relationship.delete();
myNode.delete();

List of changes:

  • Deleted myNode
  • Deleted relationship
  • Removed property name = Thomas A. Anderson from myNode
  • Removed property date = 1234567890 from relationship

NOTE When deleting a node or relationship all its current properties will show up as removed properties. Also note that alias = Neo is ignored since the node is deleted in the same transaction, hence that value is discarded.

[edit] Affecting the life cycle

A registered handler can affect the transaction in the beforeCommit method. It can modify the transaction before it's committed, but it can also throw an exception, making the transaction fail with a TransactionFailureException being thrown when committing the transaction. A use case for that could be a handler which sits and verifies all modifications before they are committed and discards transactions that doesn't adhere to its criterias.

[edit] Kernel events

Kernel event handlers can be registered and receive notifications about when the graph database is about to be shut down, so that any component depending on it can shut down before it.

It can also receive events about if the graph database enters a state from which it cannot continue, i.e. kernel panic.

[edit] See also

トランザクション

Neo4j のサイト
ツールボックス