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

[6.x] Reconnect missing connection when beginning transaction #30474

Merged
merged 1 commit into from
Oct 31, 2019
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
Reconnect missing connection when beginning transaction
  • Loading branch information
cmorbitzer committed Oct 31, 2019
commit 35370b3d62e56754f3154ee880b89a4feff6a545
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public function beginTransaction()
protected function createTransaction()
{
if ($this->transactions == 0) {
$this->reconnectIfMissingConnection();

try {
$this->getPdo()->beginTransaction();
} catch (Exception $e) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ public function testBeginTransactionMethodRetriesOnFailure()
$this->assertEquals(1, $connection->transactionLevel());
}

public function testBeginTransactionMethodReconnectsMissingConnection()
{
$connection = $this->getMockConnection();
$connection->setReconnector(function ($connection) {
$pdo = $this->createMock(DatabaseConnectionTestMockPDO::class);
$connection->setPdo($pdo);
});
$connection->disconnect();
$connection->beginTransaction();
$this->assertEquals(1, $connection->transactionLevel());
}

public function testBeginTransactionMethodNeverRetriesIfWithinTransaction()
{
$pdo = $this->createMock(DatabaseConnectionTestMockPDO::class);
Expand Down