pub struct Transaction { /* private fields */ }Expand description
事务对象,封装一个数据库事务
内部连接以 Option<Box<dyn Connection>> 形式持有:
- 事务执行期间,连接存在
- 调用
take_connection()可在 commit/rollback 后取回连接归还到连接池 - Drop 时若事务仍 Active,会尝试 spawn 后台 rollback 任务
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new(conn: Box<dyn Connection>, options: TransactOptions) -> Transaction
pub fn new(conn: Box<dyn Connection>, options: TransactOptions) -> Transaction
创建新事务(调用方应先通过 connection.begin_transaction() 启动事务)
L-5 修复:补充示例文档
通常通过 Connection::begin_transaction() 创建,而不是直接调用此方法。
§示例
ⓘ
use sz_orm_pool::transaction::{Transaction, TransactOptions};
// 通常通过 Connection::begin_transaction 创建
let tx = Transaction::new(conn, TransactOptions::default());
assert!(tx.is_active());Sourcepub fn state(&self) -> TransactionState
pub fn state(&self) -> TransactionState
获取当前事务状态
Sourcepub async fn query(
&mut self,
sql: &str,
) -> Result<Vec<HashMap<String, Value>>, TxError>
pub async fn query( &mut self, sql: &str, ) -> Result<Vec<HashMap<String, Value>>, TxError>
在事务中执行查询
Sourcepub async fn savepoint(&mut self) -> Result<String, TxError>
pub async fn savepoint(&mut self) -> Result<String, TxError>
创建保存点(用于嵌套事务)
返回自动生成的保存点名(格式 sp_<N>,N 单调递增)。
H-8 修复:检查嵌套深度,超过 options.max_nesting_depth 时返回
TxError::MaxNestingDepthExceeded。
L-5 修复:补充示例文档
§示例
ⓘ
// 在外层事务中创建保存点
let sp = tx.savepoint().await?;
// 执行一些操作
tx.execute("INSERT INTO orders (id) VALUES (1)").await?;
// 出错时回滚到保存点(不影响外层事务的其他操作)
tx.rollback_to_savepoint(&sp).await?;
// 不再需要保存点时释放
tx.release_savepoint(&sp).await?;Sourcepub async fn rollback_to_savepoint(&mut self, name: &str) -> Result<(), TxError>
pub async fn rollback_to_savepoint(&mut self, name: &str) -> Result<(), TxError>
回滚到保存点
name 必须是合法的保存点名称(仅 ASCII 字母/数字/下划线,且不以数字开头)。
通常使用 savepoint() 返回的名称。
Sourcepub async fn release_savepoint(&mut self, name: &str) -> Result<(), TxError>
pub async fn release_savepoint(&mut self, name: &str) -> Result<(), TxError>
释放保存点
name 必须是合法的保存点名称(仅 ASCII 字母/数字/下划线,且不以数字开头)。
通常使用 savepoint() 返回的名称。
Sourcepub async fn take_connection(&mut self) -> Result<Box<dyn Connection>, TxError>
pub async fn take_connection(&mut self) -> Result<Box<dyn Connection>, TxError>
取出底层连接(用于归还到连接池)
仅在事务已 commit/rollback 后才能调用,否则返回 NotActive 错误。
重复调用返回 ConnectionTaken 错误。
典型用法:
ⓘ
tx.commit().await?;
let conn = tx.take_connection().await?;
pool.release(conn).await;Sourcepub fn options(&self) -> &TransactOptions
pub fn options(&self) -> &TransactOptions
获取事务选项
Trait Implementations§
Source§impl Drop for Transaction
impl Drop for Transaction
Auto Trait Implementations§
impl !RefUnwindSafe for Transaction
impl !UnwindSafe for Transaction
impl Freeze for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl UnsafeUnpin for Transaction
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> 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