- All Superinterfaces:
- AutoCloseable,- BaseSession,- QueryRunner,- Resource,- SimpleQueryRunner
 A Session is a logical container for a causally chained series of
 transactions. Client applications typically work
 with managed transactions, which come in two flavours: transaction
 functions and auto-commit transactions. Managed transactions automatically
 handle the transaction boundaries (BEGIN and
 COMMIT/ROLLBACK) and can also provide other
 supporting features; transaction functions offer retry capabilities, for
 example.
 
Unmanaged transactions are also available but tend to be used by libraries or tooling that require more fine-grained control.
Typically, a session will acquire a TCP connection from a connection pool in order to carry out a transaction. Once the transaction has completed, and the entire result has been consumed, this connection will be released back into the pool. One connection can therefore be adopted by several sessions over its lifetime, although it will only be owned by one at a time. Client applications should never need to deal directly with connection management.
Session implementations are not generally thread-safe. Therefore, multiple sessions should be used when an application requires multiple concurrent threads of database work to be carried out.
- Since:
- 1.0 (Removed async API to AsyncSessionin 4.0)
- 
Method SummaryModifier and TypeMethodDescriptionBegin a new unmanaged transaction.beginTransaction(TransactionConfig config) Begin a new unmanaged transaction with the specifiedconfiguration.voidclose()Signal that you are done using this session.default <T> TexecuteRead(TransactionCallback<T> callback) Execute a unit of work as a single, managed transaction withreadaccess mode and retry behaviour.<T> TexecuteRead(TransactionCallback<T> callback, TransactionConfig config) Execute a unit of work as a single, managed transaction withreadaccess mode and retry behaviour.default <T> TexecuteWrite(TransactionCallback<T> callback) Execute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour.<T> TexecuteWrite(TransactionCallback<T> callback, TransactionConfig config) Execute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour.default voidexecuteWriteWithoutResult(Consumer<TransactionContext> contextConsumer) Execute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour.default voidexecuteWriteWithoutResult(Consumer<TransactionContext> contextConsumer, TransactionConfig config) Execute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour.Return a set of last bookmarks.Run a query with parameters in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.run(String query, TransactionConfig config) Run a query in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.run(Query query, TransactionConfig config) Run a query in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.
- 
Method Details- 
beginTransactionTransaction beginTransaction()Begin a new unmanaged transaction. At most one transaction may exist in a session at any point in time. To maintain multiple concurrent transactions, use multiple concurrent sessions.- Returns:
- a new Transaction
 
- 
beginTransactionBegin a new unmanaged transaction with the specifiedconfiguration. At most one transaction may exist in a session at any point in time. To maintain multiple concurrent transactions, use multiple concurrent sessions.- Parameters:
- config- configuration for the new transaction.
- Returns:
- a new Transaction
 
- 
executeReadExecute a unit of work as a single, managed transaction withreadaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver. The provided unit of work should not return Resultobject as it won't be valid outside the scope of the transaction.- Type Parameters:
- T- the return type of the given unit of work.
- Parameters:
- callback- the callback representing the unit of work.
- Returns:
- a result as returned by the given unit of work.
 
- 
executeReadExecute a unit of work as a single, managed transaction withreadaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver. The provided unit of work should not return Resultobject as it won't be valid outside the scope of the transaction.- Type Parameters:
- T- the return type of the given unit of work.
- Parameters:
- callback- the callback representing the unit of work.
- config- the transaction configuration for the managed transaction.
- Returns:
- a result as returned by the given unit of work.
 
- 
executeWriteExecute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver. The provided unit of work should not return Resultobject as it won't be valid outside the scope of the transaction.- Type Parameters:
- T- the return type of the given unit of work.
- Parameters:
- callback- the callback representing the unit of work.
- Returns:
- a result as returned by the given unit of work.
 
- 
executeWriteWithoutResultExecute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt. This method works equivalently to executeWrite(TransactionCallback), but does not have a return value.- Parameters:
- contextConsumer- the consumer representing the unit of work.
 
- 
executeWriteExecute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver. The provided unit of work should not return Resultobject as it won't be valid outside the scope of the transaction.- Type Parameters:
- T- the return type of the given unit of work.
- Parameters:
- callback- the callback representing the unit of work.
- config- the transaction configuration for the managed transaction.
- Returns:
- a result as returned by the given unit of work.
 
- 
executeWriteWithoutResultdefault void executeWriteWithoutResult(Consumer<TransactionContext> contextConsumer, TransactionConfig config) Execute a unit of work as a single, managed transaction withwriteaccess mode and retry behaviour. The transaction allows for one or more statements to be run.The driver will attempt committing the transaction when the provided unit of work completes successfully. Any exception emitted by the unit of work will result in a rollback attempt and abortion of execution unless exception is considered to be valid for retry attempt by the driver. This method works equivalently to executeWrite(TransactionCallback, TransactionConfig), but does not have a return value.- Parameters:
- contextConsumer- the consumer representing the unit of work.
- config- the transaction configuration for the managed transaction.
 
- 
runRun a query in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.- Parameters:
- query- text of a Neo4j query.
- config- configuration for the new transaction.
- Returns:
- a stream of result values and associated metadata.
 
- 
runRun a query with parameters in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.This method takes a set of parameters that will be injected into the query by Neo4j. Using parameters is highly encouraged, it helps avoid dangerous cypher injection attacks and improves database performance as Neo4j can re-use query plans more often. This version of run takes a Mapof parameters. The values in the map must be values that can be converted to Neo4j types. SeeValues.parameters(Object...)for a list of allowed types.ExampleMap<String, Object> metadata = new HashMap<>(); metadata.put("type", "update name"); TransactionConfig config = TransactionConfig.builder() .withTimeout(Duration.ofSeconds(3)) .withMetadata(metadata) .build(); Map<String, Object> parameters = new HashMap<>(); parameters.put("myNameParam", "Bob"); Result result = session.run("MATCH (n) WHERE n.name = $myNameParam RETURN (n)", parameters, config);- Parameters:
- query- text of a Neo4j query.
- parameters- input data for the query.
- config- configuration for the new transaction.
- Returns:
- a stream of result values and associated metadata.
 
- 
runRun a query in a managed auto-commit transaction with the specifiedconfiguration, and return a result stream.ExampleMap<String, Object> metadata = new HashMap<>(); metadata.put("type", "update name"); TransactionConfig config = TransactionConfig.builder() .withTimeout(Duration.ofSeconds(3)) .withMetadata(metadata) .build(); Query query = new Query("MATCH (n) WHERE n.name = $myNameParam RETURN n.age"); Result result = session.run(query.withParameters(Values.parameters("myNameParam", "Bob")));- Parameters:
- query- a Neo4j query.
- config- configuration for the new transaction.
- Returns:
- a stream of result values and associated metadata.
 
- 
lastBookmarksReturn a set of last bookmarks.When no new bookmark is received, the initial bookmarks are returned. This may happen when no work has been done using the session. Multivalued Bookmarkinstances will be mapped to distinctBookmarkinstances. If no initial bookmarks have been provided, an empty set is returned.- Returns:
- the immutable set of last bookmarks.
 
- 
closevoid close()Signal that you are done using this session. In the default driver usage, closing and accessing sessions is very low cost.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Resource
 
 
-