pub struct TransactionBuilder<'a, C> { /* private fields */ }
postgres_backend
only.Expand description
Used to build a transaction, specifying additional details.
This struct is returned by .build_transaction
.
See the documentation for methods on this struct for usage examples.
See the PostgreSQL documentation for SET TRANSACTION
for details on the behavior of each option.
Implementations§
source§impl<'a, C> TransactionBuilder<'a, C>
impl<'a, C> TransactionBuilder<'a, C>
sourcepub fn new(connection: &'a mut C) -> Self
Available on crate feature i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.
pub fn new(connection: &'a mut C) -> Self
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.Creates a new TransactionBuilder.
sourcepub fn read_only(self) -> Self
pub fn read_only(self) -> Self
Makes the transaction READ ONLY
§Example
conn.build_transaction()
.read_only()
.run::<_, diesel::result::Error, _>(|conn| {
let read_attempt = users.select(name).load::<String>(conn);
assert!(read_attempt.is_ok());
let write_attempt = diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn);
assert!(write_attempt.is_err());
Ok(())
})?;
sourcepub fn read_write(self) -> Self
pub fn read_write(self) -> Self
Makes the transaction READ WRITE
This is the default, unless you’ve changed the
default_transaction_read_only
configuration parameter.
§Example
conn.build_transaction()
.read_write()
.run(|conn| {
let read_attempt = users.select(name).load::<String>(conn);
assert!(read_attempt.is_ok());
let write_attempt = diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn);
assert!(write_attempt.is_ok());
Ok(())
})
sourcepub fn deferrable(self) -> Self
pub fn deferrable(self) -> Self
Makes the transaction DEFERRABLE
§Example
conn.build_transaction()
.deferrable()
.run(|conn| Ok(()))
sourcepub fn not_deferrable(self) -> Self
pub fn not_deferrable(self) -> Self
Makes the transaction NOT DEFERRABLE
This is the default, unless you’ve changed the
default_transaction_deferrable
configuration parameter.
§Example
conn.build_transaction()
.not_deferrable()
.run(|conn| Ok(()))
sourcepub fn read_committed(self) -> Self
pub fn read_committed(self) -> Self
Makes the transaction ISOLATION LEVEL READ COMMITTED
This is the default, unless you’ve changed the
default_transaction_isolation_level
configuration parameter.
§Example
conn.build_transaction()
.read_committed()
.run(|conn| Ok(()))
sourcepub fn repeatable_read(self) -> Self
pub fn repeatable_read(self) -> Self
Makes the transaction ISOLATION LEVEL REPEATABLE READ
§Example
conn.build_transaction()
.repeatable_read()
.run(|conn| Ok(()))
sourcepub fn serializable(self) -> Self
pub fn serializable(self) -> Self
Makes the transaction ISOLATION LEVEL SERIALIZABLE
§Example
conn.build_transaction()
.serializable()
.run(|conn| Ok(()))
sourcepub fn run<T, E, F>(&mut self, f: F) -> Result<T, E>
pub fn run<T, E, F>(&mut self, f: F) -> Result<T, E>
Runs the given function inside of the transaction with the parameters given to this builder.
This function executes the provided closure f
inside a database
transaction. If there is already an open transaction for the current
connection it will return an error. The connection is committed if
the closure returns Ok(_)
, it will be rolled back if it returns Err(_)
.
For both cases the original result value will be returned from this function.
If the transaction fails to commit and requires a rollback according to Postgres,
(e.g. serialization failure) a rollback will be attempted.
If the rollback fails, the error will be returned in a
Error::RollbackErrorOnCommit
,
from which you will be able to extract both the original commit error and
the rollback error.
In addition, the connection will be considered broken
as it contains a uncommitted unabortable open transaction. Any further
interaction with the transaction system will result in an returned error
in this case.
Trait Implementations§
source§impl<'a, C> QueryFragment<Pg> for TransactionBuilder<'a, C>
impl<'a, C> QueryFragment<Pg> for TransactionBuilder<'a, C>
source§fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Pg>) -> QueryResult<()>
fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, Pg>) -> QueryResult<()>
QueryFragment
for all passes. Read moresource§fn to_sql(&self, out: &mut DB::QueryBuilder, backend: &DB) -> QueryResult<()>
fn to_sql(&self, out: &mut DB::QueryBuilder, backend: &DB) -> QueryResult<()>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.QueryFragment
to its SQL representation. Read moresource§fn collect_binds<'b>(
&'b self,
out: &mut DB::BindCollector<'b>,
metadata_lookup: &mut DB::MetadataLookup,
backend: &'b DB,
) -> QueryResult<()>
fn collect_binds<'b>( &'b self, out: &mut DB::BindCollector<'b>, metadata_lookup: &mut DB::MetadataLookup, backend: &'b DB, ) -> QueryResult<()>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.source§fn is_safe_to_cache_prepared(&self, backend: &DB) -> QueryResult<bool>
fn is_safe_to_cache_prepared(&self, backend: &DB) -> QueryResult<bool>
i-implement-a-third-party-backend-and-opt-into-breaking-changes
only.Auto Trait Implementations§
impl<'a, C> Freeze for TransactionBuilder<'a, C>
impl<'a, C> RefUnwindSafe for TransactionBuilder<'a, C>where
C: RefUnwindSafe,
impl<'a, C> Send for TransactionBuilder<'a, C>where
C: Send,
impl<'a, C> Sync for TransactionBuilder<'a, C>where
C: Sync,
impl<'a, C> Unpin for TransactionBuilder<'a, C>
impl<'a, C> !UnwindSafe for TransactionBuilder<'a, C>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.