pub struct TransactionBuilder<'a> { /* private fields */ }
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> TransactionBuilder<'a>
impl<'a> TransactionBuilder<'a>
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, _>(|| {
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(|| {
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
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(|| 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(|| 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(|| Ok(()))
Sourcepub fn serializable(self) -> Self
pub fn serializable(self) -> Self
Makes the transaction ISOLATION LEVEL SERIALIZABLE
§Example
conn.build_transaction()
.serializable()
.run(|| Ok(()))
Sourcepub fn run<T, E, F>(&self, f: F) -> Result<T, E>
pub fn run<T, E, F>(&self, f: F) -> Result<T, E>
Runs the given function inside of the transaction with the parameters given to this builder.
Returns an error if the connection is already inside a transaction, or if the transaction fails to commit or rollback
If the transaction fails to commit due to a SerializationFailure
, a rollback will be attempted.
If the rollback succeeds, the original error will be returned, otherwise the error generated by
the rollback will be returned. In the second case the connection should be considered broken
as it contains a uncommitted unabortable open transaction.
Trait Implementations§
Source§impl<'a> Clone for TransactionBuilder<'a>
impl<'a> Clone for TransactionBuilder<'a>
Source§fn clone(&self) -> TransactionBuilder<'a>
fn clone(&self) -> TransactionBuilder<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> QueryFragment<Pg> for TransactionBuilder<'a>
impl<'a> QueryFragment<Pg> for TransactionBuilder<'a>
Source§fn walk_ast(&self, out: AstPass<'_, Pg>) -> QueryResult<()>
fn walk_ast(&self, out: AstPass<'_, Pg>) -> QueryResult<()>
QueryFragment
for all passes. Read moreSource§fn to_sql(&self, out: &mut DB::QueryBuilder) -> QueryResult<()>
fn to_sql(&self, out: &mut DB::QueryBuilder) -> QueryResult<()>
QueryFragment
to its SQL representation. Read more