Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable establishing exclusive oci8 connections #6182

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ pdo_oci / oci8
parameters will no longer be used. Note that when using this parameter, the ``getHost``
and ``getPort`` methods from ``Doctrine\DBAL\Connection`` will no longer function as expected.
- ``persistent`` (boolean): Whether to establish a persistent connection.
- ``driverOptions`` (array):
- ``exclusive`` (boolean): Once specified for an ``oci8`` connection, forces the driver to always establish
a new connection instead of reusing an existing one from the connection pool.

pdo_sqlsrv / sqlsrv
^^^^^^^^^^^^^^^^^^^
Expand Down
12 changes: 11 additions & 1 deletion src/Driver/OCI8/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\OCI8\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\OCI8\Exception\InvalidConfiguration;
use SensitiveParameter;

use function oci_connect;
Expand Down Expand Up @@ -32,8 +33,17 @@ public function connect(

$connectionString = $this->getEasyConnectString($params);

if (! empty($params['persistent'])) {
$persistent = ! empty($params['persistent']);
$exclusive = ! empty($params['driverOptions']['exclusive']);
derrabus marked this conversation as resolved.
Show resolved Hide resolved

if ($persistent && $exclusive) {
throw InvalidConfiguration::forPersistentAndExclusive();
}

if ($persistent) {
$connection = @oci_pconnect($username, $password, $connectionString, $charset, $sessionMode);
} elseif ($exclusive) {
$connection = @oci_new_connect($username, $password, $connectionString, $charset, $sessionMode);
} else {
$connection = @oci_connect($username, $password, $connectionString, $charset, $sessionMode);
}
Expand Down
20 changes: 20 additions & 0 deletions src/Driver/OCI8/Exception/InvalidConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\OCI8\Exception;

use Doctrine\DBAL\Driver\AbstractException;

/**
* @internal
*
* @psalm-immutable
*/
final class InvalidConfiguration extends AbstractException
{
public static function forPersistentAndExclusive(): self
{
return new self('The "persistent" parameter and the "exclusive" driver option are mutually exclusive');
}
}
12 changes: 12 additions & 0 deletions tests/Driver/OCI8/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\OCI8\Driver;
use Doctrine\DBAL\Driver\OCI8\Exception\InvalidConfiguration;
use Doctrine\DBAL\Tests\Driver\AbstractOracleDriverTestCase;

/** @requires extension oci8 */
class DriverTest extends AbstractOracleDriverTestCase
{
public function testPersistentAndExclusiveAreMutuallyExclusive(): void
{
$this->expectException(InvalidConfiguration::class);

(new Driver())->connect([
'persistent' => true,
'driverOptions' => ['exclusive' => true],
]);
}

protected function createDriver(): DriverInterface
{
return new Driver();
Expand Down
14 changes: 8 additions & 6 deletions tests/Functional/LockMode/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\DBAL\Tests\Functional\LockMode;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\SqlitePlatform;
Expand All @@ -21,11 +22,6 @@ class NoneTest extends FunctionalTestCase

public function setUp(): void
{
if (TestUtil::isDriverOneOf('oci8')) {
// https://github.com/doctrine/dbal/issues/4417
self::markTestSkipped('This test fails on OCI8 for a currently unknown reason');
}

if ($this->connection->getDatabasePlatform() instanceof SQLServerPlatform) {
// Use row versioning instead of locking on SQL Server (if we don't, the second connection will block when
// attempting to read the row created by the first connection, instead of reading the previous version);
Expand All @@ -43,7 +39,13 @@ public function setUp(): void

$this->dropAndCreateTable($table);

$this->connection2 = TestUtil::getConnection();
$params = TestUtil::getConnectionParams();

if (TestUtil::isDriverOneOf('oci8')) {
$params['driverOptions']['exclusive'] = true;
}

$this->connection2 = DriverManager::getConnection($params);

if ($this->connection2->getSchemaManager()->tablesExist('users')) {
return;
Expand Down