From 4f41b3e6f9ad94bb85e039289fab4b641e79b54e Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 27 Aug 2024 20:15:02 +0700 Subject: [PATCH 01/13] Fix test of timeout `PDOException: Packets out of order` --- tests/ConnectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7b77919f..7942fd4e 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -132,7 +132,7 @@ public function testRestartConnectionOnTimeout(): void $db->createCommand('SET SESSION wait_timeout = 1')->execute(); - sleep(1); + sleep(2); $result = $db->createCommand("SELECT '1'")->queryScalar(); From 8c0671e4e964fc56e14bd9bb0f4e6e3d67fd8b46 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 28 Aug 2024 12:13:18 +0700 Subject: [PATCH 02/13] Fix test of timeout `PDOException: Packets out of order` --- .github/workflows/ansi-mode.yml | 2 +- .github/workflows/build-mariadb.yml | 2 +- .github/workflows/build.yml | 2 +- src/Command.php | 7 +++++-- tests/ConnectionTest.php | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ansi-mode.yml b/.github/workflows/ansi-mode.yml index d7d85bb8..a3d68bea 100644 --- a/.github/workflows/ansi-mode.yml +++ b/.github/workflows/ansi-mode.yml @@ -84,7 +84,7 @@ jobs: run: composer update --no-interaction --no-progress --optimize-autoloader --ansi - name: Run tests with phpunit with code coverage. - run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always + run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/build-mariadb.yml b/.github/workflows/build-mariadb.yml index ba9f4ad9..fedfc19d 100644 --- a/.github/workflows/build-mariadb.yml +++ b/.github/workflows/build-mariadb.yml @@ -90,7 +90,7 @@ jobs: run: composer update --no-interaction --no-progress --optimize-autoloader --ansi - name: Run tests with phpunit with code coverage. - run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always + run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d120c845..1e462f26 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -85,7 +85,7 @@ jobs: run: composer update --no-interaction --no-progress --optimize-autoloader --ansi - name: Run tests with phpunit with code coverage. - run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always + run: vendor/bin/phpunit --coverage-clover=coverage.xml --colors=always --display-warnings --display-deprecations - name: Upload coverage to Codecov. uses: codecov/codecov-action@v3 diff --git a/src/Command.php b/src/Command.php index 594c4bb5..d3ecc1a4 100644 --- a/src/Command.php +++ b/src/Command.php @@ -48,9 +48,12 @@ protected function queryInternal(int $queryMode): mixed try { return parent::queryInternal($queryMode); } catch (IntegrityException $e) { - if (str_starts_with($e->getMessage(), 'SQLSTATE[HY000]: General error: 2006 ')) { - $this->db->close(); + if ( + str_starts_with($e->getMessage(), 'SQLSTATE[HY000]: General error: 2006 ') + && $this->db->getTransaction() === null + ) { $this->cancel(); + $this->db->close(); return parent::queryInternal($queryMode); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7942fd4e..7b77919f 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -132,7 +132,7 @@ public function testRestartConnectionOnTimeout(): void $db->createCommand('SET SESSION wait_timeout = 1')->execute(); - sleep(2); + sleep(1); $result = $db->createCommand("SELECT '1'")->queryScalar(); From fe9ddf9a9553cfc63b97df13969ee730a30c3b82 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 28 Aug 2024 13:13:43 +0700 Subject: [PATCH 03/13] Fix test of timeout `Packets out of order` --- src/Connection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index efe3219b..380ba8f4 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -4,8 +4,8 @@ namespace Yiisoft\Db\Mysql; -use Exception; use Psr\Log\LogLevel; +use Throwable; use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; @@ -30,8 +30,8 @@ public function close(): void // Solution for close connections {@link https://stackoverflow.com/questions/18277233/pdo-closing-connection} try { - $this->pdo->query('KILL CONNECTION_ID()'); - } catch (Exception) { + $this->pdo->exec('KILL CONNECTION_ID()'); + } catch (Throwable) { } $this->pdo = null; From 6ab58b4b5ec8e19bba85f99bdda8fbc314968db0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 28 Aug 2024 13:56:33 +0700 Subject: [PATCH 04/13] Do not restart connection in transaction --- tests/ConnectionTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7b77919f..eae42842 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -8,6 +8,7 @@ use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; +use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; @@ -140,4 +141,21 @@ public function testRestartConnectionOnTimeout(): void $db->close(); } + + public function testNotRestartConnectionOnTimeoutInTransaction(): void + { + $db = $this->getConnection(); + $db->beginTransaction(); + + $db->getPDO()->errorCode(); + + $db->createCommand('SET SESSION wait_timeout = 1')->execute(); + + sleep(1); + + $this->expectException(IntegrityException::class); + $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); + + $db->createCommand("SELECT '1'")->queryScalar(); + } } From 4cdac671e7042b551113bb25e337b7da76249703 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 28 Aug 2024 13:57:05 +0700 Subject: [PATCH 05/13] Do not restart connection in transaction --- tests/ConnectionTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index eae42842..56f4ecfc 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -147,8 +147,6 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $db = $this->getConnection(); $db->beginTransaction(); - $db->getPDO()->errorCode(); - $db->createCommand('SET SESSION wait_timeout = 1')->execute(); sleep(1); From c502b3ebf29297cc1009f3f411204634fd78964b Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 29 Aug 2024 11:29:03 +0700 Subject: [PATCH 06/13] Update --- tests/ConnectionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 56f4ecfc..3a41c2d6 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -133,7 +133,7 @@ public function testRestartConnectionOnTimeout(): void $db->createCommand('SET SESSION wait_timeout = 1')->execute(); - sleep(1); + sleep(2); $result = $db->createCommand("SELECT '1'")->queryScalar(); @@ -149,7 +149,7 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $db->createCommand('SET SESSION wait_timeout = 1')->execute(); - sleep(1); + sleep(2); $this->expectException(IntegrityException::class); $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); From b0cb24bb0d929686fa1fbf014dfc8f8cb0ffb1a1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 29 Aug 2024 11:50:37 +0700 Subject: [PATCH 07/13] Change error level for the tests --- tests/ConnectionTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 3a41c2d6..c5b56823 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -135,8 +135,12 @@ public function testRestartConnectionOnTimeout(): void sleep(2); + $previousErrorLevel = error_reporting(E_ALL & ~E_WARNING); + $result = $db->createCommand("SELECT '1'")->queryScalar(); + error_reporting($previousErrorLevel); + $this->assertSame('1', $result); $db->close(); @@ -154,6 +158,12 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $this->expectException(IntegrityException::class); $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); - $db->createCommand("SELECT '1'")->queryScalar(); + $previousErrorLevel = error_reporting(E_ALL & ~E_WARNING); + + try { + $db->createCommand("SELECT '1'")->queryScalar(); + } finally { + error_reporting($previousErrorLevel); + } } } From 89db03420f21467d0c708c744b0b53ec182c2bf0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 29 Aug 2024 12:51:46 +0700 Subject: [PATCH 08/13] `WithoutErrorHandler` on the tests --- tests/ConnectionTest.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index c5b56823..2d80348a 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Mysql\Tests; use PDO; +use PHPUnit\Framework\Attributes\WithoutErrorHandler; use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; @@ -127,6 +128,7 @@ static function (ConnectionInterface $db) { } /** @link https://github.com/yiisoft/db-mysql/issues/348 */ + #[WithoutErrorHandler] public function testRestartConnectionOnTimeout(): void { $db = $this->getConnection(); @@ -135,17 +137,14 @@ public function testRestartConnectionOnTimeout(): void sleep(2); - $previousErrorLevel = error_reporting(E_ALL & ~E_WARNING); - $result = $db->createCommand("SELECT '1'")->queryScalar(); - error_reporting($previousErrorLevel); - $this->assertSame('1', $result); $db->close(); } + #[WithoutErrorHandler] public function testNotRestartConnectionOnTimeoutInTransaction(): void { $db = $this->getConnection(); @@ -158,12 +157,6 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $this->expectException(IntegrityException::class); $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); - $previousErrorLevel = error_reporting(E_ALL & ~E_WARNING); - - try { - $db->createCommand("SELECT '1'")->queryScalar(); - } finally { - error_reporting($previousErrorLevel); - } + $db->createCommand("SELECT '1'")->queryScalar(); } } From 5d5acac7b3f7b664ce6a0a88eebe14047bb7723f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 29 Aug 2024 14:09:59 +0700 Subject: [PATCH 09/13] Remove `WithoutErrorHandler` --- tests/ConnectionTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 2d80348a..42d4a6a1 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -128,7 +128,6 @@ static function (ConnectionInterface $db) { } /** @link https://github.com/yiisoft/db-mysql/issues/348 */ - #[WithoutErrorHandler] public function testRestartConnectionOnTimeout(): void { $db = $this->getConnection(); @@ -144,7 +143,6 @@ public function testRestartConnectionOnTimeout(): void $db->close(); } - #[WithoutErrorHandler] public function testNotRestartConnectionOnTimeoutInTransaction(): void { $db = $this->getConnection(); From 27ec18ca10d6edc0fbb4b06ea6f2dd2e340e6ade Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 29 Aug 2024 07:10:15 +0000 Subject: [PATCH 10/13] Apply fixes from StyleCI --- tests/ConnectionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 42d4a6a1..3a41c2d6 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -5,7 +5,6 @@ namespace Yiisoft\Db\Mysql\Tests; use PDO; -use PHPUnit\Framework\Attributes\WithoutErrorHandler; use Throwable; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; From 597b4ae33f60a1fc47a8ab99278cd94118008369 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 29 Aug 2024 15:07:48 +0700 Subject: [PATCH 11/13] Update CHANGELOG.md --- CHANGELOG.md | 2 +- tests/ConnectionTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae81fad..e9a49782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - Enh #342: Add JSON overlaps condition builder (@Tigrov) - Enh #344: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov) - Enh #347: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) -- Bug #349: Restore connection if closed by connection timeout (@Tigrov) +- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 3a41c2d6..06d5ee0b 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -135,7 +135,7 @@ public function testRestartConnectionOnTimeout(): void sleep(2); - $result = $db->createCommand("SELECT '1'")->queryScalar(); + $result = $db->createCommand("SELECT 1")->queryScalar(); $this->assertSame('1', $result); @@ -154,6 +154,6 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $this->expectException(IntegrityException::class); $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); - $db->createCommand("SELECT '1'")->queryScalar(); + $db->createCommand("SELECT 1")->queryScalar(); } } From 01176c48c1ffe7336147dd670808eb9a4b28b94b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 29 Aug 2024 08:08:06 +0000 Subject: [PATCH 12/13] Apply fixes from StyleCI --- tests/ConnectionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 06d5ee0b..e8baa4ac 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -135,7 +135,7 @@ public function testRestartConnectionOnTimeout(): void sleep(2); - $result = $db->createCommand("SELECT 1")->queryScalar(); + $result = $db->createCommand('SELECT 1')->queryScalar(); $this->assertSame('1', $result); @@ -154,6 +154,6 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $this->expectException(IntegrityException::class); $this->expectExceptionMessage('SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'); - $db->createCommand("SELECT 1")->queryScalar(); + $db->createCommand('SELECT 1')->queryScalar(); } } From 532ac286001c899207c770af2be0a36bae9aaabb Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 30 Aug 2024 20:31:15 +0700 Subject: [PATCH 13/13] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52090766..ef93dfc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,8 @@ - Chg #339: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov) - Enh #342: Add JSON overlaps condition builder (@Tigrov) - Enh #344: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov) -- Enh #347: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) -- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov) - Enh #347, #353: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) -- Bug #349: Restore connection if closed by connection timeout (@Tigrov) +- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov) ## 1.2.0 March 21, 2024