Skip to content

Commit

Permalink
Add tests for beginTransaction() retrying
Browse files Browse the repository at this point in the history
  • Loading branch information
nhowell committed Sep 22, 2016
1 parent 695e4c0 commit 33c6f9a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ public function beginTransaction()
{
if ($this->transactions == 0) {
try {
$this->getPdo()->beginTransaction();
$this->pdo->beginTransaction();
} catch (Exception $e) {
if ($this->causedByLostConnection($e)) {
$this->reconnect();
$this->getPdo()->beginTransaction();
$this->pdo->beginTransaction();
} else {
throw $e;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ public function testTransactionLevelNotIncrementedOnTransactionException()
}
}

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 33c6f9a

Please sign in to comment.