6

What is the difference between the Session.run() and transaction.run() in Neo4j Bolt driver?

My knowledge is:

Session.run() will execute a single statement    
transaction.run() executes multiple statements.

Those are the information I know which are correct. What are the all other differences?

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
Jack Daniel
  • 2,527
  • 3
  • 31
  • 52

1 Answers1

23

Session.run() will actually create a transaction, execute the statement, and commit the transaction. Transaction.run() will leave the transaction open until you commit it, but the statement will still be sent and interpreted and executed, and results will be returned. However, any changes won't actually be persisted into the datastore, and won't be visible to queries outside of the transaction. You have to mark the transaction as successful and commit it or it will be rolled back.

You should try not to use transactions; open transactions prevent changes to indexes and constraints and increase memory usage. The only reason to use transactions is for the rollback potential; if you want to see what the results of the query are, and maybe undo it depending on those results, then use a transaction. Otherwise use a session.

Tore Eschliman
  • 2,477
  • 10
  • 16
  • Excellent way of answering. Thanks. – Jack Daniel Sep 18 '16 at 16:51
  • Another thing that's worth mentioning - from my experience, transactions are significally more performant when working with large data sets – Aviran Katz Jul 09 '17 at 15:15
  • That depends very much on the nature of the work you're doing. Literally every interaction is performed within a transaction, the only difference is that using `Session.run()` will automatically open and close it during execution, so you have to take the overhead of committing your transaction each statement. This would only affect performance if you are spreading your queries across multiple statements, and there's almost always a way to combine them, which is where you will see the biggest gains in performance. – Tore Eschliman Jul 10 '17 at 15:47
  • @ToreEschliman When session is being used, if multiple statements are executed, does session take care of atomicity. Referring to this example here which does session.run two times: https://github.com/neo4j-examples/neo4j-movies-template/blob/master/api/models/users.js#L10 . Also when does session get's auto committed ? Just before session getting destroyed (ref count) ? – Royal Pinto Sep 18 '17 at 14:06
  • 1
    Each statement run on a session is encapsulated in its own transaction and committed as its own atomic unit; two statements run on the same session object have no relation to each other. If you need multiple actions to be executed atomically, you need to either a) figure out a way to roll them into the same Cypher statement, or b) if you really do have to apply client-side logic in the middle of an atomic transaction, use `transaction.run` instead, though that incurs all the costs mentioned above. – Tore Eschliman Sep 19 '17 at 17:11
  • 4
    I had this question too, the answer seems to contradict the documentation, can someone shed some light into this?: "Auto-commit transactions are intended to be used for simple use cases such as when learning Cypher or writing one-off scripts. It is not recommended to use auto-commit transactions in production environments or when performance or resilience are a primary concern." https://neo4j.com/docs/driver-manual/1.7/sessions-transactions/ – G Gallegos Jan 12 '20 at 21:16
  • It's been a few years since I worked with this but you basically had: 1. Inidividual queries each in an auto-commit transaction: Inefficient, fine for learning. 2. Multiple queries in a transaction: Extra efficiency in network usage and server resources, better than 1. 3. Multiple queries composed into a single query, executed via auto-commit transaction: Strictly better than 2, because it saves on the IO round-trips. Query writing is somewhat harder because you need to think about basic query optimization to avoid really inefficient compositions. – Tore Eschliman Dec 05 '21 at 18:00