Skip to content

Commit 49dabe6

Browse files
committed
Fix incorrect handling of transactions when using deferred constraints
Let's get rid of There's no active transaction exception which occurs e.g. when using deferred constraints so the violation is checked at the end of the transaction and not during it.
1 parent 3943c20 commit 49dabe6

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/Connection.php

+40-10
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,17 @@ public function transactional(Closure $func): mixed
931931

932932
try {
933933
$res = $func($this);
934-
$this->commit();
935934

936935
$successful = true;
937-
938-
return $res;
939936
} finally {
940937
if (! $successful) {
941938
$this->rollBack();
942939
}
943940
}
941+
942+
$this->commit();
943+
944+
return $res;
944945
}
945946

946947
/**
@@ -1019,16 +1020,23 @@ public function commit(): void
10191020

10201021
$connection = $this->connect();
10211022

1022-
if ($this->transactionNestingLevel === 1) {
1023-
try {
1024-
$connection->commit();
1025-
} catch (Driver\Exception $e) {
1026-
throw $this->convertException($e);
1023+
try {
1024+
if ($this->transactionNestingLevel === 1) {
1025+
try {
1026+
$connection->commit();
1027+
} catch (Driver\Exception $e) {
1028+
throw $this->convertException($e);
1029+
}
1030+
} else {
1031+
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
10271032
}
1028-
} else {
1029-
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
1033+
} finally {
1034+
$this->updateTransactionStateAfterCommit();
10301035
}
1036+
}
10311037

1038+
private function updateTransactionStateAfterCommit(): void
1039+
{
10321040
--$this->transactionNestingLevel;
10331041

10341042
if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
@@ -1038,6 +1046,28 @@ public function commit(): void
10381046
$this->beginTransaction();
10391047
}
10401048

1049+
/**
1050+
* @return bool
1051+
*
1052+
* @throws DriverException
1053+
*/
1054+
private function doCommit(DriverConnection $connection)
1055+
{
1056+
$logger = $this->_config->getSQLLogger();
1057+
1058+
if ($logger !== null) {
1059+
$logger->startQuery('"COMMIT"');
1060+
}
1061+
1062+
$result = $connection->commit();
1063+
1064+
if ($logger !== null) {
1065+
$logger->stopQuery();
1066+
}
1067+
1068+
return $result;
1069+
}
1070+
10411071
/**
10421072
* Commits all current nesting transactions.
10431073
*

src/Driver/OCI8/Connection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function beginTransaction(): void
9595

9696
public function commit(): void
9797
{
98-
if (! oci_commit($this->connection)) {
98+
if (! @oci_commit($this->connection)) {
9999
throw Error::new($this->connection);
100100
}
101101

0 commit comments

Comments
 (0)