Skip to main content

AsyncTransaction

Struct AsyncTransaction 

Source
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>

Source

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.

Source

pub async fn rollback(self) -> Result<()>

Rolls back the transaction explicitly.

§Errors

Forwards the error from AsyncConnection::rollback. The transaction is marked completed regardless.

Source

pub fn connection(&self) -> &AsyncConnection

Returns a reference to the underlying async connection.

Source

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.

Source

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.

Source

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.

Source

pub async fn fetch_optional<Q: AsRef<str>>( &self, query: Q, ) -> Result<Option<Row>>

Fetches a single row or None.

§Errors

Forwards the error from AsyncConnection::fetch_optional.

Source

pub async fn fetch_all<Q: AsRef<str>>(&self, query: Q) -> Result<Vec<Row>>

Fetches all rows from the query.

§Errors

Forwards the error from AsyncConnection::fetch_all.

Source

pub async fn fetch_scalar<T, Q>(&self, query: Q) -> Result<T>
where T: RowValue, Q: AsRef<str>,

Fetches a single non-NULL scalar; errors if empty or NULL.

§Errors

Forwards the error from AsyncConnection::fetch_scalar.

Source

pub async fn fetch_optional_scalar<T, Q>(&self, query: Q) -> Result<Option<T>>
where T: RowValue, Q: AsRef<str>,

Fetches a single scalar, allowing NULL as None.

§Errors

Forwards the error from AsyncConnection::fetch_optional_scalar.

Source

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.

Source

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.

Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for AsyncTransaction<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more