1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Revertd partially the support to DBAL\Transaction, it was wrong. Fixed implementation of ORM\EntityTransaction and fixed tests.

This commit is contained in:
Guilherme Blanco 2010-05-06 18:39:19 -03:00
parent 4ab5dffc4c
commit b12b8b0041
21 changed files with 337 additions and 429 deletions

View File

@ -42,6 +42,26 @@ use PDO, Closure,
*/ */
class Connection implements DriverConnection class Connection implements DriverConnection
{ {
/**
* Constant for transaction isolation level READ UNCOMMITTED.
*/
const TRANSACTION_READ_UNCOMMITTED = 1;
/**
* Constant for transaction isolation level READ COMMITTED.
*/
const TRANSACTION_READ_COMMITTED = 2;
/**
* Constant for transaction isolation level REPEATABLE READ.
*/
const TRANSACTION_REPEATABLE_READ = 3;
/**
* Constant for transaction isolation level SERIALIZABLE.
*/
const TRANSACTION_SERIALIZABLE = 4;
/** /**
* The wrapped driver connection. * The wrapped driver connection.
* *
@ -66,6 +86,20 @@ class Connection implements DriverConnection
*/ */
private $_isConnected = false; private $_isConnected = false;
/**
* The transaction nesting level.
*
* @var integer
*/
private $_transactionNestingLevel = 0;
/**
* The currently active transaction isolation level.
*
* @var integer
*/
private $_transactionIsolationLevel;
/** /**
* The parameters used during creation of the Connection instance. * The parameters used during creation of the Connection instance.
* *
@ -94,14 +128,14 @@ class Connection implements DriverConnection
* @var Doctrine\DBAL\Driver * @var Doctrine\DBAL\Driver
*/ */
protected $_driver; protected $_driver;
/**
* The DBAL Transaction.
*
* @var Doctrine\DBAL\Transaction
*/
protected $_transaction;
/**
* Flag that indicates whether the current transaction is marked for rollback only.
*
* @var boolean
*/
private $_isRollbackOnly = false;
/** /**
* Initializes a new instance of the Connection class. * Initializes a new instance of the Connection class.
* *
@ -132,7 +166,6 @@ class Connection implements DriverConnection
$this->_config = $config; $this->_config = $config;
$this->_eventManager = $eventManager; $this->_eventManager = $eventManager;
if ( ! isset($params['platform'])) { if ( ! isset($params['platform'])) {
$this->_platform = $driver->getDatabasePlatform(); $this->_platform = $driver->getDatabasePlatform();
} else if ($params['platform'] instanceof Platforms\AbstractPlatform) { } else if ($params['platform'] instanceof Platforms\AbstractPlatform) {
@ -140,8 +173,7 @@ class Connection implements DriverConnection
} else { } else {
throw DBALException::invalidPlatformSpecified(); throw DBALException::invalidPlatformSpecified();
} }
$this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
$this->_transaction = new Transaction($this);
} }
/** /**
@ -244,16 +276,6 @@ class Connection implements DriverConnection
return $this->_platform; return $this->_platform;
} }
/**
* Gets the DBAL Transaction instance.
*
* @return Doctrine\DBAL\Transaction
*/
public function getTransaction()
{
return $this->_transaction;
}
/** /**
* Establishes the connection with the database. * Establishes the connection with the database.
* *
@ -288,8 +310,9 @@ class Connection implements DriverConnection
* @param string $statement The SQL query. * @param string $statement The SQL query.
* @param array $params The query parameters. * @param array $params The query parameters.
* @return array * @return array
* @todo Rename: fetchAssoc
*/ */
public function fetchAssoc($statement, array $params = array()) public function fetchRow($statement, array $params = array())
{ {
return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC); return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
} }
@ -331,6 +354,16 @@ class Connection implements DriverConnection
return $this->_isConnected; return $this->_isConnected;
} }
/**
* Checks whether a transaction is currently active.
*
* @return boolean TRUE if a transaction is currently active, FALSE otherwise.
*/
public function isTransactionActive()
{
return $this->_transactionNestingLevel > 0;
}
/** /**
* Executes an SQL DELETE statement on a table. * Executes an SQL DELETE statement on a table.
* *
@ -365,6 +398,28 @@ class Connection implements DriverConnection
$this->_isConnected = false; $this->_isConnected = false;
} }
/**
* Sets the transaction isolation level.
*
* @param integer $level The level to set.
*/
public function setTransactionIsolation($level)
{
$this->_transactionIsolationLevel = $level;
return $this->executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
}
/**
* Gets the currently active transaction isolation level.
*
* @return integer The current transaction isolation level.
*/
public function getTransactionIsolation()
{
return $this->_transactionIsolationLevel;
}
/** /**
* Executes an SQL UPDATE statement on a table. * Executes an SQL UPDATE statement on a table.
* *
@ -528,10 +583,10 @@ class Connection implements DriverConnection
* represents a row of the result set. * represents a row of the result set.
* @return mixed The projected result of the query. * @return mixed The projected result of the query.
*/ */
public function project($query, array $params, Closure $function) public function project($query, array $params = array(), Closure $function)
{ {
$result = array(); $result = array();
$stmt = $this->executeQuery($query, $params ?: array()); $stmt = $this->executeQuery($query, $params);
while ($row = $stmt->fetch()) { while ($row = $stmt->fetch()) {
$result[] = $function($row); $result[] = $function($row);
@ -602,6 +657,16 @@ class Connection implements DriverConnection
return $this->_conn->exec($statement); return $this->_conn->exec($statement);
} }
/**
* Returns the current transaction nesting level.
*
* @return integer The nesting level. A value of 0 means there's no active transaction.
*/
public function getTransactionNestingLevel()
{
return $this->_transactionNestingLevel;
}
/** /**
* Fetch the SQLSTATE associated with the last database operation. * Fetch the SQLSTATE associated with the last database operation.
* *
@ -641,6 +706,73 @@ class Connection implements DriverConnection
return $this->_conn->lastInsertId($seqName); return $this->_conn->lastInsertId($seqName);
} }
/**
* Starts a transaction by suspending auto-commit mode.
*
* @return void
*/
public function beginTransaction()
{
$this->connect();
if ($this->_transactionNestingLevel == 0) {
$this->_conn->beginTransaction();
}
++$this->_transactionNestingLevel;
}
/**
* Commits the current transaction.
*
* @return void
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
*/
public function commit()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::commitFailedNoActiveTransaction();
}
if ($this->_isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly();
}
$this->connect();
if ($this->_transactionNestingLevel == 1) {
$this->_conn->commit();
}
--$this->_transactionNestingLevel;
}
/**
* Cancel any database changes done during the current transaction.
*
* this method can be listened with onPreTransactionRollback and onTransactionRollback
* eventlistener methods
*
* @throws ConnectionException If the rollback operation failed.
*/
public function rollback()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::rollbackFailedNoActiveTransaction();
}
$this->connect();
if ($this->_transactionNestingLevel == 1) {
$this->_transactionNestingLevel = 0;
$this->_conn->rollback();
$this->_isRollbackOnly = false;
} else {
$this->_isRollbackOnly = true;
--$this->_transactionNestingLevel;
}
}
/** /**
* Gets the wrapped driver connection. * Gets the wrapped driver connection.
* *
@ -657,7 +789,7 @@ class Connection implements DriverConnection
* Gets the SchemaManager that can be used to inspect or change the * Gets the SchemaManager that can be used to inspect or change the
* database schema through the connection. * database schema through the connection.
* *
* @return Doctrine\DBAL\Schema\AbstractSchemaManager * @return Doctrine\DBAL\Schema\SchemaManager
*/ */
public function getSchemaManager() public function getSchemaManager()
{ {
@ -668,6 +800,34 @@ class Connection implements DriverConnection
return $this->_schemaManager; return $this->_schemaManager;
} }
/**
* Marks the current transaction so that the only possible
* outcome for the transaction to be rolled back.
*
* @throws ConnectionException If no transaction is active.
*/
public function setRollbackOnly()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::noActiveTransaction();
}
$this->_isRollbackOnly = true;
}
/**
* Check whether the current transaction is marked for rollback only.
*
* @return boolean
* @throws ConnectionException If no transaction is active.
*/
public function getRollbackOnly()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::noActiveTransaction();
}
return $this->_isRollbackOnly;
}
/** /**
* Converts a given value to its database representation according to the conversion * Converts a given value to its database representation according to the conversion
* rules of a specific DBAL mapping type. * rules of a specific DBAL mapping type.
@ -751,4 +911,4 @@ class Connection implements DriverConnection
} }
} }
} }
} }

BIN
lib/Doctrine/DBAL/Driver/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -36,6 +36,9 @@ interface Connection
function quote($input, $type=\PDO::PARAM_STR); function quote($input, $type=\PDO::PARAM_STR);
function exec($statement); function exec($statement);
function lastInsertId($name = null); function lastInsertId($name = null);
function beginTransaction();
function commit();
function rollBack();
function errorCode(); function errorCode();
function errorInfo(); function errorInfo();
} }

View File

@ -26,7 +26,7 @@ namespace Doctrine\DBAL\Driver\OCI8;
* *
* @since 2.0 * @since 2.0
*/ */
class OCI8Connection implements \Doctrine\DBAL\Driver\Connection, \Doctrine\DBAL\Driver\Transaction class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
{ {
private $_dbh; private $_dbh;

View File

@ -29,7 +29,7 @@ use \PDO;
* *
* @since 2.0 * @since 2.0
*/ */
class PDOConnection extends PDO implements Connection, Transaction class PDOConnection extends PDO implements Connection
{ {
public function __construct($dsn, $user = null, $password = null, array $options = null) public function __construct($dsn, $user = null, $password = null, array $options = null)
{ {

View File

@ -1,42 +0,0 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Driver;
/**
* Transaction interface.
* Each Driver Connection must implement this interface.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
interface Transaction
{
function beginTransaction();
function commit();
function rollBack();
}

View File

@ -23,7 +23,6 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException, use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Connection, Doctrine\DBAL\Connection,
Doctrine\DBAL\Transaction,
Doctrine\DBAL\Types, Doctrine\DBAL\Types,
Doctrine\DBAL\Schema\Table, Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Index,
@ -1457,13 +1456,13 @@ abstract class AbstractPlatform
protected function _getTransactionIsolationLevelSQL($level) protected function _getTransactionIsolationLevelSQL($level)
{ {
switch ($level) { switch ($level) {
case Transaction::READ_UNCOMMITTED: case Connection::TRANSACTION_READ_UNCOMMITTED:
return 'READ UNCOMMITTED'; return 'READ UNCOMMITTED';
case Transaction::READ_COMMITTED: case Connection::TRANSACTION_READ_COMMITTED:
return 'READ COMMITTED'; return 'READ COMMITTED';
case Transaction::REPEATABLE_READ: case Connection::TRANSACTION_REPEATABLE_READ:
return 'REPEATABLE READ'; return 'REPEATABLE READ';
case Transaction::SERIALIZABLE: case Connection::TRANSACTION_SERIALIZABLE:
return 'SERIALIZABLE'; return 'SERIALIZABLE';
default: default:
throw new \InvalidArgumentException('Invalid isolation level:' . $level); throw new \InvalidArgumentException('Invalid isolation level:' . $level);
@ -1596,11 +1595,11 @@ abstract class AbstractPlatform
* Gets the default transaction isolation level of the platform. * Gets the default transaction isolation level of the platform.
* *
* @return integer The default isolation level. * @return integer The default isolation level.
* @see Doctrine\DBAL\Transaction constants. * @see Doctrine\DBAL\Connection\TRANSACTION_* constants.
*/ */
public function getDefaultTransactionIsolationLevel() public function getDefaultTransactionIsolationLevel()
{ {
return Transaction::READ_COMMITTED; return Connection::TRANSACTION_READ_COMMITTED;
} }
/* supports*() metods */ /* supports*() metods */
@ -1865,4 +1864,4 @@ abstract class AbstractPlatform
{ {
return 'TRUNCATE '.$tableName; return 'TRUNCATE '.$tableName;
} }
} }

View File

@ -146,12 +146,12 @@ class OraclePlatform extends AbstractPlatform
protected function _getTransactionIsolationLevelSQL($level) protected function _getTransactionIsolationLevelSQL($level)
{ {
switch ($level) { switch ($level) {
case \Doctrine\DBAL\Transaction::READ_UNCOMMITTED: case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
return 'READ UNCOMMITTED'; return 'READ UNCOMMITTED';
case \Doctrine\DBAL\Transaction::READ_COMMITTED: case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
return 'READ COMMITTED'; return 'READ COMMITTED';
case \Doctrine\DBAL\Transaction::REPEATABLE_READ: case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
case \Doctrine\DBAL\Transaction::SERIALIZABLE: case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
return 'SERIALIZABLE'; return 'SERIALIZABLE';
default: default:
return parent::_getTransactionIsolationLevelSQL($level); return parent::_getTransactionIsolationLevelSQL($level);

View File

@ -130,11 +130,11 @@ class SqlitePlatform extends AbstractPlatform
protected function _getTransactionIsolationLevelSQL($level) protected function _getTransactionIsolationLevelSQL($level)
{ {
switch ($level) { switch ($level) {
case \Doctrine\DBAL\Transaction::READ_UNCOMMITTED: case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
return 0; return 0;
case \Doctrine\DBAL\Transaction::READ_COMMITTED: case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
case \Doctrine\DBAL\Transaction::REPEATABLE_READ: case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
case \Doctrine\DBAL\Transaction::SERIALIZABLE: case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
return 1; return 1;
default: default:
return parent::_getTransactionIsolationLevelSQL($level); return parent::_getTransactionIsolationLevelSQL($level);

View File

@ -1,229 +0,0 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL;
/**
* The Transaction class is the central access point to DBAL Transaction functionality.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Transaction
{
/**
* Constant for transaction isolation level READ UNCOMMITTED.
*/
const READ_UNCOMMITTED = 1;
/**
* Constant for transaction isolation level READ COMMITTED.
*/
const READ_COMMITTED = 2;
/**
* Constant for transaction isolation level REPEATABLE READ.
*/
const REPEATABLE_READ = 3;
/**
* Constant for transaction isolation level SERIALIZABLE.
*/
const SERIALIZABLE = 4;
/**
* The transaction nesting level.
*
* @var integer
*/
private $_transactionNestingLevel = 0;
/**
* The currently active transaction isolation level.
*
* @var integer
*/
private $_transactionIsolationLevel;
/**
* Flag that indicates whether the current transaction is marked for rollback only.
*
* @var boolean
*/
private $_isRollbackOnly = false;
/**
* Constructor
*
* @param Connection $conn The DBAL Connection
*/
public function __construct(Connection $conn)
{
$this->_conn = $conn;
$this->_transactionIsolationLevel = $conn->getDatabasePlatform()->getDefaultTransactionIsolationLevel();
}
/**
* Checks whether a transaction is currently active.
*
* @return boolean TRUE if a transaction is currently active, FALSE otherwise.
*/
public function isTransactionActive()
{
return $this->_transactionNestingLevel > 0;
}
/**
* Sets the transaction isolation level.
*
* @param integer $level The level to set.
*/
public function setTransactionIsolation($level)
{
$this->_transactionIsolationLevel = $level;
return $this->executeUpdate($this->_conn->getDatabasePlatform()->getSetTransactionIsolationSQL($level));
}
/**
* Gets the currently active transaction isolation level.
*
* @return integer The current transaction isolation level.
*/
public function getTransactionIsolation()
{
return $this->_transactionIsolationLevel;
}
/**
* Returns the current transaction nesting level.
*
* @return integer The nesting level. A value of 0 means there's no active transaction.
*/
public function getTransactionNestingLevel()
{
return $this->_transactionNestingLevel;
}
/**
* Starts a transaction by suspending auto-commit mode.
*
* @return void
*/
public function begin()
{
$conn = $this->_conn->getWrappedConnection();
if ($this->_transactionNestingLevel == 0) {
$conn->beginTransaction();
}
++$this->_transactionNestingLevel;
}
/**
* Commits the current transaction.
*
* @return void
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
*/
public function commit()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::commitFailedNoActiveTransaction();
}
if ($this->_isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly();
}
$conn = $this->_conn->getWrappedConnection();
if ($this->_transactionNestingLevel == 1) {
$conn->commit();
}
--$this->_transactionNestingLevel;
}
/**
* Cancel any database changes done during the current transaction.
*
* this method can be listened with onPreTransactionRollback and onTransactionRollback
* eventlistener methods
*
* @throws ConnectionException If the rollback operation failed.
*/
public function rollback()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::rollbackFailedNoActiveTransaction();
}
if ($this->_transactionNestingLevel == 1) {
$this->_transactionNestingLevel = 0;
$this->_conn->getWrappedConnection()->rollback();
$this->_isRollbackOnly = false;
} else {
$this->_isRollbackOnly = true;
--$this->_transactionNestingLevel;
}
}
/**
* Marks the current transaction so that the only possible
* outcome for the transaction to be rolled back.
*
* @throws ConnectionException If no transaction is active.
*/
public function setRollbackOnly()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::noActiveTransaction();
}
$this->_isRollbackOnly = true;
}
/**
* Check whether the current transaction is marked for rollback only.
*
* @return boolean
* @throws ConnectionException If no transaction is active.
*/
public function getRollbackOnly()
{
if ($this->_transactionNestingLevel == 0) {
throw ConnectionException::noActiveTransaction();
}
return $this->_isRollbackOnly;
}
}

View File

@ -134,7 +134,7 @@ class EntityManager
$config->getProxyDir(), $config->getProxyDir(),
$config->getProxyNamespace(), $config->getProxyNamespace(),
$config->getAutoGenerateProxyClasses()); $config->getAutoGenerateProxyClasses());
$this->_transaction = new EntityTransaction($conn->getTransaction()); $this->_transaction = new EntityTransaction($this);
} }
/** /**
@ -164,7 +164,7 @@ class EntityManager
*/ */
public function getTransaction() public function getTransaction()
{ {
return $this->_transaction; return $this->_transaction;
} }
/** /**
@ -207,11 +207,12 @@ class EntityManager
/** /**
* Performs a rollback on the underlying database connection and closes the * Performs a rollback on the underlying database connection and closes the
* EntityManager as it may now be in a corrupted state. * EntityManager as it may now be in a corrupted state.
*
* @return boolean TRUE on success, FALSE on failure
*/ */
public function rollback() public function rollback()
{ {
$this->getTransaction()->rollback(); return $this->getTransaction()->rollback();
$this->close();
} }
/** /**

View File

@ -25,7 +25,6 @@ use Doctrine\DBAL\Transaction;
/** /**
* The Transaction class is the central access point to ORM Transaction functionality. * The Transaction class is the central access point to ORM Transaction functionality.
* This class acts more as a delegate class to the DBAL Transaction functionality.
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
@ -36,23 +35,31 @@ use Doctrine\DBAL\Transaction;
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class EntityTransaction final class EntityTransaction
{ {
/** /**
* The wrapped DBAL Transaction. * The wrapped ORM EntityManager.
* *
* @var Doctrine\DBAL\Transaction * @var Doctrine\ORM\EntityManager
*/ */
protected $_wrappedTransaction; private $_em;
/**
* The database connection used by the EntityManager.
*
* @var Doctrine\DBAL\Connection
*/
private $_conn;
/** /**
* Constructor. * Constructor.
* *
* @param Transaction $transaction * @param Transaction $transaction
*/ */
public function __construct(Transaction $transaction) public function __construct(EntityManager $em)
{ {
$this->_wrappedTransaction = $transaction; $this->_em = $em;
$this->_conn = $em->getConnection();
} }
/** /**
@ -62,7 +69,7 @@ class EntityTransaction
*/ */
public function isTransactionActive() public function isTransactionActive()
{ {
return $this->_wrappedTransaction->isTransactionActive(); return $this->_conn->isTransactionActive();
} }
/** /**
@ -72,7 +79,7 @@ class EntityTransaction
*/ */
public function setTransactionIsolation($level) public function setTransactionIsolation($level)
{ {
return $this->_wrappedTransaction->setTransactionIsolation($level); return $this->_conn->setTransactionIsolation($level);
} }
/** /**
@ -82,7 +89,7 @@ class EntityTransaction
*/ */
public function getTransactionIsolation() public function getTransactionIsolation()
{ {
return $this->_wrappedTransaction->getTransactionIsolation(); return $this->_conn->getTransactionIsolation();
} }
/** /**
@ -92,7 +99,7 @@ class EntityTransaction
*/ */
public function getTransactionNestingLevel() public function getTransactionNestingLevel()
{ {
return $this->_wrappedTransaction->getTransactionNestingLevel(); return $this->_conn->getTransactionNestingLevel();
} }
/** /**
@ -102,7 +109,7 @@ class EntityTransaction
*/ */
public function begin() public function begin()
{ {
$this->_wrappedTransaction->begin(); $this->_conn->beginTransaction();
} }
/** /**
@ -114,41 +121,43 @@ class EntityTransaction
*/ */
public function commit() public function commit()
{ {
$this->_wrappedTransaction->commit(); $this->_conn->commit();
} }
/** /**
* Cancel any database changes done during the current transaction. * Cancel any database changes done during the current transaction.
* *
* this method can be listened with onPreTransactionRollback and onTransactionRollback * this method can be listened with onPreTransactionRollback and onTransactionRollback
* eventlistener methods * event listener methods
* *
* @return boolean TRUE on success, FALSE on failure
* @throws Doctrine\DBAL\ConnectionException If the rollback operation failed. * @throws Doctrine\DBAL\ConnectionException If the rollback operation failed.
*/ */
public function rollback() public function rollback()
{ {
$this->_wrappedTransaction->rollback(); $this->_em->close();
return $this->_conn->rollback();
} }
/** /**
* Marks the current transaction so that the only possible * Marks the current transaction so that the only possible
* outcome for the transaction to be rolled back. * outcome for the transaction to be rolled back.
* *
* @throws ConnectionException If no transaction is active. * @throws Doctrine\DBAL\ConnectionException If no transaction is active.
*/ */
public function setRollbackOnly() public function setRollbackOnly()
{ {
$this->_wrappedTransaction->setRollbackOnly(); $this->_conn->setRollbackOnly();
} }
/** /**
* Check whether the current transaction is marked for rollback only. * Check whether the current transaction is marked for rollback only.
* *
* @return boolean * @return boolean
* @throws ConnectionException If no transaction is active. * @throws Doctrine\DBAL\ConnectionException If no transaction is active.
*/ */
public function getRollbackOnly() public function isRollbackOnly()
{ {
return $this->_wrappedTransaction->getRollbackOnly(); return $this->_conn->getRollbackOnly();
} }
} }

