You are currently browsing legacy 4.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Opening a Session

To open session use the openSession method from DocumentStore.

Syntax

There are three overloads of openSession methods

// Open session for a 'default' database configured in 'DocumentStore'
IDocumentSession openSession();

// Open session for a specified database
IDocumentSession openSession(String database);

IDocumentSession openSession(SessionOptions sessionOptions);

The first method is an equivalent of doing

store.openSession(new SessionOptions());

The second method is an equivalent of doing

SessionOptions sessionOptions = new SessionOptions();
sessionOptions.setDatabase(databaseName);
store.openSession(sessionOptions);

Parameters
options OpenSessionOptions Options containing information such as name of database and RequestExecutor.
Return Value
IDocumentSession Instance of a session object.

Example

try (IDocumentSession session = store.openSession()) {
    // code here
}

Important

Always remember to release session allocated resources after usage by invoking the close method or wrapping the session object in the try statement.