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 3aa794c4a27..15ff50bdd10 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 f2d8dde4a95..43085c56095 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 2ea7dc2cda7..3b1b90345c4 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/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index e4470b07bb3..ac40617f669 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -3398,6 +3398,13 @@ public function getDummySelectSQL(string $expression = '1') : string return sprintf('SELECT %s', $expression); } + /** + * Returns the SQL to select the current connection identifier. + * + * @throws DBALException + */ + abstract public function getConnectionIdSQL() : string; + /** * Returns the SQL to create a new savepoint. * diff --git a/lib/Doctrine/DBAL/Platforms/DB2Platform.php b/lib/Doctrine/DBAL/Platforms/DB2Platform.php index b9fc54cf5f4..f95f0d4c1ba 100644 --- a/lib/Doctrine/DBAL/Platforms/DB2Platform.php +++ b/lib/Doctrine/DBAL/Platforms/DB2Platform.php @@ -878,6 +878,14 @@ public function getDummySelectSQL(string $expression = '1') : string return sprintf('SELECT %s FROM sysibm.sysdummy1', $expression); } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + throw DBALException::notSupported(__METHOD__); + } + /** * {@inheritDoc} * diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index d8fb568bf1b..85d00d6e1fe 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -1144,6 +1144,14 @@ public function getBlobTypeDeclarationSQL(array $field) return 'LONGBLOB'; } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + return 'SELECT CONNECTION_ID()'; + } + /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index 63f7442786c..e6853b091a6 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -1115,6 +1115,14 @@ public function getDummySelectSQL(string $expression = '1') : string return sprintf('SELECT %s FROM DUAL', $expression); } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + return "SELECT SYS_CONTEXT('USERENV', 'SID') FROM DUAL"; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 4fa61fbdeca..e0b00c02567 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -1044,6 +1044,14 @@ public function getClobTypeDeclarationSQL(array $field) return 'TEXT'; } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + return 'SELECT pg_backend_pid()'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php index 691d72ea908..305ebbad4d5 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php @@ -1510,6 +1510,14 @@ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName) return ['ALTER INDEX ' . $oldIndexName . ' ON ' . $tableName . ' RENAME TO ' . $index->getQuotedName($this)]; } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + throw DBALException::notSupported(__METHOD__); + } + /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php index 1c269628ac0..0f435076345 100644 --- a/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php @@ -1654,6 +1654,14 @@ public function getColumnDeclarationSQL($name, array $field) return $name . ' ' . $columnDef; } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + return 'SELECT @@SPID'; + } + /** * {@inheritdoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php index a8630569e6d..dac0b68f21e 100644 --- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php @@ -1002,6 +1002,14 @@ private function getSimpleAlterTableSQL(TableDiff $diff) return array_merge($sql, $tableSql, $columnSql); } + /** + * {@inheritdoc} + */ + public function getConnectionIdSQL() : string + { + throw DBALException::notSupported(__METHOD__); + } + /** * @return string[] */ diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 4ac0bd1b455..a7363e35a2a 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -6,10 +6,12 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\ConnectionException; +use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Tests\DbalFunctionalTestCase; +use Doctrine\Tests\TestUtil; use Error; use Exception; use RuntimeException; @@ -313,4 +315,38 @@ public function testDeterminesDatabasePlatformWhenConnectingToNonExistentDatabas $connection->close(); } + + public function testPersistentConnection() : void + { + $platform = $this->connection->getDatabasePlatform(); + + try { + $query = $platform->getConnectionIdSQL(); + } catch (DBALException $e) { + self::markTestSkipped($e->getMessage()); + } + + $params = TestUtil::getConnectionParams(); + $params['persistent'] = true; + + self::assertSame( + $this->getConnectionId($params, $query), + $this->getConnectionId($params, $query) + ); + } + + private function getConnectionId(array $params, $query) + { + $connection = DriverManager::getConnection($params); + + try { + $id = $connection->query($query)->fetchColumn(); + } finally { + $connection->close(); + } + + self::assertNotFalse($id); + + return $id; + } }