Skip to content

Commit 4a25d53

Browse files
fix: clear lost transaction state before reconnecting
reconnect() cleared transDepth and transStatus only after initialize() returned. When initialize() threw, the old transaction state survived, and a later query() that reconnected on its own let transComplete() report success. The state is now cleared before initialize() runs. OCI8 keeps its commit mode in PHP. Clearing the depth skipped _transRollback(), so commitMode stayed OCI_NO_AUTO_COMMIT and later writes were never committed. OCI8 now restores OCI_COMMIT_ON_SUCCESS when the transaction is abandoned. The guide and changelog now say this applies only when ping() reports the connection lost. On PostgreSQL, pg_ping() re-establishes a broken connection itself and returns true, so reconnect() does not clear the state there. Refs #10648 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b35b9ab commit 4a25d53

5 files changed

Lines changed: 111 additions & 7 deletions

File tree

‎system/Database/BaseConnection.php‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ public function close()
627627
*
628628
* A replaced socket is not in the transaction that was open on the old
629629
* one. The server has already rolled that work back. Clear the depth and
630-
* mark the transaction failed so transComplete() cannot report success.
630+
* mark the transaction failed before opening the new connection, so a
631+
* failed reconnect cannot still report success. OCI8 also returns its
632+
* commit mode to autocommit.
631633
*
632634
* @return void
633635
*/
@@ -637,15 +639,30 @@ public function reconnect()
637639
$lostTransaction = $this->transDepth > 0;
638640

639641
$this->close();
640-
$this->initialize();
641642

643+
// initialize() can throw. Clear the lost transaction first so a
644+
// failed reconnect cannot still report the old transaction.
642645
if ($lostTransaction) {
643-
$this->transDepth = 0;
644-
$this->transStatus = false;
646+
$this->abandonLostTransaction();
645647
}
648+
649+
$this->initialize();
646650
}
647651
}
648652

