Commit 173d7ec1 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-55 - Refactored a bit and expaned tests

parent 0e460e8d
...@@ -751,6 +751,7 @@ class Connection implements DriverConnection ...@@ -751,6 +751,7 @@ class Connection implements DriverConnection
* Set if nested transactions should use savepoints * Set if nested transactions should use savepoints
* *
* @param boolean * @param boolean
* @return void
*/ */
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{ {
...@@ -758,8 +759,8 @@ class Connection implements DriverConnection ...@@ -758,8 +759,8 @@ class Connection implements DriverConnection
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction(); throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
} }
if ($nestTransactionsWithSavepoints && !$this->_platform->supportsSavepoints()) { if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported(); throw ConnectionException::savepointsNotSupported();
} }
$this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints; $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
...@@ -781,12 +782,9 @@ class Connection implements DriverConnection ...@@ -781,12 +782,9 @@ class Connection implements DriverConnection
* *
* @return mixed a string with the savepoint name or false * @return mixed a string with the savepoint name or false
*/ */
protected function _getNestedTransactionSavePointName() { protected function _getNestedTransactionSavePointName()
if ($this->_platform->supportsSavepoints() && !empty($this->_nestTransactionsWithSavepoints)) { {
return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel; return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
}
return false;
} }
/** /**
...@@ -802,11 +800,8 @@ class Connection implements DriverConnection ...@@ -802,11 +800,8 @@ class Connection implements DriverConnection
if ($this->_transactionNestingLevel == 1) { if ($this->_transactionNestingLevel == 1) {
$this->_conn->beginTransaction(); $this->_conn->beginTransaction();
} else { } else if ($this->_nestTransactionsWithSavepoints) {
$savepointName = $this->_getNestedTransactionSavePointName(); $this->createSavepoint($this->_getNestedTransactionSavePointName());
if ($savepointName) {
$this->createSavePoint($savepointName);
}
} }
} }
...@@ -830,11 +825,8 @@ class Connection implements DriverConnection ...@@ -830,11 +825,8 @@ class Connection implements DriverConnection
if ($this->_transactionNestingLevel == 1) { if ($this->_transactionNestingLevel == 1) {
$this->_conn->commit(); $this->_conn->commit();
} else { } else if ($this->_nestTransactionsWithSavepoints) {
$savepointName = $this->_getNestedTransactionSavePointName(); $this->releaseSavepoint($this->_getNestedTransactionSavePointName());
if ($savepointName && $this->_platform->supportsReleaseSavepoints()) {
$this->releaseSavePoint($savepointName);
}
} }
--$this->_transactionNestingLevel; --$this->_transactionNestingLevel;
...@@ -860,13 +852,11 @@ class Connection implements DriverConnection ...@@ -860,13 +852,11 @@ class Connection implements DriverConnection
$this->_transactionNestingLevel = 0; $this->_transactionNestingLevel = 0;
$this->_conn->rollback(); $this->_conn->rollback();
$this->_isRollbackOnly = false; $this->_isRollbackOnly = false;
} else if ($this->_nestTransactionsWithSavepoints) {
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->_transactionNestingLevel;
} else { } else {
$savepointName = $this->_getNestedTransactionSavePointName(); $this->_isRollbackOnly = true;
if (!$this->_isRollbackOnly && $savepointName) {
$this->rollbackSavePoint($savepointName);
} else {
$this->_isRollbackOnly = true;
}
--$this->_transactionNestingLevel; --$this->_transactionNestingLevel;
} }
} }
...@@ -878,10 +868,10 @@ class Connection implements DriverConnection ...@@ -878,10 +868,10 @@ class Connection implements DriverConnection
* @param string $savepoint name of a savepoint to set * @param string $savepoint name of a savepoint to set
* @return void * @return void
*/ */
public function createSavePoint($savepoint) public function createSavepoint($savepoint)
{ {
if (!$this->_platform->supportsSavepoints()) { if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported(); throw ConnectionException::savepointsNotSupported();
} }
$this->_conn->exec($this->_platform->createSavePoint($savepoint)); $this->_conn->exec($this->_platform->createSavePoint($savepoint));
...@@ -894,10 +884,10 @@ class Connection implements DriverConnection ...@@ -894,10 +884,10 @@ class Connection implements DriverConnection
* @param string $savepoint name of a savepoint to release * @param string $savepoint name of a savepoint to release
* @return void * @return void
*/ */
public function releaseSavePoint($savepoint) public function releaseSavepoint($savepoint)
{ {
if (!$this->_platform->supportsSavepoints()) { if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported(); throw ConnectionException::savepointsNotSupported();
} }
if ($this->_platform->supportsReleaseSavepoints()) { if ($this->_platform->supportsReleaseSavepoints()) {
...@@ -912,10 +902,10 @@ class Connection implements DriverConnection ...@@ -912,10 +902,10 @@ class Connection implements DriverConnection
* @param string $savepoint name of a savepoint to rollback to * @param string $savepoint name of a savepoint to rollback to
* @return void * @return void
*/ */
public function rollbackSavePoint($savepoint) public function rollbackSavepoint($savepoint)
{ {
if (!$this->_platform->supportsSavepoints()) { if (!$this->_platform->supportsSavepoints()) {
ConnectionException::savepointsNotSupported(); throw ConnectionException::savepointsNotSupported();
} }
$this->_conn->exec($this->_platform->rollbackSavePoint($savepoint)); $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
......
...@@ -44,7 +44,7 @@ class ConnectionException extends DBALException ...@@ -44,7 +44,7 @@ class ConnectionException extends DBALException
public static function savepointsNotSupported() 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() public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
......
...@@ -14,6 +14,12 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -14,6 +14,12 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
parent::setUp(); parent::setUp();
} }
public function tearDown()
{
parent::tearDown();
$this->resetSharedConn();
}
public function testGetWrappedConnection() public function testGetWrappedConnection()
{ {
$this->assertType('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection()); $this->assertType('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection());
...@@ -93,6 +99,65 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -93,6 +99,65 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
} }
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() public function testTransactionBehaviorWithRollback()
{ {
try { try {
...@@ -101,7 +166,7 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -101,7 +166,7 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
throw new \Exception; throw new \Exception;
$this->_connx->commit(); // never reached $this->_conn->commit(); // never reached
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel()); $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
$this->_conn->rollback(); $this->_conn->rollback();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment