From 9f2086afa0462983879386ab1c4bb5e759b502df Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Oct 2025 17:04:36 +0200 Subject: [PATCH 1/2] Clear StatementPool when connection is closed --- src/Database.php | 1 + src/StatementPool.php | 13 ++++++++++++- tests/base/BaseDatabaseTest.php | 11 ++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Database.php b/src/Database.php index f49b260..868945a 100644 --- a/src/Database.php +++ b/src/Database.php @@ -147,6 +147,7 @@ public function getConnection(): \PDO public function closeConnection(): void { $this->connection = null; + $this->statementPool->clear(); } /** diff --git a/src/StatementPool.php b/src/StatementPool.php index 3c3eeaa..9841108 100644 --- a/src/StatementPool.php +++ b/src/StatementPool.php @@ -14,6 +14,7 @@ namespace Access; use Access\Database; +use Countable; /** * Query statement pool @@ -22,7 +23,7 @@ * * @author Tim */ -final class StatementPool +final class StatementPool implements Countable { /** * All open prepared statements @@ -72,4 +73,14 @@ public function clear(): void { $this->stmtPool = []; } + + /** + * Get number of prepared statements + * + * @return int + */ + public function count(): int + { + return count($this->stmtPool); + } } diff --git a/tests/base/BaseDatabaseTest.php b/tests/base/BaseDatabaseTest.php index db8f09b..310a97e 100644 --- a/tests/base/BaseDatabaseTest.php +++ b/tests/base/BaseDatabaseTest.php @@ -331,13 +331,18 @@ public function testCloseConnection(): void { $db = static::createDatabaseWithDummyData(); + $db->getStatementPool()->clear(); + $db->getStatementPool()->prepare('SELECT 1'); + $this->assertCount(1, $db->getStatementPool()); + $connection = $db->getConnection(); - self::assertInstanceOf(PDO::class, $connection); + $this->assertInstanceOf(PDO::class, $connection); $db->closeConnection(); + $this->assertEmpty($db->getStatementPool()); - self::expectException(ClosedConnectionException::class); - self::expectExceptionMessage('Connection is closed'); + $this->expectException(ClosedConnectionException::class); + $this->expectExceptionMessage('Connection is closed'); $db->getConnection(); } } From 68bc961a2691d33e4f0784051b64034e9ab93339 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Oct 2025 17:08:03 +0200 Subject: [PATCH 2/2] Add copyright info --- src/Exception/ClosedConnectionException.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Exception/ClosedConnectionException.php b/src/Exception/ClosedConnectionException.php index c716303..59c047a 100644 --- a/src/Exception/ClosedConnectionException.php +++ b/src/Exception/ClosedConnectionException.php @@ -1,5 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace Access\Exception; use Access\Exception;