653+
/**
654+
* The socket that owned the transaction is already closed.
655+
*
656+
* Drivers that keep transaction state in PHP, rather than only on the
657+
* server, must override this. OCI8 commit mode is one case: leaving it
658+
* at OCI_NO_AUTO_COMMIT makes later writes uncommitted.
659+
*/
660+
protected function abandonLostTransaction(): void
661+
{
662+
$this->transDepth = 0;
663+
$this->transStatus = false;
664+
}
665+
649666
/**
650667
* Platform dependent way method for closing the connection.
651668
*

‎system/Database/OCI8/Connection.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,13 @@ protected function _transRollback(): bool
722722
return oci_rollback($this->connID);
723723
}
724724

725+
protected function abandonLostTransaction(): void
726+
{
727+
$this->commitMode = OCI_COMMIT_ON_SUCCESS;
728+
729+
parent::abandonLostTransaction();
730+
}
731+
725732
/**
726733
* Returns the name of the current database being used.
727734
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database;
15+
16+
use CodeIgniter\Database\Exceptions\DatabaseException;
17+
use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection;
18+
use CodeIgniter\Database\OCI8\Connection as OCI8Connection;
19+
use CodeIgniter\Test\CIUnitTestCase;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('Others')]
26+
final class ReconnectLostTransactionTest extends CIUnitTestCase
27+
{
28+
public function testReconnectClearsTransactionStateWhenInitializeFails(): void
29+
{
30+
$db = new class (['DBDriver' => 'MySQLi']) extends MySQLiConnection {
31+
public function initialize(): never
32+
{
33+
throw new DatabaseException('database unavailable');
34+
}
35+
};
36+
37+
$this->setPrivateProperty($db, 'transDepth', 1);
38+
39+
try {
40+
$db->reconnect();
41+
$this->fail('initialize() should have thrown.');
42+
} catch (DatabaseException $exception) {
43+
$this->assertSame('database unavailable', $exception->getMessage());
44+
}
45+
46+
$this->assertSame(0, $this->getPrivateProperty($db, 'transDepth'));
47+
$this->assertFalse($db->transStatus());
48+
}
49+
50+
public function testReconnectRestoresOci8CommitModeWhenTransactionIsLost(): void
51+
{
52+
if (! defined('OCI_COMMIT_ON_SUCCESS')) {
53+
define('OCI_COMMIT_ON_SUCCESS', 32);
54+
}
55+
56+
if (! defined('OCI_NO_AUTO_COMMIT')) {
57+
define('OCI_NO_AUTO_COMMIT', 0);
58+
}
59+
60+
$db = new class (['DBDriver' => 'OCI8']) extends OCI8Connection {
61+
public function initialize(): void
62+
{
63+
}
64+
};
65+
66+
$db->commitMode = OCI_NO_AUTO_COMMIT;
67+
$this->setPrivateProperty($db, 'transDepth', 1);
68+
69+
$db->reconnect();
70+
71+
$this->assertSame(OCI_COMMIT_ON_SUCCESS, $db->commitMode);
72+
$this->assertSame(0, $this->getPrivateProperty($db, 'transDepth'));
73+
$this->assertFalse($db->transStatus());
74+
}
75+
}

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Bugs Fixed
3636

3737
- **CLI:** Fixed a bug where ``CLI::promptByMultipleKeys()`` accepted a key missing from the options when it was below the last key, returning an empty selection, rejected a key above the last one when the options were not in key order, and offered ``0`` as the default when the first key was not ``0``.
3838
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
39-
- **Database:** Fixed ``reconnect()`` so a replaced socket does not keep the transaction that died with the old connection. ``transDepth`` is cleared and the transaction status is failed, so ``transComplete()`` does not report success. See `#10648 <https://github.com/codeigniter4/CodeIgniter4/issues/10648>`_.
39+
- **Database:** Fixed ``reconnect()`` so a replaced socket does not keep the transaction that died with the old connection. ``transDepth`` is cleared and the transaction status is failed before the new connection is opened, so a failed reconnect cannot still report success. OCI8 commit mode returns to autocommit. ``transComplete()`` does not report success. This applies when ``ping()`` reports the connection lost; PostgreSQL's ``pg_ping()`` reconnects a broken connection itself, so it is not covered. See `#10648 <https://github.com/codeigniter4/CodeIgniter4/issues/10648>`_.
4040
- **Format:** Fixed ``XMLFormatter`` so that an array key that becomes empty after sanitization no longer triggers a ``ValueError``; the fallback tag name ``item`` is used instead. Values that cannot be converted to a string, such as objects not implementing ``Stringable``, now throw a ``FormatException`` instead of a fatal ``Error``.
4141
- **Helpers:** Fixed a regression in v4.7.5 where ``set_value()`` threw a ``TypeError`` when the default value was not a string or an array, such as an integer, a float, ``null``, or a ``Stringable`` object. Such values are now converted to a string.
4242
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive.

‎user_guide_src/source/database/connecting.rst‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ which can gracefully keep the connection alive or re-establish it.
100100
The ``reconnect()`` method pings the server to check if the connection is still
101101
alive. If the connection has been lost, it will close and re-establish the
102102
connection. An open transaction ends with the old connection. ``reconnect()``
103-
clears the transaction depth and marks the transaction failed, so
104-
``transComplete()`` does not report success.
103+
clears the transaction depth and marks the transaction failed before it
104+
opens the new connection, so ``transComplete()`` does not report success.
105+
On OCI8, commit mode returns to autocommit.
106+
107+
This only applies when ``ping()`` reports the connection lost. On PostgreSQL,
108+
``ping()`` uses ``pg_ping()``, which re-establishes a broken connection itself
109+
and returns true, so ``reconnect()`` does not clear the transaction state.
105110

106111
.. literalinclude:: connecting/007.php
107112
:lines: 2-

0 commit comments

Comments
 (0)