Skip to content

Commit

Permalink
Merge pull request #3679 from kalinin-k-a/fix_begin_transaction_after…
Browse files Browse the repository at this point in the history
…_reconnect

fix begin trasaction after reconnect
  • Loading branch information
morozov authored Oct 14, 2019
2 parents 7241a65 + b550879 commit 8012286
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 8012286

Please sign in to comment.