Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function getConnection(): \PDO
public function closeConnection(): void
{
$this->connection = null;
$this->statementPool->clear();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/ClosedConnectionException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/*
* This file is part of the Access package.
*
* (c) Tim <me@justim.net>
*
* 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;
Expand Down
13 changes: 12 additions & 1 deletion src/StatementPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Access;

use Access\Database;
use Countable;

/**
* Query statement pool
Expand All @@ -22,7 +23,7 @@
*
* @author Tim <me@justim.net>
*/
final class StatementPool
final class StatementPool implements Countable
{
/**
* All open prepared statements
Expand Down Expand Up @@ -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);
}
}
11 changes: 8 additions & 3 deletions tests/base/BaseDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}