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] Decrement transaction count when beginTransaction errors #13551

Merged
merged 3 commits into from
May 14, 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
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,20 @@ public function transaction(Closure $callback)
* Start a new database transaction.
*
* @return void
* @throws Exception
*/
public function beginTransaction()
{
++$this->transactions;

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

throw $e;
}
} elseif ($this->transactions > 1 && $this->queryGrammar->supportsSavepoints()) {
$this->pdo->exec(
$this->queryGrammar->compileSavepoint('trans'.$this->transactions)
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ public function testAffectingStatementProperlyCallsPDO()
$this->assertTrue(is_numeric($log[0]['time']));
}

public function testTransactionsDecrementedOnTransactionException()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new ErrorException('MySQL server has gone away')));
$connection = $this->getMockConnection([], $pdo);
$this->setExpectedException('ErrorException', 'MySQL server has gone away');
$connection->beginTransaction();
$connection->disconnect();
$this->assertNull($connection->getPdo());
}

public function testCantSwapPDOWithOpenTransaction()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
$connection = $this->getMockConnection([], $pdo);
$connection->beginTransaction();
$this->setExpectedException('RuntimeException', "Can't swap PDO instance while within transaction.");
$connection->disconnect();
}

public function testBeganTransactionFiresEventsIfSet()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
Expand Down