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

[5.1] beginTransaction(): Reconnect if connection is lost #15511

Merged
merged 2 commits into from
Oct 13, 2016
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
24 changes: 15 additions & 9 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,26 +498,30 @@ public function transaction(Closure $callback)
* Start a new database transaction.
*
* @return void
* @throws Exception
*
* @throws \Exception
*/
public function beginTransaction()
{
++$this->transactions;

if ($this->transactions == 1) {
if ($this->transactions == 0) {
try {
$this->pdo->beginTransaction();
} catch (Exception $e) {
--$this->transactions;

throw $e;
if ($this->causedByLostConnection($e)) {
$this->reconnect();
$this->pdo->beginTransaction();
} else {
throw $e;
}
}
} elseif ($this->transactions > 1 && $this->queryGrammar->supportsSavepoints()) {
} elseif ($this->transactions >= 1 && $this->queryGrammar->supportsSavepoints()) {
$this->pdo->exec(
$this->queryGrammar->compileSavepoint('trans'.$this->transactions)
$this->queryGrammar->compileSavepoint('trans'.($this->transactions + 1))
);
}

++$this->transactions;

$this->fireConnectionEvent('beganTransaction');
}

Expand Down Expand Up @@ -866,6 +870,8 @@ public function getReadPdo()
*
* @param \PDO|null $pdo
* @return $this
*
* @throws \RuntimeException
*/
public function setPdo($pdo)
{
Expand Down
36 changes: 33 additions & 3 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,48 @@ public function testAffectingStatementProperlyCallsPDO()
$this->assertTrue(is_numeric($log[0]['time']));
}

public function testTransactionsDecrementedOnTransactionException()
public function testTransactionLevelNotIncrementedOnTransactionException()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new ErrorException('MySQL server has gone away')));
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new Exception));
$connection = $this->getMockConnection([], $pdo);
try {
$connection->beginTransaction();
} catch (ErrorException $e) {
} catch (Exception $e) {
$this->assertEquals(0, $connection->transactionLevel());
}
}

public function testBeginTransactionMethodRetriesOnFailure()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->exactly(2))->method('beginTransaction');
$pdo->expects($this->at(0))->method('beginTransaction')->will($this->throwException(new ErrorException('server has gone away')));
$connection = $this->getMockConnection(['reconnect'], $pdo);
$connection->expects($this->once())->method('reconnect');
$connection->beginTransaction();
$this->assertEquals(1, $connection->transactionLevel());
}

public function testBeginTransactionMethodNeverRetriesIfWithinTransaction()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction');
$pdo->expects($this->once())->method('exec')->will($this->throwException(new Exception));
$connection = $this->getMockConnection([], $pdo);
$queryGrammar = $this->getMock('Illuminate\Database\Query\Grammars\Grammar');
$queryGrammar->expects($this->once())->method('supportsSavepoints')->will($this->returnValue(true));
$connection->setQueryGrammar($queryGrammar);
$connection->expects($this->never())->method('reconnect');
$connection->beginTransaction();
$this->assertEquals(1, $connection->transactionLevel());
try {
$connection->beginTransaction();
} catch (Exception $e) {
$this->assertEquals(1, $connection->transactionLevel());
}
}

/**
* @expectedException RuntimeException
*/
Expand Down