class Mongo::Session::ClientSession

Overview

A client session used to logically bind operations together.

Defined in:

cryomongo/sessions/sessions.cr
cryomongo/sessions/transactions.cr

Instance Method Summary

Instance Method Detail

def abort_transaction(*, write_concern : WriteConcern | Nil = nil) #

Aborts the currently active transaction in this session.

NOTE Raises an error if this session has no transaction.

client = Mongo::Client.new
session = client.start_session
session.start_transaction
client["db"]["collection"].tap { |collection|
  collection.insert_one({_id: 1}, session: session)
  collection.insert_one({_id: 2}, session: session)
}
session.abort_transaction

def advance_cluster_time(cluster_time : ClusterTime) #

This method advances the cluster_time.

NOTE this method is a no-op if the provider cluster time is less than the current cluster time.


def advance_operation_time(operation_time : BSON::Timestamp) #

This method advances the operation_time.

NOTE this method is a no-op if the provider operation time is less than the current operation time.


def cluster_time : ClusterTime | Nil #

This property returns the most recent cluster time seen by this session. If no operations have been executed using this session this value will be null unless advanceClusterTime has been called. This value will also be null when a cluster does not report cluster times.


def commit_transaction(*, write_concern : WriteConcern | Nil = nil) #

Commits the currently active transaction in this session.

NOTE Raises an error if this session has no transaction.

client = Mongo::Client.new
session = client.start_session
session.start_transaction
client["db"]["collection"].tap { |collection|
  collection.insert_one({_id: 1}, session: session)
  collection.insert_one({_id: 2}, session: session)
}
session.commit_transaction

def current_transaction_options : Mongo::Session::TransactionOptions #

Options for the current transaction.


def end #

Aborts any currently active transaction and ends this session.


def is_transaction? #

def operation_time : BSON::Timestamp | Nil #

This property returns the operation time of the most recent operation performed using this session. If no operations have been performed using this session the value will be null unless advanceOperationTime has been called. This value will also be null when the cluster does not report operation times.


def options : Options #

The session options used when creating the session.


def recovery_token : BSON | Nil #

The recoveryToken field enables the driver to recover a sharded transaction's outcome on a new mongos when the original mongos is no longer available.


def recovery_token=(recovery_token : BSON | Nil) #

The recoveryToken field enables the driver to recover a sharded transaction's outcome on a new mongos when the original mongos is no longer available.


def server_description : SDAM::ServerDescription | Nil #

Server description if the session is pinned to a specific mongos.


def start_transaction(**options) #

Starts a new transaction with the given options.

This session's options.default_transaction_options of type TransactionOptions is used when options is omitted.

NOTE Raises an error if this session is already in a transaction.

client = Mongo::Client.new
session = client.start_session

# transaction options arguments are optional
session.start_transaction(
  read_concern: Mongo::ReadConcern.new(level: "snapshot"),
  write_concern: Mongo::WriteConcern.new(w: "majority")
)

def transaction_state : TransactionState #

The state of the transaction.


def transitions_from : TransactionState | Nil #

The state from where the transaction is transitioning.


def with_transaction(**options, &) #

Same as #start_transaction but will commit the transaction after the block returns.

NOTE If an error is thrown, the transaction will be aborted.

client = Mongo::Client.new
session = client.start_session
session.with_transaction {
  client["db"]["collection"].tap { |collection|
    collection.insert_one({_id: 1}, session: session)
    collection.insert_one({_id: 2}, session: session)
  }
}