Skip to content

Commit 9dd6143

Browse files
author
Ibrahim BinAlshikh
committed
fix(pool): don't throw when max connections exceeded, create untracked connection instead
The hard limit caused 'Connection pool exhausted' errors in environments where Database objects are held by long-lived references (e.g., PHPUnit test classes). Instead of throwing, the pool now creates a connection outside the pool when the limit is exceeded.
1 parent a309c57 commit 9dd6143

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

WebFiori/Database/ConnectionPool.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ public function acquire(ConnectionInfo $info): Connection {
8686
$conn->close();
8787
}
8888

89-
// Check total limit
89+
// Check total limit — if exceeded, create connection without pooling
9090
if ($this->getActiveCount() >= $this->maxTotal) {
91-
throw new DatabaseException(
92-
"Connection pool exhausted (max: {$this->maxTotal})"
93-
);
91+
return $this->createConnection($info);
9492
}
9593

9694
// Create new connection

tests/WebFiori/Tests/Database/Common/ConnectionPoolTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PHPUnit\Framework\TestCase;
66
use WebFiori\Database\ConnectionInfo;
77
use WebFiori\Database\ConnectionPool;
8-
use WebFiori\Database\DatabaseException;
98

109
/**
1110
* Test cases for ConnectionPool.
@@ -113,17 +112,19 @@ public function testCloseAllDrainsPool() {
113112
/**
114113
* @test
115114
*/
116-
public function testMaxTotalEnforced() {
115+
public function testMaxTotalExceededStillWorks() {
117116
$pool = ConnectionPool::getInstance();
118117
$pool->setMaxTotal(2);
119118
$info = $this->createMySQLConnectionInfo();
120119

121-
$pool->acquire($info);
122-
$pool->acquire($info);
120+
$conn1 = $pool->acquire($info);
121+
$conn2 = $pool->acquire($info);
122+
$conn3 = $pool->acquire($info);
123123

124-
$this->expectException(DatabaseException::class);
125-
$this->expectExceptionMessage('Connection pool exhausted');
126-
$pool->acquire($info);
124+
// All connections work, but only 2 are tracked as active
125+
$this->assertNotNull($conn3);
126+
$this->assertTrue($conn3->isAlive());
127+
$this->assertEquals(2, $pool->getActiveCount());
127128
}
128129

129130
/**

0 commit comments

Comments
 (0)