Unverified Commit 6555a467 authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3515 from morozov/persistent-connections

Implemented support for persistent connections in PDO and mysqli drivers
parents de7b33d0 c5d298c5
...@@ -56,6 +56,11 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar ...@@ -56,6 +56,11 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? ''; $dbname = $params['dbname'] ?? '';
$host = $params['host'];
if (! empty($params['persistent'])) {
$host = 'p:' . $host;
}
$flags = $driverOptions[static::OPTION_FLAGS] ?? 0; $flags = $driverOptions[static::OPTION_FLAGS] ?? 0;
...@@ -67,7 +72,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar ...@@ -67,7 +72,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
set_error_handler(static function () { set_error_handler(static function () {
}); });
try { 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); throw MysqliException::fromConnectionError($this->conn);
} }
} finally { } finally {
......
...@@ -8,6 +8,7 @@ use Doctrine\DBAL\DBALException; ...@@ -8,6 +8,7 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use PDO;
/** /**
* PDO MySql driver. * PDO MySql driver.
...@@ -19,6 +20,10 @@ class Driver extends AbstractMySQLDriver ...@@ -19,6 +20,10 @@ class Driver extends AbstractMySQLDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}
try { try {
$conn = new PDOConnection( $conn = new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
......
...@@ -8,6 +8,7 @@ use Doctrine\DBAL\DBALException; ...@@ -8,6 +8,7 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Driver\PDOException;
use PDO;
/** /**
* PDO Oracle driver. * PDO Oracle driver.
...@@ -24,6 +25,10 @@ class Driver extends AbstractOracleDriver ...@@ -24,6 +25,10 @@ class Driver extends AbstractOracleDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}
try { try {
return new PDOConnection( return new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
......
...@@ -21,6 +21,10 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -21,6 +21,10 @@ class Driver extends AbstractPostgreSQLDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}
try { try {
$connection = new PDOConnection( $connection = new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
......
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Driver\PDOSqlsrv; namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use PDO;
use function is_int; use function is_int;
use function sprintf; use function sprintf;
...@@ -28,6 +29,10 @@ class Driver extends AbstractSQLServerDriver ...@@ -28,6 +29,10 @@ class Driver extends AbstractSQLServerDriver
} }
} }
if (! empty($params['persistent'])) {
$pdoOptions[PDO::ATTR_PERSISTENT] = true;
}
return new Connection( return new Connection(
$this->_constructPdoDsn($params, $dsnOptions), $this->_constructPdoDsn($params, $dsnOptions),
$username, $username,
......
...@@ -7,9 +7,13 @@ namespace Doctrine\Tests\DBAL\Functional; ...@@ -7,9 +7,13 @@ namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\Tests\DbalFunctionalTestCase; use Doctrine\Tests\DbalFunctionalTestCase;
use Doctrine\Tests\TestUtil;
use Error; use Error;
use Exception; use Exception;
use PDO; use PDO;
...@@ -360,4 +364,28 @@ class ConnectionTest extends DbalFunctionalTestCase ...@@ -360,4 +364,28 @@ class ConnectionTest extends DbalFunctionalTestCase
])->ping() ])->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));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment