client_session – Logical sessions for sequential operations

    Requires MongoDB 3.6.

    New in version 3.6.

    If causal_consistency is True (the default), read operations that use the session are causally after previous read and write operations. Using a causally consistent session, an application can read its own writes and is guaranteed monotonic reads, even when reading from replica set secondaries.

    See also

    The MongoDB documentation on

    causal-consistency

    New in version 3.7.

    MongoDB 4.0 adds support for transactions on replica set primaries. A transaction is associated with a . To start a transaction on a session, use ClientSession.start_transaction() in a with-statement. Then, execute an operation within the transaction by passing the session to the operation:

    Upon normal completion of with session.start_transaction() block, the transaction automatically calls . If the block exits with an exception, the transaction automatically calls ClientSession.abort_transaction().

    In general, multi-document transactions only support read/write (CRUD) operations on existing collections. However, MongoDB 4.4 adds support for creating collections and indexes with some limitations, including an insert operation that would result in the creation of a new collection. For a complete description of all the supported and unsupported operations see the .

    A session may only have a single active transaction at a time, multiple transactions on the same session can be executed in sequence.

    New in version 3.9.

    PyMongo 3.9 adds support for transactions on sharded clusters running MongoDB >=4.2. Sharded transactions have the same API as replica set transactions. When running a transaction against a sharded cluster, the session is pinned to the mongos server selected for the first operation in the transaction. All subsequent operations that are part of the same transaction are routed to the same mongos server. When the transaction is completed, by running either commitTransaction or abortTransaction, the session is unpinned.

    See also

    The MongoDB documentation on

    transactions

    New in version 3.12.

    MongoDB 5.0 adds support for snapshot reads. Snapshot reads are requested by passing the snapshot option to . If snapshot is True, all read operations that use this session read data from the same snapshot timestamp. The server chooses the latest majority-committed snapshot timestamp when executing the first read operation using the session. Subsequent reads on this session read from the same snapshot timestamp. Snapshot reads are also supported when reading from replica set secondaries.

    Snapshot Reads Limitations

    Snapshot reads sessions are incompatible with causal_consistency=True. Only the following read operations are supported in a snapshot reads session:

    class pymongo.client_session.ClientSession(client, server_session, options, authset, implicit)

    A session for ordering sequential operations.

    ClientSession instances are not thread-safe or fork-safe. They can only be used by one thread or process at a time. A single cannot be used to run multiple operations concurrently.

    Should not be initialized directly by application developers - to create a ClientSession, call .

    • abort_transaction()

      Abort a multi-statement transaction.

      New in version 3.7.

    • advance_cluster_time(cluster_time)

      Update the cluster time for this session.

    • The MongoClient this session was created from.

    • cluster_time

      The cluster time returned by the last operation executed in this session.

    • commit_transaction()

      Commit a multi-statement transaction.

      New in version 3.7.

    • end_session()

      Finish this session. If a transaction has started, abort it.

      It is an error to use the session after the session has ended.

    • has_ended

      True if this session is finished.

    • in_transaction

      True if this session has an active multi-statement transaction.

      New in version 3.10.

    • operation_time

      The operation time returned by the last operation executed in this session.

    • options

      The this session was created with.

    • session_id

      A BSON document, the opaque server session identifier.

    • start_transaction(read_concern=None, write_concern=None, read_preference=None, max_commit_time_ms=None)

      Start a multi-statement transaction.

      Takes the same arguments as TransactionOptions.

      Changed in version 3.9: Added the max_commit_time_ms option.

      New in version 3.7.

    • with_transaction(callback, read_concern=None, write_concern=None, read_preference=None, max_commit_time_ms=None)

      Execute a callback in a transaction.

      This method starts a transaction on this session, executes callback once, and then commits the transaction. For example:

      To pass arbitrary arguments to the callback, wrap your callable with a like this:

      In the event of an exception, with_transaction may retry the commit or the entire transaction, therefore callback may be invoked multiple times by a single call to with_transaction. Developers should be mindful of this possiblity when writing a callback that modifies application state or has any other side-effects. Note that even when the callback is invoked multiple times, with_transaction ensures that the transaction will be committed at-most-once on the server.

      instances are not thread-safe or fork-safe. Consequently, the callback must not attempt to execute multiple operations concurrently.

      When callback raises an exception, with_transaction automatically aborts the current transaction. When callback or commit_transaction() raises an exception that includes the "TransientTransactionError" error label, with_transaction starts a new transaction and re-executes the callback.

      When raises an exception with the "UnknownTransactionCommitResult" error label, with_transaction retries the commit until the result of the transaction is known.

      This method will cease retrying after 120 seconds has elapsed. This timeout is not configurable and any exception raised by the callback or by ClientSession.commit_transaction() after the timeout is reached will be re-raised. Applications that desire a different timeout duration should not use this method.

      New in version 3.9.

    class pymongo.client_session.SessionOptions(causal_consistency=None, default_transaction_options=None, snapshot=False)

    Options for a new .

    Changed in version 3.12: Added the snapshot parameter.

    • causal_consistency

      Whether causal consistency is configured.

    • default_transaction_options

      The default TransactionOptions to use for transactions started on this session.

      New in version 3.7.

    • snapshot

      Whether snapshot reads are configured.

      New in version 3.12.

    class pymongo.client_session.TransactionOptions(read_concern=None, write_concern=None, read_preference=None, max_commit_time_ms=None)

    Options for ClientSession.start_transaction().

    Changed in version 3.9: Added the max_commit_time_ms option.

    New in version 3.7.

    • max_commit_time_ms

      The maxTimeMS to use when running a commitTransaction command.

      New in version 3.9.

    • read_concern

      This transaction’s .

    • read_preference

      This transaction’s ReadPreference.