Session: Opening a session

To open synchronous session use openSession method from DocumentStore.

Syntax

There are three overloads of openSession method

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

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

public IDocumentSession openSession(OpenSessionOptions sessionOptions);

First method is a equivalent of doing

store.openSession(new OpenSessionOptions());

Second method is a equivalent of doing

OpenSessionOptions sessionOptions = new OpenSessionOptions();
sessionOptions.setDatabase(databaseName);
store.openSession(sessionOptions);
Parameters
options OpenSessionOptions Options containing information such as name of database on which session will work and credentials that will be used.
Return Value
IDocumentSession Instance of a session object that implements IDocumentSession interface.

Example I

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

Remarks

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