pub struct TransactionBuilder<'a, C> { /* private fields */ }
postgres
only.Expand description
Used to build a transaction, specifying additional details.
This struct is returned by AsyncPgConnection::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 read_only(self) -> Self
pub fn read_only(self) -> Self
Makes the transaction READ ONLY
§Example
use diesel_async::RunQueryDsl;
conn.build_transaction()
.read_only()
.run::<_, diesel::result::Error, _>(|conn| Box::pin(async move {
let read_attempt = users.select(name).load::<String>(conn).await;
assert!(read_attempt.is_ok());
let write_attempt = diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await;
assert!(write_attempt.is_err());
Ok(())
}) as _).await?;
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
use diesel_async::RunQueryDsl;
conn.build_transaction()
.read_write()
.run(|conn| Box::pin( async move {
let read_attempt = users.select(name).load::<String>(conn).await;
assert!(read_attempt.is_ok());
let write_attempt = diesel::insert_into(users)
.values(name.eq("Ruby"))
.execute(conn)
.await;
assert!(write_attempt.is_ok());
Ok(())
}) as _)
.await
Sourcepub fn deferrable(self) -> Self
pub fn deferrable(self) -> Self
Makes the transaction DEFERRABLE
§Example
conn.build_transaction()
.deferrable()
.run(|conn| Box::pin(async { Ok(()) }))
.await
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| Box::pin(async { Ok(()) }) as _)
.await
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| Box::pin(async { Ok(()) }) as _)
.await
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| Box::pin(async { Ok(()) }) as _)
.await
Sourcepub fn serializable(self) -> Self
pub fn serializable(self) -> Self
Makes the transaction ISOLATION LEVEL SERIALIZABLE
§Example
conn.build_transaction()
.serializable()
.run(|conn| Box::pin(async { Ok(()) }) as _)
.await
Sourcepub async fn run<'b, T, E, F>(&mut self, f: F) -> Result<T, E>
pub async fn run<'b, 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.
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
or a
ReadOnlyTransaction
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, 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 as Backend>::QueryBuilder,
backend: &DB,
) -> Result<(), Error>
fn to_sql( &self, out: &mut <DB as Backend>::QueryBuilder, backend: &DB, ) -> Result<(), Error>
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 as Backend>::BindCollector<'b>,
metadata_lookup: &mut <DB as TypeMetadata>::MetadataLookup,
backend: &'b DB,
) -> Result<(), Error>
fn collect_binds<'b>( &'b self, out: &mut <DB as Backend>::BindCollector<'b>, metadata_lookup: &mut <DB as TypeMetadata>::MetadataLookup, backend: &'b DB, ) -> Result<(), Error>
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self
to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self
to an expression for Diesel’s query builder. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T, DB> QueryFragmentForCachedStatement<DB> for T
impl<T, DB> QueryFragmentForCachedStatement<DB> for T
Source§impl<T, Conn> RunQueryDsl<Conn> for T
impl<T, Conn> RunQueryDsl<Conn> for T
Source§fn execute<'conn, 'query>(
self,
conn: &'conn mut Conn,
) -> Conn::ExecuteFuture<'conn, 'query>
fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> Conn::ExecuteFuture<'conn, 'query>
Source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
Source§fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnection,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
Source§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> GetResult<'conn, 'query, Self, Conn, U>
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> GetResult<'conn, 'query, Self, Conn, U>
Source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> LoadFuture<'conn, 'query, Self, Conn, U>
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> LoadFuture<'conn, 'query, Self, Conn, U>
Vec
with the affected rows. Read more