Open a Session


  • A Session object is obtained from the Document Store.

  • Open a session using open_session().

  • Various Session options can be configured using the SessionOptions object.
    If no database is specified in the options then the Default Database (stored in the Document Store) is assumed.

  • Be sure to wrap the session variable using a 'with' statement to ensure proper disposal.

  • In this page:


Syntax

Use open_session() to open a session from the Document Store.

# Open a Session, you may pass either specified database, or preconfigured SessionOptions object
# Passing no optional arguments opens a session for the default database configured in DocumentStore.database
def open_session(
    self, database: Optional[str] = None, session_options: Optional[SessionOptions] = None
) -> DocumentSession:
    ...
Parameter Type Description
database str The session will operate on this database,
overriding the default database set in the document store.
options SessionOptions An object with Session configuration options. See details below.
Return Value Description
IDocumentSession Instance of a Session object

Session options

  • The SessionOptions object contains various options to configure the Session's behavior.
Option Type Description Default Value
database str The Session will operate on this database,
overriding the Default Database.
None - the Session operates on the Default Database
no_tracking bool True - The Session tracks changes made to all entities it loaded, stored, or queried for.
False - Tracking will be turned off.
Learn more in Disable tracking
False
no_caching bool True - Server responses will not be cached.
False - The Session caches the server responses.
Learn more in Disable caching
False
request_executor RequestExecutor ( Advanced option )
The request executor the Session should use.
None - the default request executor is used
transaction_mode TransactionMode Specify the Session's transaction mode
SINGLE_NODE / CLUSTER_WIDE
Learn more in Cluster-wide vs. Single-node
SINGLE_NODE
  • Experts Only:
Option Type Description Default Value
disable_atomic_document_writes_in_cluster_wide_transaction bool Experts only
True - Disable Atomic-Guards in cluster-wide sessions.
False - Automatic atomic writes in cluster-wide sessions are enabled.
Learn more in Atomic-Guards
False

Open session example

  • The following example opens a cluster-wide Session:

with DocumentStore() as store:
    store.open_session()
    # - is equivalent to:
    store.open_session(session_options=SessionOptions())

    # The second overload -
    store.open_session("your_database_name")
    # - is equivalent to:
    store.open_session(session_options=SessionOptions(database="your_database_name"))