Skip to content

Commit

Permalink
Reset transaction nesting level on connection loss.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kalinin-k-a authored and k.kalinin committed Oct 14, 2019
1 parent b0726e7 commit b550879
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ public function testTransactionNestingBehavior() : void
$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() : void
Expand Down

0 comments on commit b550879

Please sign in to comment.