Interface Transaction

All Superinterfaces:
AutoCloseable, QueryRunner, Resource, SimpleQueryRunner

public interface Transaction extends Resource, QueryRunner
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

    Modifier and Type
    Method
    Description
    void
    Close the transaction.
    void
    Commit this current transaction.
    void
    Roll back this current transaction.

    Methods inherited from interface org.neo4j.driver.util.Resource

    isOpen

    Methods inherited from interface org.neo4j.driver.SimpleQueryRunner

    run, run, run, run, run
  • 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 calling 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();
       }
       
    • 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 in close().
      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 been 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"));
       }
       
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Resource