Skip to content

Commit 485763d

Browse files
ionutf6sXerkus
authored andcommitted
Use local variable instead of instance variable for storing select statement when in prepare mode
Signed-off-by: Ionut Codreanu <[email protected]>
1 parent 58fd2df commit 485763d

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/Adapter/Adapter.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,17 @@ public function query(
177177
}
178178

179179
if ($mode === self::QUERY_MODE_PREPARE) {
180-
$this->lastPreparedStatement = null;
181-
$this->lastPreparedStatement = $this->driver->createStatement($sql);
182-
$this->lastPreparedStatement->prepare();
180+
$lastPreparedStatement = $this->driver->createStatement($sql);
181+
$lastPreparedStatement->prepare();
183182
if (is_array($parameters) || $parameters instanceof ParameterContainer) {
184183
if (is_array($parameters)) {
185-
$this->lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
184+
$lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
186185
} else {
187-
$this->lastPreparedStatement->setParameterContainer($parameters);
186+
$lastPreparedStatement->setParameterContainer($parameters);
188187
}
189-
$result = $this->lastPreparedStatement->execute();
188+
$result = $lastPreparedStatement->execute();
190189
} else {
191-
return $this->lastPreparedStatement;
190+
return $lastPreparedStatement;
192191
}
193192
} else {
194193
$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)