Struct mongodb::sync::ClientSession
source · [−]pub struct ClientSession { /* private fields */ }
sync
or tokio-sync
only.Expand description
A MongoDB client session. This struct represents a logical session used for ordering sequential
operations. To create a ClientSession
, call start_session
on a
Client
.
ClientSession
instances are not thread safe or fork safe. They can only be used by one thread
or process at a time.
Implementations
sourceimpl ClientSession
impl ClientSession
sourcepub fn cluster_time(&self) -> Option<&ClusterTime>
pub fn cluster_time(&self) -> Option<&ClusterTime>
The highest seen cluster time this session has seen so far.
This will be None
if this session has not been used in an operation yet.
sourcepub fn options(&self) -> Option<&SessionOptions>
pub fn options(&self) -> Option<&SessionOptions>
The options used to create this session.
sourcepub fn advance_cluster_time(&mut self, to: &ClusterTime)
pub fn advance_cluster_time(&mut self, to: &ClusterTime)
Set the cluster time to the provided one if it is greater than this session’s highest seen
cluster time or if this session’s cluster time is None
.
sourcepub fn start_transaction(
&mut self,
options: impl Into<Option<TransactionOptions>>
) -> Result<()>
pub fn start_transaction(
&mut self,
options: impl Into<Option<TransactionOptions>>
) -> Result<()>
Starts a new transaction on this session with the given TransactionOptions
. If no options
are provided, the session’s defaultTransactionOptions
will be used. This session must
be passed into each operation within the transaction; otherwise, the operation will be
executed outside of the transaction.
session.start_transaction(None)?;
let result = coll.insert_one_with_session(doc! { "x": 1 }, None, &mut session)?;
session.commit_transaction()?;
sourcepub fn commit_transaction(&mut self) -> Result<()>
pub fn commit_transaction(&mut self) -> Result<()>
Commits the transaction that is currently active on this session.
session.start_transaction(None)?;
let result = coll.insert_one_with_session(doc! { "x": 1 }, None, &mut session)?;
session.commit_transaction()?;
This operation will retry once upon failure if the connection and encountered error support retryability. See the documentation here for more information on retryable writes.
sourcepub fn abort_transaction(&mut self) -> Result<()>
pub fn abort_transaction(&mut self) -> Result<()>
Aborts the transaction that is currently active on this session. Any open transaction will
be aborted automatically in the Drop
implementation of ClientSession
.
session.start_transaction(None)?;
match execute_transaction(coll, &mut session) {
Ok(_) => session.commit_transaction()?,
Err(_) => session.abort_transaction()?,
}
fn execute_transaction(coll: Collection<Document>, session: &mut ClientSession) -> Result<()> {
coll.insert_one_with_session(doc! { "x": 1 }, None, session)?;
coll.delete_one_with_session(doc! { "y": 2 }, None, session)?;
Ok(())
}
This operation will retry once upon failure if the connection and encountered error support retryability. See the documentation here for more information on retryable writes.
Trait Implementations
sourceimpl From<ClientSession> for ClientSession
impl From<ClientSession> for ClientSession
sourcefn from(async_client_session: AsyncClientSession) -> Self
fn from(async_client_session: AsyncClientSession) -> Self
Converts to this type from the input type.
Auto Trait Implementations
impl !RefUnwindSafe for ClientSession
impl Send for ClientSession
impl Sync for ClientSession
impl Unpin for ClientSession
impl !UnwindSafe for ClientSession
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more