Sessions and Transactions

Sessions

Through the Driver, we open Sessions.

A session is a container for a sequence of transactions. Sessions borrow connections from a pool as required and are considered lightweight and disposable.

It is important to remember that sessions are not the same as database connections. When the Driver connects to the database, it opens up multiple TCP connections that can be borrowed by the session. A query may be sent over multiple connections, and results may be received by the driver over multiple connections.

Instead, sessions should be considered a client-side abstraction for grouping units of work, which also handle the underlying connections. The connections themselves are managed internally by the driver and are not directly exposed to the application.

To open a new session, call the session() method on the driver.

python
Open a new Session
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag="driver.session",indent=0]

This session method takes an optional configuration argument, which can be used to set the database to run any queries against in a multi-database setup, and the default access mode for any queries run within the transaction.

python
Open a new Session with additional arguments
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=sessionWithArgs,indent=0]

If no database is supplied, the default database will be used. This is configured in the dbms.default_database in neo4j.conf, the default value is neo4j. You cannot create multiple databases in Neo4j Aura or in Neo4j Community Edition.

For more information on multi-database setup, see Managing Multiple Databases.

The default access mode is set to write, but this can be overwritten by explicitly calling the execute_read() or execute_write() functions.

Transactions

Through a Session, we can run one or more Transactions.

A transaction comprises a unit of work performed against a database. It is treated in a coherent and reliable way, independent of other transactions.

ACID Transactions

A transaction, by definition, must be atomic, consistent, isolated, and durable. Many developers are familiar with ACID transactions from their work with relational databases, and as such the ACID consistency model has been the norm for some time.

There are three types of transaction exposed by the driver:

  • Auto-commit Transactions

  • Read Transactions

  • Write Transactions

Auto-commit Transactions

Auto-commit transactions are a single unit of work that are immediately executed against the DBMS and acknowledged immediately. You can run an auto-commit transaction by calling the run() method on the session object, passing in a Cypher statement as a string and optionally an object containing a set of parameters.

python
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.run]

For one-off queries only

In the event that there are any transient errors when running a query, the driver will not attempt to retry a query when using session.run(). For this reason, these should only be used for one-off queries and shouldn’t be used in production.

Read Transactions

When you intend to read data from Neo4j, you should execute a Read Transaction. In a clustered environment (including Neo4j Aura), read queries are distributed across the cluster.

The session provides an execute_read() function, which expects a single parameter, a function that represents the unit of work. The first argument passed to the function will be a transaction object, on which you can call the run() function to execute a Cypher statement. As with the session.run example above, the first argument for the run() function should be a Cypher statement, and any parameters in the Cypher statement should be passed as named parameters.

python
Running a Read Transaction
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.readTransaction]

Parameterized Queries

In the query above, the the $ prefix of $title (1) indicates that this value relates to the parameter defined in the second argument (2) of the run() function call.

You do not need to explicitly commit a read transaction.

Write Transactions

If you intend to write data to the database, you should execute a Write Transaction.

In clustered environments, write queries are sent exclusively to the leader of the cluster. The leader of the cluster is then responsible for processing the query and synchronising the transaction across the followers and read-replica servers in the cluster.

The process is identical to running a Read Transaction.

python
Running a Write Transaction
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.writeTransaction,indent=0]

If anything goes wrong within of the unit of work or there is a problem on Neo4j’s side, the transaction will be automatically rolled back and the database will remain in its previous state. If the unit of work succeeds, the transaction will be automatically committed.

Unlike session.run(), if a transient error is received by the driver, for example a connectivity issue with the DBMS, the driver will automatically retry the unit of work.

Manually Creating Transactions

It is also possible to explicitly create a transaction object by calling the begin_transaction() function on the session.

python
Creating an Manual Transaction
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.beginTransaction,indent=0]

This returns a Transaction object identical to the one passed in to the unit of work function when calling execute_read() or execute_write().

This method differs from the execute_read and execute_write() functions, in that the transaction will have to be manually committed or rolled back depending on the outcome of the unit of work.

You can commit a transaction by calling the tx.commit() function, or roll back the transaction by calling tx.rollback().

python
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.beginTransaction.Try]

Closing the Session

Once you are finished with your session, you call the close() method to release any database connections held by that session.

python
Closing a Session
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=session.close]

A Working Example

Click to reveal a complete working example

The following code defines a function that accepts a name parameter, then executes a write transaction to create a :Person node in the people database.

python
Create a Person node in the customers database
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/example/index.py[tag=createPerson]

Check your understanding

1. Valid Query Methods

Which of the following options are valid methods for running a read query through the driver?

  • session.run()

  • session.query()

  • session.execute_read()

  • session.read_transaction()

Hint

You can either run a Cypher statement within an auto-commit transaction or execute a Cypher statement within a managed transaction.

Solution

The answers are session.run() and session.execute_read().

2. Reading from the Database

Now we want to execute the unit_of_work function to query the database within a read transaction.

Which of the functions below should you call?

  • session.read(unit_of_work)

  • session.execute_read(unit_of_work)

  • session.execute_write(unit_of_work)

  • session.read_transaction(unit_of_work)

Hint

You are looking to execute a read query against the database.

Solution

The answer is execute_read

3. Writing to the Database

Now we want to execute the unit_of_work function to create a new node in the database.

Which of the functions below should you call?

  • session.execute_read(unit_of_work)

  • session.execute_write(unit_of_work)

  • session.write_query(unit_of_work)

  • session.write_transaction(unit_of_work)

Hint

You are looking to execute a write query against the database.

Solution

The answer is session.execute_write

Lesson Summary

In this lesson, you have learned about the process of creating sessions and running Cypher queries within transaction functions.

In the next lesson we will look at how we process the results of a query.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?