Skip to content

Commit

Permalink
Merge pull request #292 from ionutf6s/database_connection_not_released
Browse files Browse the repository at this point in the history
Fix issue with connection not released in specific scenario
  • Loading branch information
Xerkus committed Feb 1, 2024
2 parents 7e1bbe4 + e725a60 commit 5d8c89d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
/** @var ResultSet\ResultSetInterface */
protected $queryResultSetPrototype;

/** @var Driver\StatementInterface */
/**
* @deprecated
*
* @var Driver\StatementInterface
*/
protected $lastPreparedStatement;

/**
* @param Driver\DriverInterface|array $driver
* @throws Exception\InvalidArgumentException
Expand Down Expand Up @@ -177,18 +180,17 @@ public function query(
}

if ($mode === self::QUERY_MODE_PREPARE) {
$this->lastPreparedStatement = null;
$this->lastPreparedStatement = $this->driver->createStatement($sql);
$this->lastPreparedStatement->prepare();
$lastPreparedStatement = $this->driver->createStatement($sql);
$lastPreparedStatement->prepare();
if (is_array($parameters) || $parameters instanceof ParameterContainer) {
if (is_array($parameters)) {
$this->lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
$lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
} else {
$this->lastPreparedStatement->setParameterContainer($parameters);
$lastPreparedStatement->setParameterContainer($parameters);
}
$result = $this->lastPreparedStatement->execute();
$result = $lastPreparedStatement->execute();
} else {
return $this->lastPreparedStatement;
return $lastPreparedStatement;
}
} else {
$result = $this->driver->getConnection()->execute($sql);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql;

use Laminas\Db\TableGateway\TableGateway;
use PHPUnit\Framework\TestCase;

use function array_fill;

/**
* Usually mysql has 151 max connections by default.
* Set up a test where executed Laminas\Db\Adapter\Adapter::query and then using table gateway to fetch a row
* On tear down disconnected from the database and set the driver adapter on null
* Running many tests ended up in consuming all mysql connections and not releasing them
*/
class TableGatewayAndAdapterTest extends TestCase
{
use AdapterTrait;

/**
* @dataProvider connections
*/
public function testGetOutOfConnections(): void
{
$this->adapter->query('SELECT VERSION();');
$table = new TableGateway(
'test',
$this->adapter
);
$select = $table->getSql()->select()->where(['name' => 'foo']);
$result = $table->selectWith($select);
self::assertCount(3, $result->current());
}

protected function tearDown(): void
{
if ($this->adapter->getDriver()->getConnection()->isConnected()) {
$this->adapter->getDriver()->getConnection()->disconnect();
}
$this->adapter = null;
}

public function connections(): array
{
return array_fill(0, 200, []);
}
}

0 comments on commit 5d8c89d

Please sign in to comment.