org.neo4j.api.core
Class Transaction

java.lang.Object
  extended by org.neo4j.api.core.Transaction

public class Transaction
extends Object

A utility class to manage transactions in Neo. All operations that work with the node space (even read operations) must be wrapped in a transaction. Fortunately, the Transaction class makes this very easy. Here's the idiomatic use of transactions in Neo:

 Transaction tx = Transaction.begin();
 try
 {
        ... // any operation that works with the node space
        tx.success();
 }
 finally
 {
        tx.finish();
 } 

Let's walk through this example line by line. First we retrieve a Transaction object by invoking the static begin() factory method. This creates a new Transaction instance which has internal state to keep track of whether the current transaction is successful. Then we wrap all operations that work with the node space in a try-finally block. At the end of the block, we invoke the tx.success() method to indicate that the transaction is successful. As we exit the block, the finally clause will kick in and tx.finish will commit the transaction if the internal state indicates success or else mark it for rollback.

If an exception is raised in the try-block, tx.success() will never be invoked and the internal state of the transaction object will cause tx.finish() to roll back the transaction. This is very important: unless success() is invoked, the transaction will fail upon finish(). A transaction can be explicitly marked for rollback by invoking the tx.failure() method.


Method Summary
static Transaction begin()
          Starts a new transaction.
 void failure()
          Marks this transaction as failed, which means that it will inexplicably be rolled back upon invocation of finish().
 void finish()
          Commits or marks this transaction for rollback, depending on whether success() or failure() has been previously invoked.
 void success()
          Marks this transaction as successful, which means that it will be commited upon invocation of finish() unless failure() has or will be invoked before then.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

begin

public static Transaction begin()
Starts a new transaction.

Returns:
a transaction object representing the current transaction

failure

public void failure()
Marks this transaction as failed, which means that it will inexplicably be rolled back upon invocation of finish(). Once this method has been invoked, it doesn't matter how many times success() is invoked -- the transaction will still be rolled back.


success

public void success()
Marks this transaction as successful, which means that it will be commited upon invocation of finish() unless failure() has or will be invoked before then.


finish

public void finish()
Commits or marks this transaction for rollback, depending on whether success() or failure() has been previously invoked.



Copyright © 2007 Neo4j. All Rights Reserved.