Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix begin trasaction after reconnect #3679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
morozov marked this conversation as resolved.
Show resolved Hide resolved
}

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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the transaction nesting level be checked here too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. The fact that the transaction was successfully rolled back is enough. The nesting level is an implementation detail in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the transaction nesting level be checked here too?

The exact values of nestingLevel is completely tested in testTransactionNestingBehavior()

}

public function testTransactionNestingBehaviorWithSavepoints() : void
Expand Down