Skip to content

Commit

Permalink
DBAL-55 - Refactored a bit and expaned tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei authored and juokaz committed Oct 5, 2010
1 parent 18d09fb commit 759af3c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 32 deletions.
50 changes: 20 additions & 30 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,16 @@ public function transactional(Closure $func)
* Set if nested transactions should use savepoints
*
* @param boolean
* @return void
*/
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{
if ($this->_transactionNestingLevel > 0) {
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
}

if ($nestTransactionsWithSavepoints && !$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported();
if (!$this->_platform->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
}

$this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
Expand All @@ -781,12 +782,9 @@ public function getNestTransactionsWithSavepoints()
*
* @return mixed a string with the savepoint name or false
*/
protected function _getNestedTransactionSavePointName() {
if ($this->_platform->supportsSavepoints() && !empty($this->_nestTransactionsWithSavepoints)) {
return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
}

return false;
protected function _getNestedTransactionSavePointName()
{
return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
}

/**
Expand All @@ -802,11 +800,8 @@ public function beginTransaction()

if ($this->_transactionNestingLevel == 1) {
$this->_conn->beginTransaction();
} else {
$savepointName = $this->_getNestedTransactionSavePointName();
if ($savepointName) {
$this->createSavePoint($savepointName);
}
} else if ($this->_nestTransactionsWithSavepoints) {
$this->createSavepoint($this->_getNestedTransactionSavePointName());
}
}

Expand All @@ -830,11 +825,8 @@ public function commit()

if ($this->_transactionNestingLevel == 1) {
$this->_conn->commit();
} else {
$savepointName = $this->_getNestedTransactionSavePointName();
if ($savepointName && $this->_platform->supportsReleaseSavepoints()) {
$this->releaseSavePoint($savepointName);
}
} else if ($this->_nestTransactionsWithSavepoints) {
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
}

--$this->_transactionNestingLevel;
Expand All @@ -860,13 +852,11 @@ public function rollback()
$this->_transactionNestingLevel = 0;
$this->_conn->rollback();
$this->_isRollbackOnly = false;
} else if ($this->_nestTransactionsWithSavepoints) {
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->_transactionNestingLevel;
} else {
$savepointName = $this->_getNestedTransactionSavePointName();
if (!$this->_isRollbackOnly && $savepointName) {
$this->rollbackSavePoint($savepointName);
} else {
$this->_isRollbackOnly = true;
}
$this->_isRollbackOnly = true;
--$this->_transactionNestingLevel;
}
}
Expand All @@ -878,10 +868,10 @@ public function rollback()
* @param string $savepoint name of a savepoint to set
* @return void
*/
public function createSavePoint($savepoint)
public function createSavepoint($savepoint)
{
if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported();
throw ConnectionException::savepointsNotSupported();
}

$this->_conn->exec($this->_platform->createSavePoint($savepoint));
Expand All @@ -894,10 +884,10 @@ public function createSavePoint($savepoint)
* @param string $savepoint name of a savepoint to release
* @return void
*/
public function releaseSavePoint($savepoint)
public function releaseSavepoint($savepoint)
{
if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported();
throw ConnectionException::savepointsNotSupported();
}

if ($this->_platform->supportsReleaseSavepoints()) {
Expand All @@ -912,10 +902,10 @@ public function releaseSavePoint($savepoint)
* @param string $savepoint name of a savepoint to rollback to
* @return void
*/
public function rollbackSavePoint($savepoint)
public function rollbackSavepoint($savepoint)
{
if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported();
throw ConnectionException::savepointsNotSupported();
}

$this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function noActiveTransaction()

public static function savepointsNotSupported()
{
return new self("Savepoints are not supported transaction.");
return new self("Savepoints are not supported by this driver.");
}

public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
Expand Down
67 changes: 66 additions & 1 deletion tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public function setUp()
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
$this->resetSharedConn();
}

public function testGetWrappedConnection()
{
$this->assertType('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection());
Expand Down Expand Up @@ -93,6 +99,65 @@ public function testTransactionNestingBehaviorWithSavepoints()
}
}

public function testTransactionNestingBehaviorCantBeChangedInActiveTransaction()
{
if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
$this->markTestSkipped('This test requires the platform to support savepoints.');
}

$this->_conn->beginTransaction();
try {
$this->_conn->setNestTransactionsWithSavepoints(true);
$this->fail('An exception should have been thrown by chaning the nesting transaction behavior within an transaction.');
} catch(ConnectionException $e) {
$this->_conn->rollBack();
}
}

public function testSetNestedTransactionsThroughSavepointsNotSupportedThrowsException()
{
if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
$this->markTestSkipped('This test requires the platform not to support savepoints.');
}

$this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

$this->_conn->setNestTransactionsWithSavepoints(true);
}

public function testCreateSavepointsNotSupportedThrowsException()
{
if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
$this->markTestSkipped('This test requires the platform not to support savepoints.');
}

$this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

$this->_conn->createSavepoint('foo');
}

public function testReleaseSavepointsNotSupportedThrowsException()
{
if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
$this->markTestSkipped('This test requires the platform not to support savepoints.');
}

$this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

$this->_conn->releaseSavepoint('foo');
}

public function testRollbackSavepointsNotSupportedThrowsException()
{
if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
$this->markTestSkipped('This test requires the platform not to support savepoints.');
}

$this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");

$this->_conn->rollbackSavepoint('foo');
}

public function testTransactionBehaviorWithRollback()
{
try {
Expand All @@ -101,7 +166,7 @@ public function testTransactionBehaviorWithRollback()

throw new \Exception;

$this->_connx->commit(); // never reached
$this->_conn->commit(); // never reached
} catch (\Exception $e) {
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
$this->_conn->rollback();
Expand Down

0 comments on commit 759af3c

Please sign in to comment.