pub struct TransactionConn { /* private fields */ }Expand description
事务连接持有者
在 TransactionMiddleware 中创建,注入到 request extensions 供 handler
复用同一连接执行查询。handler 通过 web::ReqData<TransactionConn> 提取。
连接包装在 Arc<Mutex<Option<PooledConnection>>> 中:
Some(conn):事务进行中,handler 可获取连接执行查询None:事务已结束(中间件取回连接执行 commit/rollback)
§用法
ⓘ
use actix_web::web::ReqData;
use sz_orm_actix::TransactionConn;
async fn handler(tx: ReqData<TransactionConn>) -> impl Responder {
if let Some(mut guard) = tx.conn().await {
if let Some(conn) = guard.as_mut() {
conn.execute("INSERT INTO users (name) VALUES ('Alice')").await?;
}
}
HttpResponse::Ok()
}Implementations§
Source§impl TransactionConn
impl TransactionConn
Sourcepub async fn conn(&self) -> Option<MutexGuard<'_, Option<PooledConnection>>>
pub async fn conn(&self) -> Option<MutexGuard<'_, Option<PooledConnection>>>
获取连接的可变引用
返回 MutexGuard<Option<PooledConnection>>,调用方通过 guard.as_mut()
获取 &mut Option<PooledConnection>,再 .as_mut().unwrap() 得到
&mut PooledConnection。
如果中间件已取回连接(事务结束),返回 None。
Trait Implementations§
Source§impl Clone for TransactionConn
impl Clone for TransactionConn
Source§impl FromRequest for TransactionConn
impl FromRequest for TransactionConn
Source§type Future = Ready<Result<TransactionConn, <TransactionConn as FromRequest>::Error>>
type Future = Ready<Result<TransactionConn, <TransactionConn as FromRequest>::Error>>
Future that resolves to a
Self. Read moreSource§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a
Self from request parts asynchronously.Auto Trait Implementations§
impl !RefUnwindSafe for TransactionConn
impl !UnwindSafe for TransactionConn
impl Freeze for TransactionConn
impl Send for TransactionConn
impl Sync for TransactionConn
impl Unpin for TransactionConn
impl UnsafeUnpin for TransactionConn
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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