Skip to content

feat(database): 实现数据库事务管理器- 新增 DatabaseTransactionsManager 类用于管理数据库事务#7544

Open
woweijun123 wants to merge 1 commit intohyperf:3.1from
woweijun123:3.1-weichengjun
Open

feat(database): 实现数据库事务管理器- 新增 DatabaseTransactionsManager 类用于管理数据库事务#7544
woweijun123 wants to merge 1 commit intohyperf:3.1from
woweijun123:3.1-weichengjun

Conversation

@woweijun123
Copy link
Copy Markdown

  • 新增 DatabaseTransactionRecord 类用于记录事务信息
  • 在 Connection 类中添加事务管理器的设置方法
  • 在 db-connection 中自动注入事务管理器
  • 实现事务开始、提交、回滚时的回调机制- 支持事务提交后的回调执行
  • 支持事务回滚时的回调执行
  • 优化事务层级管理逻辑 -修复事务丢失连接时的处理逻辑

- 新增 DatabaseTransactionRecord 类用于记录事务信息
- 在 Connection 类中添加事务管理器的设置方法
- 在 db-connection 中自动注入事务管理器
- 实现事务开始、提交、回滚时的回调机制- 支持事务提交后的回调执行
- 支持事务回滚时的回调执行
- 优化事务层级管理逻辑
-修复事务丢失连接时的处理逻辑
@huangdijia huangdijia requested a review from Copilot September 23, 2025 11:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter type should be declared as DatabaseTransactionsManager instead of using a generic type. This improves type safety and makes the API more explicit.

Copilot uses AI. Check for mistakes.
&& $this->transactions > 1) {
--$this->transactions;

$this->transactionsManager?->rollback($this->getName(), $this->transactions);
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
$this->transactionsManager?->rollback($this->getName(), $this->transactions);
$this->transactionsManager->rollback($this->getName(), $this->transactions);

Copilot uses AI. Check for mistakes.
if ($this->causedByLostConnection($e)) {
$this->transactions = 0;

$this->transactionsManager->rollback($this->getName(), $this->transactions);
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
$this->transactionsManager->rollback($this->getName(), $this->transactions);
$this->transactionsManager?->rollback($this->getName(), $this->transactions);

Copilot uses AI. Check for mistakes.
&& $transaction->level > $newTransactionLevel
)->values();

if ($this->currentTransaction) {
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if ($this->currentTransaction) {
if (isset($this->currentTransaction[$connection])) {

Copilot uses AI. Check for mistakes.
*/
protected function removeAllTransactionsForConnection(string $connection): void
{
if ($this->currentTransaction) {
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as line 143 - the condition should check if the specific connection exists in the currentTransaction array instead of checking the entire array.

Suggested change
if ($this->currentTransaction) {
if (isset($this->currentTransaction[$connection])) {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants