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>
impl<'tx> TransactionContext<'tx>
Sourcepub async fn begin(pool: &MySqlPool) -> Result<Self>
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?;Sourcepub async fn commit(self) -> Result<()>
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?;Sourcepub async fn rollback(self) -> Result<()>
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?;Sourcepub fn as_executor(&mut self) -> &mut MySqlConnection
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?;Sourcepub fn into_inner(self) -> Transaction<'tx, MySql>
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§
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> 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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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