Struct axum_sqlx_tx::Layer
source · pub struct Layer<DB: Database, E = Error> { /* private fields */ }
Expand description
A tower_layer::Layer
that enables the Tx
extractor.
This layer adds a lazily-initialised transaction to the request extensions. The first time the
Tx
extractor is used on a request, a connection is acquired from the configured
[sqlx::Pool
] and a transaction is started on it. The same transaction will be returned for
subsequent uses of Tx
on the same request. The inner service is then called as normal. Once
the inner service responds, the transaction is committed or rolled back depending on the status
code of the response.
Implementations§
source§impl<DB: Database> Layer<DB>
impl<DB: Database> Layer<DB>
sourcepub fn new(pool: Pool<DB>) -> Self
pub fn new(pool: Pool<DB>) -> Self
Construct a new layer with the given pool
.
A connection will be obtained from the pool the first time a Tx
is extracted
from a request.
If you want to access the pool outside of a transaction, you should add it also with
axum::Extension
.
To use a different type than Error
to convert commit errors into responses, see
new_with_error
.
sourcepub fn new_with_error<E>(pool: Pool<DB>) -> Layer<DB, E>
pub fn new_with_error<E>(pool: Pool<DB>) -> Layer<DB, E>
Construct a new layer with a specific error type.
See Layer::new
for more information.