From c68b2e3a7b8308a9fe140e596944b1a20f763982 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 13 Apr 2019 23:50:35 -0700 Subject: [PATCH] Implemented support for persistent connections in PDO and mysqli drivers --- .../DBAL/Driver/Mysqli/MysqliConnection.php | 7 ++++- lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php | 5 ++++ lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php | 5 ++++ lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php | 4 +++ lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php | 5 ++++ .../Tests/DBAL/Functional/ConnectionTest.php | 28 +++++++++++++++++++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php index 02697c96013..37b29be6f5b 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php @@ -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; @@ -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 { diff --git a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php index ae2f2eff2f8..a3d8784f54e 100644 --- a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOException; +use PDO; /** * PDO MySql driver. @@ -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), diff --git a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php index 06e726df84d..5825606fcc0 100644 --- a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOException; +use PDO; /** * PDO Oracle driver. @@ -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), diff --git a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php index 0a40e37e032..f86c97331b4 100644 --- a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php @@ -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), diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php index 9ef35eb0243..b5091087dbb 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Driver\PDOSqlsrv; use Doctrine\DBAL\Driver\AbstractSQLServerDriver; +use PDO; use function is_int; use function sprintf; @@ -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, diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 0bd89184aef..a313539b816 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -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; @@ -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)); + } }