pub struct AsyncTransaction<'conn> { /* private fields */ }Expand description
An async RAII transaction guard.
Created via AsyncConnection::transaction(). This exclusively borrows the
connection for the lifetime of the transaction, preventing any other code from
using the raw connection while the transaction is active. Always explicitly call
commit() or rollback() — the Drop
implementation cannot issue async rollback and will only emit a warning.
§Example
let txn = conn.transaction().await?;
txn.execute_command("INSERT INTO users VALUES (1, 'Alice')").await?;
txn.commit().await?;Implementations§
Source§impl<'conn> AsyncTransaction<'conn>
impl<'conn> AsyncTransaction<'conn>
Sourcepub async fn commit(self) -> Result<()>
pub async fn commit(self) -> Result<()>
Commits the transaction.
§Errors
Forwards the error from AsyncConnection::commit. The transaction
is marked completed regardless, so the drop guard will not warn.
Sourcepub async fn rollback(self) -> Result<()>
pub async fn rollback(self) -> Result<()>
Rolls back the transaction explicitly.
§Errors
Forwards the error from AsyncConnection::rollback. The
transaction is marked completed regardless.
Sourcepub fn connection(&self) -> &AsyncConnection
pub fn connection(&self) -> &AsyncConnection
Returns a reference to the underlying async connection.
Sourcepub async fn execute_command(&self, sql: &str) -> Result<u64>
pub async fn execute_command(&self, sql: &str) -> Result<u64>
Executes a SQL command within this transaction.
§Errors
Forwards the error from AsyncConnection::execute_command.
Sourcepub async fn execute_query(&self, query: &str) -> Result<AsyncRowset<'_>>
pub async fn execute_query(&self, query: &str) -> Result<AsyncRowset<'_>>
Executes a streaming query within this transaction.
§Errors
Forwards the error from AsyncConnection::execute_query.
Sourcepub async fn fetch_one<Q: AsRef<str>>(&self, query: Q) -> Result<Row>
pub async fn fetch_one<Q: AsRef<str>>(&self, query: Q) -> Result<Row>
Fetches a single row, erroring if zero rows are returned.
§Errors
Forwards the error from AsyncConnection::fetch_one.
Sourcepub async fn fetch_scalar<T, Q>(&self, query: Q) -> Result<T>
pub async fn fetch_scalar<T, Q>(&self, query: Q) -> Result<T>
Fetches a single non-NULL scalar; errors if empty or NULL.
§Errors
Forwards the error from AsyncConnection::fetch_scalar.
Sourcepub async fn fetch_optional_scalar<T, Q>(&self, query: Q) -> Result<Option<T>>
pub async fn fetch_optional_scalar<T, Q>(&self, query: Q) -> Result<Option<T>>
Fetches a single scalar, allowing NULL as None.
§Errors
Forwards the error from AsyncConnection::fetch_optional_scalar.
Sourcepub async fn query_count(&self, query: &str) -> Result<i64>
pub async fn query_count(&self, query: &str) -> Result<i64>
Returns a count from a SELECT COUNT(*) style query.
§Errors
Forwards the error from AsyncConnection::query_count.
Sourcepub async fn query_params(
&self,
query: &str,
params: &[&dyn ToSqlParam],
) -> Result<AsyncRowset<'_>>
pub async fn query_params( &self, query: &str, params: &[&dyn ToSqlParam], ) -> Result<AsyncRowset<'_>>
Executes a parameterized query within this transaction.
§Errors
Forwards the error from AsyncConnection::query_params.
Sourcepub async fn command_params(
&self,
query: &str,
params: &[&dyn ToSqlParam],
) -> Result<u64>
pub async fn command_params( &self, query: &str, params: &[&dyn ToSqlParam], ) -> Result<u64>
Executes a parameterized command within this transaction.
§Errors
Forwards the error from AsyncConnection::command_params.
Trait Implementations§
Source§impl<'conn> Debug for AsyncTransaction<'conn>
impl<'conn> Debug for AsyncTransaction<'conn>
Source§impl Drop for AsyncTransaction<'_>
impl Drop for AsyncTransaction<'_>
Auto Trait Implementations§
impl<'conn> Freeze for AsyncTransaction<'conn>
impl<'conn> !RefUnwindSafe for AsyncTransaction<'conn>
impl<'conn> Send for AsyncTransaction<'conn>
impl<'conn> Sync for AsyncTransaction<'conn>
impl<'conn> Unpin for AsyncTransaction<'conn>
impl<'conn> UnsafeUnpin for AsyncTransaction<'conn>
impl<'conn> !UnwindSafe for AsyncTransaction<'conn>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request