Skip to content

Removing transactionIsolationLevel & operations with savepoint from Schema #62

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

Merged
merged 4 commits into from
Apr 18, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 4 additions & 164 deletions src/PDO/TransactionPDOOracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,172 +4,12 @@

namespace Yiisoft\Db\Oracle\PDO;

use Psr\Log\LogLevel;
use Throwable;
use Yiisoft\Db\AwareTrait\LoggerAwareTrait;
use Yiisoft\Db\Connection\ConnectionPDOInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Transaction\TransactionInterface;
use Yiisoft\Db\Transaction\TransactionPDO;

final class TransactionPDOOracle implements TransactionInterface
final class TransactionPDOOracle extends TransactionPDO
{
use LoggerAwareTrait;

/**
* A constant representing the transaction isolation level `READ UNCOMMITTED`.
*
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels}
*/
public const READ_UNCOMMITTED = 'READ UNCOMMITTED';

/**
* A constant representing the transaction isolation level `READ COMMITTED`.
*
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels}
*/
public const READ_COMMITTED = 'READ COMMITTED';

/**
* A constant representing the transaction isolation level `REPEATABLE READ`.
*
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels}
*/
public const REPEATABLE_READ = 'REPEATABLE READ';

/**
* A constant representing the transaction isolation level `SERIALIZABLE`.
*
* {@see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels}
*/
public const SERIALIZABLE = 'SERIALIZABLE';

private int $level = 0;

public function __construct(private ConnectionPDOInterface $db)
{
}

public function begin(?string $isolationLevel = null): void
{
$this->db->open();

if ($this->level === 0) {
if ($isolationLevel !== null) {
$this->db->getSchema()->setTransactionIsolationLevel($isolationLevel);
}

$this->logger?->log(
LogLevel::DEBUG,
'Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : '')
. ' ' . __METHOD__
);
$this->db->getPDO()?->beginTransaction();
$this->level = 1;
return;
}

$schema = $this->db->getSchema();

if ($schema->supportsSavepoint()) {
$this->logger?->log(LogLevel::DEBUG, 'Set savepoint ' . $this->level . ' ' . __METHOD__);
$schema->createSavepoint('LEVEL' . $this->level);
} else {
$this->logger?->log(
LogLevel::DEBUG,
'Transaction not started: nested transaction not supported ' . __METHOD__
);
throw new NotSupportedException('Transaction not started: nested transaction not supported.');
}

$this->level++;
}

/**
* Commits a transaction.
*
* @throws Exception|Throwable if the transaction is not active
*/
public function commit(): void
{
if (!$this->isActive()) {
throw new Exception('Failed to commit transaction: transaction was inactive.');
}

$this->level--;

if ($this->level === 0) {
$this->logger?->log(LogLevel::DEBUG, 'Commit transaction ' . __METHOD__);
$this->db->getPDO()?->commit();
return;
}

$schema = $this->db->getSchema();

if ($schema->supportsSavepoint()) {
$this->logger?->log(LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__);
$schema->releaseSavepoint('LEVEL' . $this->level);
} else {
$this->logger?->log(
LogLevel::INFO,
'Transaction not committed: nested transaction not supported ' . __METHOD__
);
}
}

public function getLevel(): int
{
return $this->level;
}

public function isActive(): bool
public function releaseSavepoint(string $name): void
{
return $this->level > 0 && $this->db->isActive();
}

/**
* @throws Exception|InvalidConfigException|Throwable
*/
public function rollBack(): void
{
if (!$this->isActive()) {
/**
* do nothing if transaction is not active: this could be the transaction is committed but the event handler
* to "commitTransaction" throw an exception
*/
return;
}

$this->level--;
if ($this->level === 0) {
$this->logger?->log(LogLevel::INFO, 'Roll back transaction ' . __METHOD__);
$this->db->getPDO()?->rollBack();
return;
}

$schema = $this->db->getSchema();
if ($schema->supportsSavepoint()) {
$this->logger?->log(LogLevel::DEBUG, 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__);
$schema->rollBackSavepoint('LEVEL' . $this->level);
} else {
$this->logger?->log(
LogLevel::INFO,
'Transaction not rolled back: nested transaction not supported ' . __METHOD__
);
}
}

public function setIsolationLevel(string $level): void
{
if (!$this->isActive()) {
throw new Exception('Failed to set isolation level: transaction was inactive.');
}

$this->logger?->log(
LogLevel::DEBUG,
'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__
);
$this->db->getSchema()->setTransactionIsolationLevel($level);
// does nothing as Oracle does not support this
}
}
27 changes: 0 additions & 27 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,6 @@ protected function loadTableDefaultValues(string $tableName): array
throw new NotSupportedException('Oracle does not support default value constraints.');
}

public function releaseSavepoint(string $name): void
{
/* does nothing as Oracle does not support this */
}

/**
* Create a column schema builder instance giving the type and value precision.
*
Expand Down Expand Up @@ -821,16 +816,6 @@ protected function createColumnSchema(): ColumnSchema
return new ColumnSchema();
}

public function rollBackSavepoint(string $name): void
{
$this->db->createCommand("ROLLBACK TO SAVEPOINT $name")->execute();
}

public function setTransactionIsolationLevel(string $level): void
{
$this->db->createCommand("SET TRANSACTION ISOLATION LEVEL $level")->execute();
}

/**
* Returns the actual name of a given table name.
*
Expand Down Expand Up @@ -902,16 +887,4 @@ public function supportsSavepoint(): bool
{
return $this->db->isSavepointEnabled();
}

/**
* Creates a new savepoint.
*
* @param string $name the savepoint name
*
* @throws Exception|InvalidConfigException|Throwable
*/
public function createSavepoint(string $name): void
{
$this->db->createCommand("SAVEPOINT $name")->execute();
}
}
8 changes: 4 additions & 4 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Oracle\PDO\TransactionPDOOracle;
use Yiisoft\Db\TestSupport\TestConnectionTrait;
use Yiisoft\Db\Transaction\TransactionInterface;

/**
* @group oracle
Expand Down Expand Up @@ -112,12 +112,12 @@ public function testTransactionIsolation()
{
$db = $this->getConnection();

$transaction = $db->beginTransaction(TransactionPDOOracle::READ_COMMITTED);
$transaction = $db->beginTransaction(TransactionInterface::READ_COMMITTED);
$transaction->commit();
/* should not be any exception so far */
$this->assertTrue(true);

$transaction = $db->beginTransaction(TransactionPDOOracle::SERIALIZABLE);
$transaction = $db->beginTransaction(TransactionInterface::SERIALIZABLE);
$transaction->commit();
/* should not be any exception so far */
$this->assertTrue(true);
Expand All @@ -129,7 +129,7 @@ public function testTransactionShortcutCustom()
$result = $db->transaction(static function (ConnectionInterface $db) {
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute();
return true;
}, TransactionPDOOracle::READ_COMMITTED);
}, TransactionInterface::READ_COMMITTED);
$this->assertTrue($result, 'transaction shortcut valid value should be returned from callback');

$profilesCount = $db->createCommand(
Expand Down