stefano / stefano-nested-transaction
嵌套事务管理器
0.1.1
2022-09-30 19:53 UTC
Requires
- php: >=7.1.0
Requires (Dev)
- mockery/mockery: ^1.0.0
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: >=7 <10
This package is auto-updated.
Last update: 2024-08-29 03:58:00 UTC
README
使用Composer安装
- 运行命令
composer require stefano/stefano-nested-transaction
特性
- 管理嵌套事务
使用方法
- 配置
//$transactionAdapter implements \StefanoNestedTransaction\Adapter\TransactionInterface
$transactionAdapter = new YourTransactionAdapter();
$transactionManager = new \StefanoNestedTransaction\TransactionManager($transactionAdapter);
- 示例:正常流程
$transactionManager->begin(); //REAL start transaction
try {
// ...
//nested transaction block, that might be in some other code
$transactionManager->begin(); //increase internal transaction counter
try {
// ...
$transactionManager->commit(); //decrease internal transaction counter
} catch(\Exception $e) {
$transactionManager->rollback(); //skipped
throw $e->getPrevious();
}
// ...
$transactionManager->commit(); //REAL commit transaction;
} catch(\Exception $e) {
$transactionManager->rollback(); //skipped
throw $e->getPrevious();
}
- 示例:抛出异常
$transactionManager->begin(); //REAL start transaction
try {
// ...
//nested transaction block, that might be in some other code
$transactionManager->begin(); //increase internal transaction counter
try {
// ...
throw new \Exception();
$transactionManager->commit(); //skipped
} catch(\Exception $e) {
$transactionManager->rollback(); //marked as rollback only
throw $e->getPrevious();
}
// ...
$transactionManager->commit(); //skipped
} catch(\Exception $e) {
$transactionManager->rollback(); //REAL rollback
throw $e->getPrevious();
}
- 示例:抛出异常
$transactionManager->begin(); //REAL start transaction
try {
// ...
//nested transaction block, that might be in some other code
$transactionManager->begin(); //increase internal transaction counter
try {
// ...
throw new \Exception();
$transactionManager->commit(); //do nothing
} catch(\Exception $e) {
$transactionManager->rollback(); //marked as rollback only
}
// ...
$transactionManager->commit(); //this throw exception because transaction is marked as rollback only
} catch(\Exception $e) {
$transactionManager->rollback(); //REAL rollback
throw $e->getPrevious();
}