Skip to main content

sqlx_otel/
transaction.rs

1use crate::database::Database;
2use crate::pool::SharedState;
3
4/// An in-progress database transaction instrumented for OpenTelemetry.
5///
6/// Wraps a `sqlx::Transaction` and propagates shared attributes and metric instruments.
7/// Pass `&mut transaction` directly to `SQLx` query builders – the `Executor` impl on
8/// `&mut Transaction` provides full span and metric instrumentation.
9#[derive(Debug)]
10pub struct Transaction<'c, DB: sqlx::Database> {
11    pub(crate) inner: sqlx::Transaction<'c, DB>,
12    pub(crate) state: SharedState,
13}
14
15impl<DB> Transaction<'_, DB>
16where
17    DB: Database,
18{
19    /// Commit the transaction.
20    ///
21    /// # Errors
22    ///
23    /// Returns `sqlx::Error` if the commit fails.
24    pub async fn commit(self) -> Result<(), sqlx::Error> {
25        self.inner.commit().await
26    }
27
28    /// Roll back the transaction.
29    ///
30    /// # Errors
31    ///
32    /// Returns `sqlx::Error` if the rollback fails.
33    pub async fn rollback(self) -> Result<(), sqlx::Error> {
34        self.inner.rollback().await
35    }
36
37    impl_with_annotations_mut!();
38}