feat(database): 实现数据库事务管理器- 新增 DatabaseTransactionsManager 类用于管理数据库事务#7544
feat(database): 实现数据库事务管理器- 新增 DatabaseTransactionsManager 类用于管理数据库事务#7544woweijun123 wants to merge 1 commit intohyperf:3.1from
Conversation
woweijun123
commented
Sep 23, 2025
- 新增 DatabaseTransactionRecord 类用于记录事务信息
- 在 Connection 类中添加事务管理器的设置方法
- 在 db-connection 中自动注入事务管理器
- 实现事务开始、提交、回滚时的回调机制- 支持事务提交后的回调执行
- 支持事务回滚时的回调执行
- 优化事务层级管理逻辑 -修复事务丢失连接时的处理逻辑
- 新增 DatabaseTransactionRecord 类用于记录事务信息 - 在 Connection 类中添加事务管理器的设置方法 - 在 db-connection 中自动注入事务管理器 - 实现事务开始、提交、回滚时的回调机制- 支持事务提交后的回调执行 - 支持事务回滚时的回调执行 - 优化事务层级管理逻辑 -修复事务丢失连接时的处理逻辑
There was a problem hiding this comment.
Pull Request Overview
Implements a comprehensive database transaction management system with callback support for transaction lifecycle events. The PR introduces a centralized transaction manager that tracks transaction states and handles commit/rollback callbacks across nested transactions.
Key changes:
- Adds DatabaseTransactionsManager and DatabaseTransactionRecord classes for transaction state management
- Integrates transaction manager with database connections and the connection pool
- Implements callback mechanisms for post-commit and rollback scenarios
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/db-connection/src/Connection.php | Automatically injects transaction manager into database connections |
| src/database/src/DatabaseTransactionsManager.php | Core transaction manager with nested transaction support and callback handling |
| src/database/src/DatabaseTransactionRecord.php | Transaction record class for storing transaction state and callbacks |
| src/database/src/Connection.php | Adds transaction manager setter method |
| src/database/src/Concerns/ManagesTransactions.php | Integrates transaction manager calls into existing transaction methods |
| src/contract/src/ShouldDispatchAfterCommit.php | Marker interface for post-commit dispatch behavior |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| $this->events = null; | ||
| } | ||
|
|
||
| public function setTransactionManager($manager) |
There was a problem hiding this comment.
The parameter type should be declared as DatabaseTransactionsManager instead of using a generic type. This improves type safety and makes the API more explicit.
| && $this->transactions > 1) { | ||
| --$this->transactions; | ||
|
|
||
| $this->transactionsManager?->rollback($this->getName(), $this->transactions); |
There was a problem hiding this comment.
The null-safe operator is used inconsistently. Other calls to transactionsManager in this file don't use the null-safe operator, which could cause fatal errors if the transaction manager is not set.
| $this->transactionsManager?->rollback($this->getName(), $this->transactions); | |
| $this->transactionsManager->rollback($this->getName(), $this->transactions); |
| if ($this->causedByLostConnection($e)) { | ||
| $this->transactions = 0; | ||
|
|
||
| $this->transactionsManager->rollback($this->getName(), $this->transactions); |
There was a problem hiding this comment.
Missing null-safe operator. This call could cause a fatal error if transactionsManager is not set, while line 152 uses the null-safe operator for the same scenario.
| $this->transactionsManager->rollback($this->getName(), $this->transactions); | |
| $this->transactionsManager?->rollback($this->getName(), $this->transactions); |
| && $transaction->level > $newTransactionLevel | ||
| )->values(); | ||
|
|
||
| if ($this->currentTransaction) { |
There was a problem hiding this comment.
The condition checks if the entire currentTransaction array is truthy, but it should check if the specific connection exists in the array. This could cause issues when multiple connections are used.
| if ($this->currentTransaction) { | |
| if (isset($this->currentTransaction[$connection])) { |
| */ | ||
| protected function removeAllTransactionsForConnection(string $connection): void | ||
| { | ||
| if ($this->currentTransaction) { |
There was a problem hiding this comment.
Same issue as line 143 - the condition should check if the specific connection exists in the currentTransaction array instead of checking the entire array.
| if ($this->currentTransaction) { | |
| if (isset($this->currentTransaction[$connection])) { |