Skip to content

Commit 5d8c89d

Browse files
authored
Merge pull request laminas#292 from ionutf6s/database_connection_not_released
Fix issue with connection not released in specific scenario
2 parents 7e1bbe4 + e725a60 commit 5d8c89d

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/Adapter/Adapter.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
4949
/** @var ResultSet\ResultSetInterface */
5050
protected $queryResultSetPrototype;
5151

52-
/** @var Driver\StatementInterface */
52+
/**
53+
* @deprecated
54+
*
55+
* @var Driver\StatementInterface
56+
*/
5357
protected $lastPreparedStatement;
54-
5558
/**
5659
* @param Driver\DriverInterface|array $driver
5760
* @throws Exception\InvalidArgumentException
@@ -177,18 +180,17 @@ public function query(
177180
}
178181

179182
if ($mode === self::QUERY_MODE_PREPARE) {
180-
$this->lastPreparedStatement = null;
181-
$this->lastPreparedStatement = $this->driver->createStatement($sql);
182-
$this->lastPreparedStatement->prepare();
183+
$lastPreparedStatement = $this->driver->createStatement($sql);
184+
$lastPreparedStatement->prepare();
183185
if (is_array($parameters) || $parameters instanceof ParameterContainer) {
184186
if (is_array($parameters)) {
185-
$this->lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
187+
$lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
186188
} else {
187-
$this->lastPreparedStatement->setParameterContainer($parameters);
189+
$lastPreparedStatement->setParameterContainer($parameters);
188190
}
189-
$result = $this->lastPreparedStatement->execute();
191+
$result = $lastPreparedStatement->execute();
190192
} else {
191-
return $this->lastPreparedStatement;
193+
return $lastPreparedStatement;
192194
}
193195
} else {
194196
$result = $this->driver->getConnection()->execute($sql);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql;
4+
5+
use Laminas\Db\TableGateway\TableGateway;
6+
use PHPUnit\Framework\TestCase;
7+
8+
use function array_fill;
9+
10+
/**
11+
* Usually mysql has 151 max connections by default.
12+
* Set up a test where executed Laminas\Db\Adapter\Adapter::query and then using table gateway to fetch a row
13+
* On tear down disconnected from the database and set the driver adapter on null
14+
* Running many tests ended up in consuming all mysql connections and not releasing them
15+
*/
16+
class TableGatewayAndAdapterTest extends TestCase
17+
{
18+
use AdapterTrait;
19+
20+
/**
21+
* @dataProvider connections
22+
*/
23+
public function testGetOutOfConnections(): void
24+
{
25+
$this->adapter->query('SELECT VERSION();');
26+
$table = new TableGateway(
27+
'test',
28+
$this->adapter
29+
);
30+
$select = $table->getSql()->select()->where(['name' => 'foo']);
31+
$result = $table->selectWith($select);
32+
self::assertCount(3, $result->current());
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
if ($this->adapter->getDriver()->getConnection()->isConnected()) {
38+
$this->adapter->getDriver()->getConnection()->disconnect();
39+
}
40+
$this->adapter = null;
41+
}
42+
43+
public function connections(): array
44+
{
45+
return array_fill(0, 200, []);
46+
}
47+
}

0 commit comments

Comments
 (0)