Skip to content

Commit a9414de

Browse files
author
Ibrahim BinAlshikh
committed
fix(pool): each acquire creates its own connection, reuse only after release
Sharing a single active connection across multiple Database instances caused transaction/state leaks between them. The pool now creates a separate connection per acquire() call (same as before pooling), but reuses idle connections after release(). The maxTotal limit is advisory — no exception is thrown when exceeded.
1 parent 9dd6143 commit a9414de

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

WebFiori/Database/ConnectionPool.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function getInstance(): self {
6868
*
6969
* @return Connection A ready-to-use database connection.
7070
*
71-
* @throws DatabaseException If the pool is exhausted or connection fails.
71+
* @throws DatabaseException If connection creation fails.
7272
*/
7373
public function acquire(ConnectionInfo $info): Connection {
7474
$key = $this->buildKey($info);
@@ -86,12 +86,7 @@ public function acquire(ConnectionInfo $info): Connection {
8686
$conn->close();
8787
}
8888

89-
// Check total limit — if exceeded, create connection without pooling
90-
if ($this->getActiveCount() >= $this->maxTotal) {
91-
return $this->createConnection($info);
92-
}
93-
94-
// Create new connection
89+
// Create new connection (no hard limit — pool is advisory)
9590
$conn = $this->createConnection($info);
9691
$this->active[$key][] = $conn;
9792
return $conn;

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testAcquireReusesIdleConnection() {
8181
/**
8282
* @test
8383
*/
84-
public function testMultipleAcquiresCreateMultipleConnections() {
84+
public function testMultipleAcquiresCreateSeparateConnections() {
8585
$pool = ConnectionPool::getInstance();
8686
$info = $this->createMySQLConnectionInfo();
8787

@@ -112,7 +112,7 @@ public function testCloseAllDrainsPool() {
112112
/**
113113
* @test
114114
*/
115-
public function testMaxTotalExceededStillWorks() {
115+
public function testMaxTotalIsAdvisoryNotHardLimit() {
116116
$pool = ConnectionPool::getInstance();
117117
$pool->setMaxTotal(2);
118118
$info = $this->createMySQLConnectionInfo();
@@ -121,10 +121,10 @@ public function testMaxTotalExceededStillWorks() {
121121
$conn2 = $pool->acquire($info);
122122
$conn3 = $pool->acquire($info);
123123

124-
// All connections work, but only 2 are tracked as active
124+
// All connections created — no exception thrown
125125
$this->assertNotNull($conn3);
126126
$this->assertTrue($conn3->isAlive());
127-
$this->assertEquals(2, $pool->getActiveCount());
127+
$this->assertEquals(3, $pool->getActiveCount());
128128
}
129129

130130
/**
@@ -163,18 +163,16 @@ public function testReset() {
163163
/**
164164
* @test
165165
*/
166-
public function testDifferentConnectionInfoUsesDifferentKeys() {
166+
public function testAcquireAfterReleaseReusesSameConnection() {
167167
$pool = ConnectionPool::getInstance();
168-
$info1 = $this->createMySQLConnectionInfo();
168+
$info = $this->createMySQLConnectionInfo();
169169

170-
$conn1 = $pool->acquire($info1);
170+
$conn1 = $pool->acquire($info);
171171
$pool->release($conn1);
172172

173-
// Same params should reuse
174-
$info2 = $this->createMySQLConnectionInfo();
175-
$conn2 = $pool->acquire($info2);
173+
// Same params after release should reuse
174+
$conn2 = $pool->acquire($info);
176175
$this->assertSame($conn1, $conn2);
177-
178176
$this->assertEquals(1, $pool->getActiveCount());
179177
}
180178

0 commit comments

Comments
 (0)