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

Implemented support for persistent connections in PDO and mysqli drivers #3515

Merged
merged 1 commit into from
Apr 23, 2019
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
7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function __construct(array $params, $username, $password, array $driverOp

$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? '';
$host = $params['host'];

if (! empty($params['persistent'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer array_key_exists() over more dangerous ! empty().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it's irrelevant. array_key_exists() over a mistyped key will have the same effect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of introducing a ConnectionParameters object which will be just a typed structure which will also contain username and password if they are specified. It could avoid optional $username and $pasword in driver/connection methods. Any suggestions/ideas?

$host = 'p:' . $host;
}

$flags = $driverOptions[static::OPTION_FLAGS] ?? 0;

Expand All @@ -67,7 +72,7 @@ public function __construct(array $params, $username, $password, array $driverOp
set_error_handler(static function () {
});
try {
if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
if (! $this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) {
throw MysqliException::fromConnectionError($this->conn);
}
} finally {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException;
use PDO;

/**
* PDO MySql driver.
Expand All @@ -19,6 +20,10 @@ class Driver extends AbstractMySQLDriver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

try {
$conn = new PDOConnection(
$this->constructPdoDsn($params),
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException;
use PDO;

/**
* PDO Oracle driver.
Expand All @@ -24,6 +25,10 @@ class Driver extends AbstractOracleDriver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

try {
return new PDOConnection(
$this->constructPdoDsn($params),
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Driver extends AbstractPostgreSQLDriver
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

try {
$connection = new PDOConnection(
$this->_constructPdoDsn($params),
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\DBAL\Driver\PDOSqlsrv;

use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use PDO;
use function is_int;
use function sprintf;

Expand All @@ -28,6 +29,10 @@ public function connect(array $params, $username = null, $password = null, array
}
}

if (! empty($params['persistent'])) {
$pdoOptions[PDO::ATTR_PERSISTENT] = true;
}

return new Connection(
$this->_constructPdoDsn($params, $dsnOptions),
$username,
Expand Down
29 changes: 29 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
use Doctrine\Tests\TestUtil;
use Error;
use Exception;
use PDO;
use RuntimeException;
use Throwable;
use function in_array;
Expand Down Expand Up @@ -313,4 +318,28 @@ public function testDeterminesDatabasePlatformWhenConnectingToNonExistentDatabas

$connection->close();
}

public function testPersistentConnection() : void
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof SqlitePlatform
|| $platform instanceof SQLServerPlatform) {
self::markTestSkipped('The platform does not support persistent connections');
}

$params = TestUtil::getConnectionParams();
$params['persistent'] = true;

$connection = DriverManager::getConnection($params);
$driverConnection = $connection->getWrappedConnection();

if (! $driverConnection instanceof PDOConnection) {
self::markTestSkipped('Unable to test if the connection is persistent');
}

$pdo = $driverConnection->getWrappedConnection();

self::assertTrue($pdo->getAttribute(PDO::ATTR_PERSISTENT));
}
}