Skip to content

Commit 6456e69

Browse files
kalinin-k-ak.kalinin
authored andcommitted
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
1 parent b0726e7 commit 6456e69

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/Doctrine/DBAL/Connection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ public function connect()
354354
$this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions);
355355
$this->isConnected = true;
356356

357+
$this->transactionNestingLevel = 0;
358+
357359
if ($this->autoCommit === false) {
358360
$this->beginTransaction();
359361
}

tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ public function testTransactionNestingBehavior() : void
7070
$this->connection->rollBack();
7171
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
7272
}
73+
74+
$this->connection->beginTransaction();
75+
$this->connection->close();
76+
$this->connection->beginTransaction();
77+
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
78+
}
79+
80+
public function testTransactionNestingLevelIsResetOnReconnect() : void
81+
{
82+
if ($this->connection->getDatabasePlatform()->getName() === 'sqlite') {
83+
$params = $this->connection->getParams();
84+
$params['memory'] = false;
85+
$params['path'] = '/tmp/test_nesting.sqlite';
86+
87+
$connection = DriverManager::getConnection(
88+
$params,
89+
$this->connection->getConfiguration(),
90+
$this->connection->getEventManager()
91+
);
92+
} else {
93+
$connection = $this->connection;
94+
}
95+
96+
$connection->executeQuery('CREATE TABLE test_nesting(test int not null)');
97+
98+
$this->connection->beginTransaction();
99+
$this->connection->beginTransaction();
100+
$connection->close(); // connection closed in runtime (for example if lost or another application logic)
101+
102+
$connection->beginTransaction(); // should connect, reset nesting level and increase it once
103+
$connection->executeQuery('insert into test_nesting values (33)');
104+
$connection->rollback();
105+
106+
self::assertEquals(0, $connection->fetchColumn('select count(*) from test_nesting'));
73107
}
74108

75109
public function testTransactionNestingBehaviorWithSavepoints() : void

0 commit comments

Comments
 (0)