IMDB Transaction handling

Neo4j Wiki から

Transactions are at the core of Neo4j, you need to be aware of how to use them.


[edit] Neo4j and transactions

With Neo4j, every single operation has to be in a transaction. You'd want that anyhow, yes?!Of course the transactions are ACID. There's more information here and of course in the Transaction class API documentation.

Neo4j transactions are used to partition the storage interactions into logical units and commit successful transactions.

[edit] Transactions in the IMDB example

[edit] Spring injections

To make the code cleaner, we use the Spring functionality to configure a transaction manager and invoke it by using the @Transaction annotation in the code.

Note: The annotations are not as visible when reading the code as the explicit calling of Transaction in the snippet above. Don't get fooled by this - the transactions are still there!

This is how the transactions are set up in the Spring web configuration (src/main/webapp/WEB-INF/imdb-app-servlet.xml):

	<tx:annotation-driven />
	<bean id="transactionManager"
		<property name="transactionManager" ref="neo4jTransactionManagerService" />
		<property name="userTransaction" ref="neo4jUserTransactionService" />
	<bean id="neo4jTransactionManagerService" class="org.neo4j.kernel.impl.transaction.SpringTransactionManager" />
	<bean id="neo4jUserTransactionService" class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
		<constructor-arg index="0" ref="graphDbService" />

This will make Spring delegate the transaction handling to the Neo4j transaction handler.

A typical invocation of transaction handling looks like this:

    public void newActors( final List<ActorData> actorList )
        // code that operates on the graph

[edit] Different contexts

In the IMDB example application, we decided not to put the transactions in the domain layer, but let the consumers of it decide on what should go into one transaction.

There are three classes that handle transactions in the application:


The ActorFindControllerDelegate and MovieFindControllerDelegate classes provide the actor and movie search pages content. They start a single transaction for every time a search is performed.

The ImdbReaderImpl class inserts the bulk IMDB data into the graph. Every set of data will be inside one transaction. This makes it possible for the caller to decide on balancing buffering of data to insert against transaction performance concerns.

By this design - not handling the transactions in the domain layer - the domain layer can be used in a more flexible way providing possible performance optimizations. Just remember that every single Neo4j operation has to be in a transaction.
Important points:
  • all interactions (including read operations) with the graph must be wrapped in a transaction
  • transactions take care of the store/fetch cycle
  • consider logical units of work and performance when designing the transactions

Next part: IMDB Inserting Data Index page: overview

Neo4j のサイト