Skip to content

Commit

Permalink
Merge branch 'transaction-reconnect-if-lost-connection' of https://gi…
Browse files Browse the repository at this point in the history
…thub.com/nhowell/laravel-framework into nhowell-transaction-reconnect-if-lost-connection
  • Loading branch information
taylorotwell committed Oct 13, 2016
2 parents 44a9fcf + 33c6f9a commit 411aa8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
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

0 comments on commit 411aa8e

Please sign in to comment.