TransactionContext

Struct TransactionContext 

Source
pub struct TransactionContext<'tx> { /* private fields */ }
Expand description

Transaction context wrapper providing type-safe transaction boundaries.

This struct wraps SQLx’s Transaction and provides automatic rollback on drop if commit() is not explicitly called.

§Safety

If this struct is dropped without calling commit(), the transaction will be automatically rolled back. This prevents accidental commits when errors occur.

§Examples

use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let mut tx = TransactionContext::begin(&pool).await?;

// Perform database operations using tx.as_executor()
// sqlx::query("INSERT INTO ...").execute(tx.as_executor()).await?;

// Explicitly commit the transaction
tx.commit().await?;

Implementations§

Source§

impl<'tx> TransactionContext<'tx>

Source

pub async fn begin(pool: &MySqlPool) -> Result<Self>

Begins a new transaction from the connection pool.

§Errors

Returns an error if the database connection fails or transaction cannot be started.

§Examples
use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let mut tx = TransactionContext::begin(&pool).await?;
// Use the transaction...
tx.commit().await?;
Source

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

Commits the transaction.

After calling this method, the TransactionContext is consumed and cannot be used.

§Errors

Returns an error if the commit operation fails.

§Examples
use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let mut tx = TransactionContext::begin(&pool).await?;
// ... perform operations
tx.commit().await?;
Source

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

Explicitly rolls back the transaction.

Normally, rollback happens automatically when the TransactionContext is dropped without calling commit(). This method allows explicit rollback for error handling.

§Errors

Returns an error if the rollback operation fails.

§Examples
use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let mut tx = TransactionContext::begin(&pool).await?;
// ... if something goes wrong
tx.rollback().await?;
Source

pub fn as_executor(&mut self) -> &mut MySqlConnection

Returns a mutable reference to the underlying connection for use as an Executor.

This method provides access to &mut MySqlConnection, which implements SQLx’s Executor trait. Use this when calling SQLx query methods or other libraries that accept an executor.

§Panics

Panics if the transaction has already been consumed (committed or rolled back).

§Examples
use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let mut tx = TransactionContext::begin(&pool).await?;

sqlx::query("INSERT INTO users (name) VALUES (?)")
    .bind("Alice")
    .execute(tx.as_executor())
    .await?;

tx.commit().await?;
Source

pub fn into_inner(self) -> Transaction<'tx, MySql>

Consumes the context and returns the underlying SQLx Transaction.

This is useful when you need direct access to SQLx’s transaction API. After calling this method, the TransactionContext cannot be used.

§Panics

Panics if the transaction has already been consumed.

§Examples
use sqlx::MySqlPool;
use sqlx_transaction_manager::TransactionContext;

let tx_ctx = TransactionContext::begin(&pool).await?;
let tx = tx_ctx.into_inner();
// Use raw SQLx transaction...
tx.commit().await?;

Trait Implementations§

Source§

impl<'tx> Drop for TransactionContext<'tx>

Source§

fn drop(&mut self)

Automatically rolls back the transaction if not committed.

This ensures that uncommitted transactions are always rolled back, preventing accidental commits when errors occur or when the transaction context goes out of scope.

Auto Trait Implementations§

§

impl<'tx> Freeze for TransactionContext<'tx>

§

impl<'tx> !RefUnwindSafe for TransactionContext<'tx>

§

impl<'tx> Send for TransactionContext<'tx>

§

impl<'tx> Sync for TransactionContext<'tx>

§

impl<'tx> Unpin for TransactionContext<'tx>

§

impl<'tx> !UnwindSafe for TransactionContext<'tx>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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