From 112e27157c05d196532159739202e3d9777434bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 15 Apr 2020 18:59:49 +0800 Subject: [PATCH] Reset transaction level to zero, when reconnent to mysql server. (#1565) --- CHANGELOG.md | 3 ++- src/db/src/AbstractConnection.php | 4 ++++ src/db/src/MySQLConnection.php | 2 +- src/db/src/PDOConnection.php | 1 + src/db/tests/Cases/PDODriverTest.php | 13 +++++++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac2ed1d06..38e785bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Fixed -- [#1563](https://github.com/hyperf/hyperf/pull/1563) Fixed crontab's `onOneServer` option not resetting mutex on shutdown +- [#1563](https://github.com/hyperf/hyperf/pull/1563) Fixed crontab's `onOneServer` option not resetting mutex on shutdown. +- [#1565](https://github.com/hyperf/hyperf/pull/1565) Reset transaction level to zero, when reconnent to mysql server. # v1.1.25 - 2020-04-09 diff --git a/src/db/src/AbstractConnection.php b/src/db/src/AbstractConnection.php index 19c09d9bd..c5b1e3be7 100644 --- a/src/db/src/AbstractConnection.php +++ b/src/db/src/AbstractConnection.php @@ -58,6 +58,10 @@ abstract class AbstractConnection extends Connection implements ConnectionInterf public function retry(\Throwable $throwable, $name, $arguments) { + if ($this->transactionLevel() > 0) { + throw $throwable; + } + if ($this->causedByLostConnection($throwable)) { try { $this->reconnect(); diff --git a/src/db/src/MySQLConnection.php b/src/db/src/MySQLConnection.php index 489107464..a9f72db0d 100644 --- a/src/db/src/MySQLConnection.php +++ b/src/db/src/MySQLConnection.php @@ -78,7 +78,7 @@ class MySQLConnection extends AbstractConnection $this->connection = $connection; $this->lastUseTime = microtime(true); - + $this->transactions = 0; return true; } diff --git a/src/db/src/PDOConnection.php b/src/db/src/PDOConnection.php index 629f9a7da..f28fb6062 100644 --- a/src/db/src/PDOConnection.php +++ b/src/db/src/PDOConnection.php @@ -93,6 +93,7 @@ class PDOConnection extends AbstractConnection $this->connection = $pdo; $this->lastUseTime = microtime(true); + $this->transactions = 0; return true; } diff --git a/src/db/tests/Cases/PDODriverTest.php b/src/db/tests/Cases/PDODriverTest.php index 95e899dd9..de77f5f68 100644 --- a/src/db/tests/Cases/PDODriverTest.php +++ b/src/db/tests/Cases/PDODriverTest.php @@ -115,4 +115,17 @@ class PDODriverTest extends AbstractTestCase $this->assertSame('Hyperf', $res['name']); } + + public function testTransactionLevelWhenReconnect() + { + $container = $this->getContainer(); + $factory = $container->get(PoolFactory::class); + $pool = $factory->getPool('default'); + $connection = $pool->get(); + $this->assertSame(0, $connection->transactionLevel()); + $connection->beginTransaction(); + $this->assertSame(1, $connection->transactionLevel()); + $connection->reconnect(); + $this->assertSame(0, $connection->transactionLevel()); + } }