Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with connection not released in specific scenario #292

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
}
} 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, []);
}
}