This section describes how Cypher queries work with database transactions.

All Cypher queries run within transactions. Modifications done by updating queries are held in memory by the transaction until it is committed, at which point the changes are persisted to disk and become visible to other transactions. If an error occurs - either during query evaluation, such as division by zero, or during commit, such as constraint violations - the transaction is automatically rolled back, and no changes are persisted in the graph.

In short, an updating query always either fully succeeds, or does not succeed at all.

A query that makes a large number of updates consequently uses large amounts of memory since the transaction holds changes in memory. For memory configuration in Neo4j, see the Neo4j Operations Manual → Memory configuration.

Transactions can be either explicit or implicit.

  • Explicit transactions:

    • Are opened by the user.

    • Can execute multiple Cypher queries in sequence.

    • Are committed, or rolled back, by the user.

  • Implicit transactions, sometimes called auto-commit transactions or :auto transactions:

    • Are opened automatically.

    • Can execute a single Cypher query.

    • Are committed automatically when the query finishes successfully.

Queries that start separate transactions themselves, such as queries using CALL { ... } IN TRANSACTIONS, are only allowed in implicit mode. Explicit transactions cannot be managed directly from queries, they must be managed via APIs or tools.

For examples of the API, or the commands used to start and commit transactions, refer to the API or tool-specific documentation:

When writing procedures or using Neo4j embedded, remember that all iterators returned from an execution result should be either fully exhausted or closed. This ensures that the resources bound to them are properly released.

DBMS Transactions

Beginning a transaction while connected to a DBMS will start a DBMS-level transaction. A DBMS-level transaction is a container for database transactions.

A database transaction is started when the first query to a specific database is issued. Database transactions opened inside a DBMS-level transaction are committed or rolled back when the DBMS-level transaction is committed or rolled back.

For an example of how queries to multiple databases can be issued in one transaction, see Databases and execution context in the Neo4j Driver manuals.

DBMS transactions have the following limitations:

  • Only one database can be written to in a DBMS transaction

  • Cypher operations fall into the following main categories:

    • Operations on graphs.

    • Schema commands.

    • Administration commands.

    It is not possible to combine any of these workloads in a single DBMS transaction.