Skip to content

Commit

Permalink
Merge pull request doctrine#3515 from morozov/persistent-connections
Browse files Browse the repository at this point in the history
Implemented support for persistent connections in PDO and mysqli drivers
  • Loading branch information
morozov committed May 31, 2019
2 parents addae80 + 84d8ec3 commit 3ea729f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
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'])) {
$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
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
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;
Expand Down Expand Up @@ -326,4 +330,28 @@ public function testUserProvidedPDOConnection() : void
])->ping()
);
}

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));
}
}

0 comments on commit 3ea729f

Please sign in to comment.