Skip to content

Commit

Permalink
remove dangerous transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Green committed Mar 17, 2015
1 parent daf2f31 commit 5aaa5b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
52 changes: 20 additions & 32 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ class Connection implements ConnectionInterface
*/
protected $fetchMode = PDO::FETCH_ASSOC;

/**
* The number of active transactions.
*
* @var int
*/
protected $transactions = 0;

/**
* Indicates whether queries are being logged.
*
Expand Down Expand Up @@ -433,65 +426,57 @@ public function transaction(Closure $callback)
/**
* Start a new database transaction.
*
* @return void
* @return $this
*/
public function beginTransaction()
{
++$this->transactions;
$this->reconnectIfMissingConnection();

if ($this->transactions == 1) {
$this->pdo->beginTransaction();
}
$this->pdo->beginTransaction();

return $this;
}

/**
* Commit the active database transaction.
*
* @return void
* @return $this
*/
public function commit()
{
if ($this->transactions == 1) $this->pdo->commit();
$this->reconnectIfMissingConnection();

--$this->transactions;
$this->pdo->commit();

return $this;
}

/**
* Rollback the active database transaction.
*
* @return void
* @return $this
*/
public function rollBack()
{
if ($this->transactions == 1) {
$this->transactions = 0;
$this->reconnectIfMissingConnection();

$this->pdo->rollBack();
} else {
--$this->transactions;
}
$this->pdo->rollBack();

return $this;
}

/**
* Get the number of active transactions.
*
* @return int
* @return bool
*/
public function transactionLevel()
public function inTransaction()
{
return $this->transactions;
$this->reconnectIfMissingConnection();

return $this->pdo->inTransaction();
}

/**
* Disconnect from the underlying PDO connection.
*
* @return void
* @return $this
*/
public function disconnect()
{
Expand Down Expand Up @@ -575,9 +560,12 @@ public function getPdo()
*/
public function getReadPdo()
{
if ($this->transactions >= 1) return $this->getPdo();
if (!$this->readPdo || $this->pdo->inTransaction())
{
return $this->getPdo();
}

return $this->readPdo ?: $this->pdo;
return $this->readPdo;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public function commit();
public function rollBack();

/**
* Get the number of active transactions.
* Checks the connection to see if there is an active transaction
*
* @return int
*/
public function transactionLevel();
public function inTransaction();

/**
* Execute the given callback in "dry run" mode.
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ public function testFetchOneCallsSelectAndReturnsSingleResult()
public function testFetchProperlyCallsPDO()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO', array('prepare'));
$writePdo = $this->getMock('DatabaseConnectionTestMockPDO', array('prepare'));
$writePdo = $this->getMock('DatabaseConnectionTestMockPDO', array('prepare', 'inTransaction'));
$writePdo->expects($this->never())->method('prepare');
$writePdo->expects($this->exactly(2))->method('inTransaction')->willReturn(false);
$statement = $this->getMock('PDOStatement', array('execute', 'fetch'));
$statement->expects($this->once())->method('execute')->with($this->equalTo(array('foo' => 'bar')));
$statement->expects($this->once())->method('fetch')->will($this->returnValue(array('boom')));
$pdo->expects($this->once())->method('prepare')->with('foo')->will($this->returnValue($statement));
$mock = $this->getMockConnection(array('prepareBindings', 'query'), $writePdo);
$mock->setReadPdo($pdo);
$mock->setPdo($pdo);
$mock->setPdo($writePdo);
$mock->expects($this->once())->method('prepareBindings')->with($this->equalTo(array('foo' => 'bar')))->will($this->returnValue(array('foo' => 'bar')));
$results = $mock->fetch('foo', array('foo' => 'bar'));
$this->assertEquals(array('boom'), $results);
Expand Down

0 comments on commit 5aaa5b9

Please sign in to comment.