Skip to content

Commit

Permalink
use getPdo
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 14, 2016
2 parents 8c51aa5 + c8fbb8e commit d06c52a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,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->getPdo()->beginTransaction();
try {
$this->getPdo()->beginTransaction();
} catch (Exception $e) {
--$this->transactions;

throw $e;
}
} elseif ($this->transactions > 1 && $this->queryGrammar->supportsSavepoints()) {
$this->getPdo()->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

0 comments on commit d06c52a

Please sign in to comment.