diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 85761fcca9b..3f4fe5a59a1 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -356,6 +356,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 b9d03d5e66c..6da314781b7 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php @@ -69,6 +69,40 @@ public function testTransactionNestingBehavior() $this->connection->rollBack(); self::assertEquals(0, $this->connection->getTransactionNestingLevel()); } + + $this->connection->beginTransaction(); + $this->connection->close(); + $this->connection->beginTransaction(); + self::assertEquals(1, $this->connection->getTransactionNestingLevel()); + } + + public function testTransactionNestingLevelIsResetOnReconnect() : void + { + if ($this->connection->getDatabasePlatform()->getName() === 'sqlite') { + $params = $this->connection->getParams(); + $params['memory'] = false; + $params['path'] = '/tmp/test_nesting.sqlite'; + + $connection = DriverManager::getConnection( + $params, + $this->connection->getConfiguration(), + $this->connection->getEventManager() + ); + } else { + $connection = $this->connection; + } + + $connection->executeQuery('CREATE TABLE test_nesting(test int not null)'); + + $this->connection->beginTransaction(); + $this->connection->beginTransaction(); + $connection->close(); // connection closed in runtime (for example if lost or another application logic) + + $connection->beginTransaction(); + $connection->executeQuery('insert into test_nesting values (33)'); + $connection->rollback(); + + self::assertEquals(0, $connection->fetchColumn('select count(*) from test_nesting')); } public function testTransactionNestingBehaviorWithSavepoints()