Database Reference
In-Depth Information
This code begins by making use of the Java 7 try-with-resource statement to create a new
transaction and define a block of code against which this transaction will apply. Any state-
ments that interact with the database and are executed within this block will all operate
against the same transaction. The call to the success method at the end of the try
block signifies that a commit should occur when the transaction finishes (or the resource
“closes,” in try-with-resource terminology). The transaction finishes (or closes) when the
code block completes. Neo4j will then ensure that this transaction is committed (having
detectedthatthe success methodwaspreviouslycalled).Ifanexceptionisthrownwhile
“doingsomethingwiththedatabase,”the success methodwon'tbecalled,andthetrans-
action will be rolled back when it finishes. Neo4j's decision logic regarding whether to
commit or roll back a transaction is based on whether the success or failure meth-
od was previously called. If you want to roll back a transaction explicitly, such as from
a conditional code block, you can invoke the failure method, and the transaction will
be unconditionally rolled back at the end of the block. Calling neither the success nor
failure method will also result in the transaction being rolled back (default).
Transactions before Neo4j 2.0
In older versions of Neo4j (1.9 and earlier), managing transactions was a bit more com-
plicated, mainly because of the lack of auto-closable resources before Java 7, which Neo4j
complies with since version 2.0.
If you're using an older version of Neo4j, the idiomatic way to handle transactions is as
follows:
Transaction tx = graphDatabaseService.beginTx();
try {
//do something
tx.success();
} finally {
tx.finish();
}
This style is still available but has been deprecated in Neo4j 2.0.
Search WWH ::




Custom Search