View File

@ -278,7 +278,7 @@ class UnitOfWork implements PropertyChangedListener
$commitOrder = $this->_getCommitOrder(); $commitOrder = $this->_getCommitOrder();
$tx = $this->_em->getTransaction(); $tx = $this->_em->getTransaction();
try { try {
$tx->begin(); $tx->begin();

View File

@ -26,7 +26,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\PostgreSqlSchemaManagerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\PostgreSqlSchemaManagerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\OracleSchemaManagerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\OracleSchemaManagerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\Db2SchemaManagerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\Db2SchemaManagerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\TransactionTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Functional\ConnectionTest');
return $suite; return $suite;
} }

View File

@ -0,0 +1,64 @@
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\ConnectionException;
require_once __DIR__ . '/../../TestInit.php';
class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function testTransactionNestingBehavior()
{
try {
$this->_conn->beginTransaction();
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
try {
$this->_conn->beginTransaction();
$this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
throw new \Exception;
$this->_conn->commit(); // never reached
} catch (\Exception $e) {
$this->_conn->rollback();
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
//no rethrow
}
$this->assertTrue($this->_conn->getRollbackOnly());
$this->_conn->commit(); // should throw exception
$this->fail('Transaction commit after failed nested transaction should fail.');
} catch (ConnectionException $e) {
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
$this->_conn->rollback();
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
}
public function testTransactionBehavior()
{
try {
$this->_conn->beginTransaction();
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
throw new \Exception;
$this->_connx->commit(); // never reached
} catch (\Exception $e) {
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
$this->_conn->rollback();
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
try {
$this->_conn->beginTransaction();
$this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
$this->_conn->commit();
} catch (\Exception $e) {
$this->_conn->rollback();
$this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
}
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\ConnectionException;
require_once __DIR__ . '/../../TestInit.php';
class TransactionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
public function testTransactionNestingBehavior()
{
$tx = $this->_conn->getTransaction();
try {
$tx->begin();
$this->assertEquals(1, $tx->getTransactionNestingLevel());
try {
$tx->begin();
$this->assertEquals(2, $tx->getTransactionNestingLevel());
throw new \Exception;
$tx->commit(); // never reached
} catch (\Exception $e) {
$tx->rollback();
$this->assertEquals(1, $tx->getTransactionNestingLevel());
//no rethrow
}
$this->assertTrue($tx->getRollbackOnly());
$tx->commit(); // should throw exception
$this->fail('Transaction commit after failed nested transaction should fail.');
} catch (ConnectionException $e) {
$this->assertEquals(1, $tx->getTransactionNestingLevel());
$tx->rollback();
$this->assertEquals(0, $tx->getTransactionNestingLevel());
}
}
public function testTransactionBehavior()
{
$tx = $this->_conn->getTransaction();
try {
$tx->begin();
$this->assertEquals(1, $tx->getTransactionNestingLevel());
throw new \Exception;
$tx->commit(); // never reached
} catch (\Exception $e) {
$this->assertEquals(1, $tx->getTransactionNestingLevel());
$tx->rollback();
$this->assertEquals(0, $tx->getTransactionNestingLevel());
}
try {
$tx->begin();
$this->assertEquals(1, $tx->getTransactionNestingLevel());
$tx->commit();
} catch (\Exception $e) {
$tx->rollback();
$this->assertEquals(0, $tx->getTransactionNestingLevel());
}
}
}

View File

@ -44,19 +44,19 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
{ {
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_UNCOMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ COMMITTED', 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_COMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ', 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::REPEATABLE_READ) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::SERIALIZABLE) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
); );
} }

