class

Mongo::Session::ClientSession

Inherits Reference / Object

A client session used to logically bind operations together.

Instance methods

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
Source
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.

Source
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.

Source
cluster_time

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.

Source
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
Source
current_transaction_options

Options for the current transaction.

Source
end

Aborts any currently active transaction and ends this session.

Source
is_transaction?
Source
operation_time

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.

Source
options

The session options used when creating the session.

Source
recovery_token

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.

Source
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.

Source
server_description

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

Source
start_transaction

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")
)
Source
transaction_state

The state of the transaction.

Source
transitions_from

The state from where the transaction is transitioning.

Source
with_transaction

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)
  }
}
Source