public interface Session extends Resource, StatementRunner
A Session hosts a series of transactions
carried out against a database. Within the database, all statements are
carried out within a transaction. Within application code, however, it is
not always necessary to explicitly begin a
transaction. If a statement is StatementRunner.run(java.lang.String, org.neo4j.driver.v1.Value) directly against a Session, the server will automatically BEGIN and
COMMIT that statement within its own transaction. This type
of transaction is known as an autocommit transaction.
Explicit transactions allow multiple statements to be committed as part of a single atomic operation and can be rolled back if necessary. They can also be used to ensure causal consistency, meaning that an application can run a series of queries on different members of a cluster, while ensuring that each query sees the state of graph at least as up-to-date as the graph seen by the previous query. For more on causal consistency, see the Neo4j clustering manual.
Typically, a session will wrap a TCP connection. Such a connection will be acquired from a connection pool and released back there when the session is destroyed. One connection can therefore be adopted by many sessions, although by only one at a time. Application code should never need to deal directly with connection management.
A session inherits its destination address and permissions from its underlying connection. This means that one session may only ever target one machine within a cluster and does not support re-authentication. To achieve otherwise requires creation of a separate session.
Similarly, multiple sessions should be used when working with concurrency; session implementations are generally not thread safe.
| Modifier and Type | Method and Description |
|---|---|
Transaction |
beginTransaction()
Begin a new explicit transaction.
|
Transaction |
beginTransaction(String bookmark)
Deprecated.
This method is deprecated in favour of
Driver.session(String) that accepts an initial
bookmark. Session will ensure that all nested transactions are chained with bookmarks to guarantee
causal consistency. This method will be removed in the next major release. |
void |
close()
Signal that you are done using this session.
|
String |
lastBookmark()
Return the bookmark received following the last completed
transaction.
|
<T> T |
readTransaction(TransactionWork<T> work)
Execute given unit of work in a
read transaction. |
void |
reset()
Deprecated.
This method should not be used and violates the expected usage pattern of
Session objects.
They are expected to be not thread-safe and should not be shared between thread. However this method is only
useful when Session object is passed to another monitoring thread that calls it when appropriate.
It is not useful when Session is used in a single thread because in this case close()
can be used. Since version 3.1, Neo4j database allows users to specify maximum transaction execution time and
contains procedures to list and terminate running queries. These functions should be used instead of calling
this method. |
<T> T |
writeTransaction(TransactionWork<T> work)
Execute given unit of work in a
write transaction. |
run, run, run, run, run, typeSystemTransaction beginTransaction()
Transaction@Deprecated Transaction beginTransaction(String bookmark)
Driver.session(String) that accepts an initial
bookmark. Session will ensure that all nested transactions are chained with bookmarks to guarantee
causal consistency. This method will be removed in the next major release.bookmark - a reference to a previous transactionTransaction<T> T readTransaction(TransactionWork<T> work)
read transaction.
Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
Transaction.close() or transaction is explicitly marked for failure via Transaction.failure().
T - the return type of the given unit of work.work - the TransactionWork to be applied to a new read transaction.<T> T writeTransaction(TransactionWork<T> work)
write transaction.
Transaction will automatically be committed unless exception is thrown from the unit of work itself or from
Transaction.close() or transaction is explicitly marked for failure via Transaction.failure().
T - the return type of the given unit of work.work - the TransactionWork to be applied to a new write transaction.String lastBookmark()
@Deprecated void reset()
Session objects.
They are expected to be not thread-safe and should not be shared between thread. However this method is only
useful when Session object is passed to another monitoring thread that calls it when appropriate.
It is not useful when Session is used in a single thread because in this case close()
can be used. Since version 3.1, Neo4j database allows users to specify maximum transaction execution time and
contains procedures to list and terminate running queries. These functions should be used instead of calling
this method.void close()
Driver.
When this method returns, all outstanding statements in the session are guaranteed to
have completed, meaning any writes you performed are guaranteed to be durably stored.close in interface AutoCloseableclose in interface ResourceCopyright © 2017. All rights reserved.