Skip to content

Commit

Permalink
add tests and update transaction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 13, 2016
1 parent 6976bf1 commit 7a0832b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
23 changes: 13 additions & 10 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,22 +601,25 @@ public function transaction(Closure $callback, $attempts = 1)
*/
public function beginTransaction()
{
++$this->transactions;

if ($this->transactions == 1) {
if ($this->transactions == 0) {
try {
$this->getPdo()->beginTransaction();
$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()) {
$this->getPdo()->exec(
$this->queryGrammar->compileSavepoint('trans'.$this->transactions)
} elseif ($this->transactions >= 1 && $this->queryGrammar->supportsSavepoints()) {
$this->pdo->exec(
$this->queryGrammar->compileSavepoint('trans'.($this->transactions + 1))
);
}

++$this->transactions;

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

Expand Down
41 changes: 36 additions & 5 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,46 @@ public function testAffectingStatementProperlyCallsPDO()
$this->assertTrue(is_numeric($log[0]['time']));
}

public function testTransactionsDecrementedOnTransactionException()
public function testTransactionLevelNotIncrementedOnTransactionException()
{
$pdo = $this->createMock('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);
$this->setExpectedException('ErrorException', 'MySQL server has gone away');
try {
$connection->beginTransaction();
} catch (Exception $e) {
$this->assertEquals(0, $connection->transactionLevel());
}
}

public function testBeginTransactionMethodRetriesOnFailure()
{
$pdo = $this->createMock('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();
$connection->disconnect();
$this->assertNull($connection->getPdo());
$this->assertEquals(1, $connection->transactionLevel());
}

public function testBeginTransactionMethodNeverRetriesIfWithinTransaction()
{
$pdo = $this->createMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction');
$pdo->expects($this->once())->method('exec')->will($this->throwException(new Exception));
$connection = $this->getMockConnection(['reconnect'], $pdo);
$queryGrammar = $this->createMock('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());
}
}

public function testCantSwapPDOWithOpenTransaction()
Expand Down

0 comments on commit 7a0832b

Please sign in to comment.