public interface Transaction extends Resource, QueryRunner
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 QueryRunner.run(Query)
.Session.run(java.lang.String, org.neo4j.driver.TransactionConfig)
,
QueryRunner
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the transaction.
|
void |
commit()
Commit this current transaction.
|
void |
rollback()
Roll back this current transaction.
|
void commit()
close()
to have your transaction committed.
If a transaction is not committed or rolled back before close,
the transaction will be rolled back by default in close()
.
Example:
try(Transaction tx = session.beginTransaction() )
{
tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice"));
tx.commit();
}
void rollback()
close()
.
Example:
try(Transaction tx = session.beginTransaction() )
{
tx.run("CREATE (a:Person {name: $name})", parameters("name", "Alice"));
tx.rollback();
}
void close()
committed
or rolled 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"));
}
close
in interface AutoCloseable