From 9b918da955f8170758698ba17c4252f3c77bad6c Mon Sep 17 00:00:00 2001 From: Konstantin Kalinin Date: Fri, 20 Sep 2019 12:59:32 +0300 Subject: [PATCH] Reset transaction nesting level on connection loss. When the connection is lost or is closed, subsequent transaction will no longer be nested because they started in a brand new session. Our internal representation of the nesting shold take this into account --- lib/Doctrine/DBAL/Connection.php | 2 ++ .../Tests/DBAL/Functional/ConnectionTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 7fef4e8628a..d5e6b7a048b 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -354,6 +354,8 @@ public function connect() $this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions); $this->isConnected = true; + $this->transactionNestingLevel = 0; + if ($this->autoCommit === false) { $this->beginTransaction(); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php index 9218d35e0ab..7b7a09935cd 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -72,6 +72,22 @@ public function testTransactionNestingBehavior() : void } } + public function testTransactionNestingLevelIsResetOnReconnect() : void + { + $this->connection->executeQuery('CREATE TABLE test_nesting(test int not null)'); + + $this->connection->beginTransaction(); + $this->connection->beginTransaction(); + $this->connection->close(); // connection is lost + + $this->connection->beginTransaction(); // should connect, reset nesting level and increase it once + $this->connection->executeQuery('insert into test_nesting values (33)'); + $this->connection->rollback(); + + self::assertEquals(0, $this->connection->fetchColumn('select count(*) from test_nesting' )); + + } + public function testTransactionNestingBehaviorWithSavepoints() : void { if (! $this->connection->getDatabasePlatform()->supportsSavepoints()) {