pub struct Transaction<'coll, 'db: 'coll> { /* private fields */ }
Expand description
Represents an active transaction.
This structure is a transaction guard for an EJDB collection. It employs the RAII pattern: a value of this structure is returned when a transaction is started and when this value is dropped, the transaction is committed or aborted.
By default when a transaction goes out of scope, it is aborted. This is done because of the way how errors are handled in Rust: if you interleave working with an EJDB collection with other, potentially failing operations, then it is possible for an error to cause an early return, dropping the transaction in progress. In this case aborting the transaction is usually the most natural option.
However, it is possible to change the default behavior with set_commit()/set_abort()
methods,
and it is also possible to commit or abort the transaction with commit()
/abort()
methods.
finish()
method is essentially equivalent to dropping the transaction guard, except that
it returns a value which may contain an error (for regular drops any errors are ignored).
will_commit()
/will_abort()
methods return true
if their respective action will be taken
upon finishing.
In EJDB transactions can only span one collection, therefore transactions created from a collection has a direct lifetime dependency on it.
See Collection::begin_transaction()
documentation for examples.
Implementations§
Source§impl<'coll, 'db> Transaction<'coll, 'db>
impl<'coll, 'db> Transaction<'coll, 'db>
Sourcepub fn will_commit(&self) -> bool
pub fn will_commit(&self) -> bool
Checks whether this transaction will be committed upon drop.
Returns true
if this transaction will be committed when dropped or when finish()
method is called.
Sourcepub fn will_abort(&self) -> bool
pub fn will_abort(&self) -> bool
Checks whether this transaction will be aborted upon drop.
Returns true
if this transaction will be aborted when dropped or when finish()
method is called.
Sourcepub fn set_commit(&mut self)
pub fn set_commit(&mut self)
Makes this transaction commit when dropped.