[DBAL-3515] Implemented support for persistent connections in PDO and mysqli drivers

parent 87edf7ad
......@@ -57,6 +57,11 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? null;
$host = $params['host'];
if (! empty($params['persistent'])) {
$host = 'p:' . $host;
}
$flags = $driverOptions[static::OPTION_FLAGS] ?? null;
......@@ -68,7 +73,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
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 new MysqliException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno);
}
} finally {
......
......@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDOConnection;
use PDO;
use PDOException;
/**
......@@ -17,6 +18,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),
......
......@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Driver\PDOOracle;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\PDOConnection;
use PDO;
use PDOException;
/**
......@@ -22,6 +23,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),
......
......@@ -19,6 +19,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),
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use PDO;
use function is_int;
use function sprintf;
......@@ -26,6 +27,10 @@ class Driver extends AbstractSQLServerDriver
}
}
if (! empty($params['persistent'])) {
$pdoOptions[PDO::ATTR_PERSISTENT] = true;
}
return new Connection(
$this->_constructPdoDsn($params, $dsnOptions),
$username,
......
......@@ -5,11 +5,15 @@ namespace Doctrine\Tests\DBAL\Functional;
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\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\DbalFunctionalTestCase;
use Doctrine\Tests\TestUtil;
use Error;
use Exception;
use PDO;
......@@ -364,4 +368,28 @@ class ConnectionTest extends DbalFunctionalTestCase
])->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