Mongo::Session::ClientSession
Inherits Reference / Object
A client session used to logically bind operations together.
Instance methods
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
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.
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.
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.
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
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.
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.
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.
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")
)
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)
}
}