- All Superinterfaces:
AutoCloseable
,QueryRunner
,Resource
,SimpleQueryRunner
Logical container for an atomic unit of work.
A driver Transaction object corresponds to a server transaction.
Transactions are typically wrapped in a try-with-resources block
which ensures in case of any error in try block, the transaction is
automatically rolled back on close. Note that ROLLBACK
is the
default action unless commit()
is called before closing.
try(Transaction tx = session.beginTransaction())
{
tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice"));
tx.commit();
}
Blocking calls are: commit()
, rollback()
, close()
and various overloads of SimpleQueryRunner.run(Query)
.- Since:
- 1.0
- See Also:
-
Method Summary
-
Method Details
-
commit
void commit()Commit this current transaction. When this method returns, all outstanding queries in the transaction are guaranteed to have completed, meaning any writes you performed are guaranteed to be durably stored. No more queries can be executed inside this transaction once this transaction is committed. After this method is called, the transaction cannot be committed or rolled back again. You must call this method before callingclose()
to have your transaction committed. If a transaction is not committed or rolled back before close, the transaction will be rolled back by default inclose()
.Example:
try(Transaction tx = session.beginTransaction() ) { tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice")); tx.commit(); }
-
rollback
void rollback()Roll back this current transaction. No more queries can be executed inside this transaction once this transaction is committed. After this method has been called, the transaction cannot be committed or rolled back again. If a transaction is not committed or rolled back before close, the transaction will be rolled back by default inclose()
.Example:
try(Transaction tx = session.beginTransaction() ) { tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice")); tx.rollback(); }
-
close
void close()Close the transaction. If the transaction has beencommitted
orrolled back
, the close is optional and no operation is performed inside. Otherwise, the transaction will be rolled back by default by this method.Example:
try(Transaction tx = session.beginTransaction() ) { tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice")); }
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceResource
-