View File

@ -53,20 +53,20 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
{ {
$this->assertEquals( $this->assertEquals(
'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_UNCOMMITTED), $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
'' ''
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_COMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ', 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::REPEATABLE_READ) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::SERIALIZABLE) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
); );
} }

View File

@ -55,19 +55,19 @@ class OraclePlatformTest extends AbstractPlatformTestCase
{ {
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_UNCOMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ COMMITTED', 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_COMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::REPEATABLE_READ) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
); );
$this->assertEquals( $this->assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::SERIALIZABLE) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
); );
} }

View File

@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Transaction;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
@ -74,19 +73,19 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{ {
$this->assertEquals( $this->assertEquals(
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED', 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
$this->_platform->getSetTransactionIsolationSQL(Transaction::READ_UNCOMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED', 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED',
$this->_platform->getSetTransactionIsolationSQL(Transaction::READ_COMMITTED) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ', 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ',
$this->_platform->getSetTransactionIsolationSQL(Transaction::REPEATABLE_READ) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
); );
$this->assertEquals( $this->assertEquals(
'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->_platform->getSetTransactionIsolationSQL(Transaction::SERIALIZABLE) $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
); );
} }

View File

@ -36,10 +36,22 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
public function testGeneratesTransactionCommands() public function testGeneratesTransactionCommands()
{ {
$this->assertEquals('PRAGMA read_uncommitted = 0', $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_UNCOMMITTED)); $this->assertEquals(
$this->assertEquals('PRAGMA read_uncommitted = 1', $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::READ_COMMITTED)); 'PRAGMA read_uncommitted = 0',
$this->assertEquals('PRAGMA read_uncommitted = 1', $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::REPEATABLE_READ)); $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
$this->assertEquals('PRAGMA read_uncommitted = 1', $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Transaction::SERIALIZABLE)); );
$this->assertEquals(
'PRAGMA read_uncommitted = 1',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
);
$this->assertEquals(
'PRAGMA read_uncommitted = 1',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
);
$this->assertEquals(
'PRAGMA read_uncommitted = 1',
$this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
);
} }
public function testPrefersIdentityColumns() public function testPrefersIdentityColumns()