Repository navigation
fix: clear transaction state when reconnect() replaces the socket #10650
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
Open
KevinBlakeLopez
wants to merge
3
commits into
codeigniter4:develop
Choose a base branch
from
KevinBlakeLopez:fix/10648-reconnect-clears-transaction
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -625,16 +625,44 @@ public function close() | |
| * Keep or establish the connection if no queries have been sent for | ||
| * a length of time exceeding the server's idle timeout. | ||
| * | ||
| * A replaced socket is not in the transaction that was open on the old | ||
| * one. The server has already rolled that work back. Clear the depth and | ||
| * mark the transaction failed before opening the new connection, so a | ||
| * failed reconnect cannot still report success. OCI8 also returns its | ||
| * commit mode to autocommit. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function reconnect() | ||
| { | ||
| if ($this->ping() === false) { | ||
| $lostTransaction = $this->transDepth > 0; | ||
|
|
||
| $this->close(); | ||
|
|
||
| // initialize() can throw. Clear the lost transaction first so a | ||
| // failed reconnect cannot still report the old transaction. | ||
|
Comment on lines
+643
to
+644
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to just repeat the added docblock above. |
||
| if ($lostTransaction) { | ||
| $this->abandonLostTransaction(); | ||
| } | ||
|
|
||
| $this->initialize(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The socket that owned the transaction is already closed. | ||
| * | ||
| * Drivers that keep transaction state in PHP, rather than only on the | ||
| * server, must override this. OCI8 commit mode is one case: leaving it | ||
| * at OCI_NO_AUTO_COMMIT makes later writes uncommitted. | ||
| */ | ||
| protected function abandonLostTransaction(): void | ||
| { | ||
| $this->transDepth = 0; | ||
| $this->transStatus = false; | ||
| } | ||
|
|
||
| /** | ||
| * Platform dependent way method for closing the connection. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tests/system/Database/Live/ReconnectDuringTransactionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database\Live; | ||
|
|
||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use CodeIgniter\Test\DatabaseTestTrait; | ||
| use Config\Database; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('DatabaseLive')] | ||
| final class ReconnectDuringTransactionTest extends CIUnitTestCase | ||
| { | ||
| use DatabaseTestTrait; | ||
|
|
||
| protected $migrate = false; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| // Dedicated connection so a forced wait_timeout cannot drop the shared one. | ||
| $this->db = Database::connect($this->DBGroup, false); | ||
|
|
||
| parent::setUp(); | ||
| } | ||
|
|
||
| public function testReconnectInsideTransactionDoesNotReportSuccess(): void | ||
| { | ||
| if ($this->db->DBDriver !== 'MySQLi') { | ||
| $this->markTestSkipped('Requires the MySQLi driver.'); | ||
| } | ||
|
|
||
| $table = $this->db->escapeIdentifier($this->db->prefixTable('reconnect_probe')); | ||
| $this->db->query('DROP TABLE IF EXISTS ' . $table); | ||
| $this->db->query( | ||
| 'CREATE TABLE ' . $table . ' (' | ||
| . 'id INT NOT NULL AUTO_INCREMENT, ' | ||
| . 'note VARCHAR(32) NOT NULL, ' | ||
| . 'PRIMARY KEY (id)' | ||
| . ') ENGINE=InnoDB', | ||
| ); | ||
|
|
||
| $observer = Database::connect($this->DBGroup, false); | ||
| $observer->initialize(); | ||
|
|
||
| try { | ||
| $this->db->transStart(); | ||
| $this->db->table('reconnect_probe')->insert(['note' => 'before-timeout']); | ||
| $this->assertSame(1, $this->db->transDepth); | ||
| $this->assertTrue($this->db->transStatus()); | ||
|
|
||
| $this->db->query('SET SESSION wait_timeout = 1'); | ||
| sleep(3); | ||
|
|
||
| $this->assertFalse($this->db->ping(), 'Precondition: the server should have closed the idle socket.'); | ||
|
|
||
| $this->db->reconnect(); | ||
|
|
||
| $depthAfterReconnect = $this->db->transDepth; | ||
| $statusAfterReconnect = $this->db->transStatus(); | ||
| $completed = $this->db->transComplete(); | ||
| $rows = (int) $observer->table('reconnect_probe')->countAllResults(); | ||
|
|
||
| $this->assertSame( | ||
| ['depth' => 0, 'status' => false, 'completed' => false, 'rows' => 0], | ||
| [ | ||
| 'depth' => $depthAfterReconnect, | ||
| 'status' => $statusAfterReconnect, | ||
| 'completed' => $completed, | ||
| 'rows' => $rows, | ||
| ], | ||
| ); | ||
| } finally { | ||
| if ($this->db->transDepth > 0) { | ||
| $this->db->transRollback(); | ||
| } | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS ' . $table); | ||
| $observer->close(); | ||
| $this->db->close(); | ||
| } | ||
| } | ||
|
|
||
| public function testReconnectKeepsAnOpenTransactionWhenTheSocketIsAlive(): void | ||
| { | ||
| if ($this->db->DBDriver !== 'MySQLi') { | ||
| $this->markTestSkipped('Requires the MySQLi driver.'); | ||
| } | ||
|
|
||
| $table = $this->db->escapeIdentifier($this->db->prefixTable('reconnect_probe')); | ||
| $this->db->query('DROP TABLE IF EXISTS ' . $table); | ||
| $this->db->query( | ||
| 'CREATE TABLE ' . $table . ' (' | ||
| . 'id INT NOT NULL AUTO_INCREMENT, ' | ||
| . 'note VARCHAR(32) NOT NULL, ' | ||
| . 'PRIMARY KEY (id)' | ||
| . ') ENGINE=InnoDB', | ||
| ); | ||
|
|
||
| try { | ||
| $this->db->transStart(); | ||
| $this->db->table('reconnect_probe')->insert(['note' => 'still-open']); | ||
| $this->assertTrue($this->db->ping()); | ||
|
|
||
| $this->db->reconnect(); | ||
|
|
||
| $this->assertSame(1, $this->db->transDepth); | ||
| $this->assertTrue($this->db->transStatus()); | ||
| $this->assertTrue($this->db->transComplete()); | ||
| $this->assertSame(1, $this->db->table('reconnect_probe')->countAllResults()); | ||
| } finally { | ||
| if ($this->db->transDepth > 0) { | ||
| $this->db->transRollback(); | ||
| } | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS ' . $table); | ||
| $this->db->close(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database; | ||
|
|
||
| use CodeIgniter\Database\Exceptions\DatabaseException; | ||
| use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection; | ||
| use CodeIgniter\Database\OCI8\Connection as OCI8Connection; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class ReconnectLostTransactionTest extends CIUnitTestCase | ||
| { | ||
| public function testReconnectClearsTransactionStateWhenInitializeFails(): void | ||
| { | ||
| $db = new class (['DBDriver' => 'MySQLi']) extends MySQLiConnection { | ||
| public function initialize(): never | ||
| { | ||
| throw new DatabaseException('database unavailable'); | ||
| } | ||
| }; | ||
|
|
||
| $this->setPrivateProperty($db, 'transDepth', 1); | ||
|
|
||
| try { | ||
| $db->reconnect(); | ||
| $this->fail('initialize() should have thrown.'); | ||
| } catch (DatabaseException $exception) { | ||
| $this->assertSame('database unavailable', $exception->getMessage()); | ||
| } | ||
|
|
||
| $this->assertSame(0, $this->getPrivateProperty($db, 'transDepth')); | ||
| $this->assertFalse($db->transStatus()); | ||
| } | ||
|
|
||
| public function testReconnectRestoresOci8CommitModeWhenTransactionIsLost(): void | ||
| { | ||
| if (! defined('OCI_COMMIT_ON_SUCCESS')) { | ||
| define('OCI_COMMIT_ON_SUCCESS', 32); | ||
| } | ||
|
|
||
| if (! defined('OCI_NO_AUTO_COMMIT')) { | ||
| define('OCI_NO_AUTO_COMMIT', 0); | ||
| } | ||
|
|
||
| $db = new class (['DBDriver' => 'OCI8']) extends OCI8Connection { | ||
| public function initialize(): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $db->commitMode = OCI_NO_AUTO_COMMIT; | ||
| $this->setPrivateProperty($db, 'transDepth', 1); | ||
|
|
||
| $db->reconnect(); | ||
|
|
||
| $this->assertSame(OCI_COMMIT_ON_SUCCESS, $db->commitMode); | ||
| $this->assertSame(0, $this->getPrivateProperty($db, 'transDepth')); | ||
| $this->assertFalse($db->transStatus()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the mention of OCI8 can go to the OCI8 connection?