Skip to content

Commit

Permalink
Merge pull request #3480 from morozov/transaction-void
Browse files Browse the repository at this point in the history
Transaction-related Statement methods return void
  • Loading branch information
Ocramius authored and morozov committed Mar 15, 2019
2 parents c0f1b8f + a89da2d commit 322dc7c
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 85 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 3.0

## BC BREAK Transaction-related `Statement` methods return `void`.

`Statement::beginTransaction()`, `::commit()` and `::rollBack()` no longer return a boolean value. They will throw a `DriverException` in case of failure.

## MINOR BC BREAK `Statement::fetchColumn()` with an invalid index.

Similarly to `PDOStatement::fetchColumn()`, DBAL statements throw an exception in case of an invalid column index.
Expand Down
58 changes: 35 additions & 23 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
Expand Down Expand Up @@ -493,12 +494,11 @@ public function isAutoCommit()
*
* @see isAutoCommit
*
* @param bool $autoCommit True to enable auto-commit mode; false to disable it.
* @throws ConnectionException
* @throws DriverException
*/
public function setAutoCommit($autoCommit)
public function setAutoCommit(bool $autoCommit) : void
{
$autoCommit = (bool) $autoCommit;

// Mode not changed, no-op.
if ($autoCommit === $this->autoCommit) {
return;
Expand Down Expand Up @@ -1178,7 +1178,7 @@ protected function _getNestedTransactionSavePointName()
/**
* {@inheritDoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
$connection = $this->getWrappedConnection();

Expand All @@ -1188,15 +1188,17 @@ public function beginTransaction()

if ($this->transactionNestingLevel === 1) {
$logger->startQuery('"START TRANSACTION"');
$connection->beginTransaction();
$logger->stopQuery();

try {
$connection->beginTransaction();
} finally {
$logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) {
$logger->startQuery('"SAVEPOINT"');
$this->createSavepoint($this->_getNestedTransactionSavePointName());
$logger->stopQuery();
}

return true;
}

/**
Expand All @@ -1205,11 +1207,12 @@ public function beginTransaction()
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
*/
public function commit()
public function commit() : void
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
}

if ($this->isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly();
}
Expand All @@ -1220,8 +1223,12 @@ public function commit()

if ($this->transactionNestingLevel === 1) {
$logger->startQuery('"COMMIT"');
$connection->commit();
$logger->stopQuery();

try {
$connection->commit();
} finally {
$logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) {
$logger->startQuery('"RELEASE SAVEPOINT"');
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
Expand All @@ -1231,18 +1238,19 @@ public function commit()
--$this->transactionNestingLevel;

if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
return true;
return;
}

$this->beginTransaction();

return true;
}

/**
* Commits all current nesting transactions.
*
* @throws ConnectionException
* @throws DriverException
*/
private function commitAll()
private function commitAll() : void
{
while ($this->transactionNestingLevel !== 0) {
if ($this->autoCommit === false && $this->transactionNestingLevel === 1) {
Expand All @@ -1258,11 +1266,11 @@ private function commitAll()
}

/**
* Cancels any database changes done during the current transaction.
* {@inheritDoc}
*
* @throws ConnectionException If the rollback operation failed.
*/
public function rollBack()
public function rollBack() : void
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
Expand All @@ -1275,12 +1283,16 @@ public function rollBack()
if ($this->transactionNestingLevel === 1) {
$logger->startQuery('"ROLLBACK"');
$this->transactionNestingLevel = 0;
$connection->rollBack();
$this->isRollbackOnly = false;
$logger->stopQuery();

if ($this->autoCommit === false) {
$this->beginTransaction();
try {
$connection->rollBack();
} finally {
$this->isRollbackOnly = false;
$logger->stopQuery();

if ($this->autoCommit === false) {
$this->beginTransaction();
}
}
} elseif ($this->nestTransactionsWithSavepoints) {
$logger->startQuery('"ROLLBACK TO SAVEPOINT"');
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,31 +230,31 @@ public function executeUpdate(string $query, array $params = [], array $types =
/**
* {@inheritDoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
$this->connect('master');

return parent::beginTransaction();
parent::beginTransaction();
}

/**
* {@inheritDoc}
*/
public function commit()
public function commit() : void
{
$this->connect('master');

return parent::commit();
parent::commit();
}

/**
* {@inheritDoc}
*/
public function rollBack()
public function rollBack() : void
{
$this->connect('master');

return parent::rollBack();
parent::rollBack();
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/DBAL/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public function lastInsertId($name = null);
/**
* Initiates a transaction.
*
* @return bool TRUE on success or FALSE on failure.
* @throws DriverException
*/
public function beginTransaction();
public function beginTransaction() : void;

/**
* Commits a transaction.
*
* @return bool TRUE on success or FALSE on failure.
* @throws DriverException
*/
public function commit();
public function commit() : void;

/**
* Rolls back the current transaction, as initiated by beginTransaction().
*
* @return bool TRUE on success or FALSE on failure.
* @throws DriverException
*/
public function rollBack();
public function rollBack() : void;

/**
* Returns the error code associated with the last operation on the database handle.
Expand Down
20 changes: 14 additions & 6 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,39 @@ public function lastInsertId($name = null)
/**
* {@inheritdoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
throw new DB2Exception(db2_conn_errormsg($this->conn));
}
}

/**
* {@inheritdoc}
*/
public function commit()
public function commit() : void
{
if (! db2_commit($this->conn)) {
throw new DB2Exception(db2_conn_errormsg($this->conn));
}
db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);

if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
throw new DB2Exception(db2_conn_errormsg($this->conn));
}
}

/**
* {@inheritdoc}
*/
public function rollBack()
public function rollBack() : void
{
if (! db2_rollback($this->conn)) {
throw new DB2Exception(db2_conn_errormsg($this->conn));
}
db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);

if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
throw new DB2Exception(db2_conn_errormsg($this->conn));
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Doctrine\DBAL\Driver\IBMDB2;

use Exception;
use Doctrine\DBAL\Driver\AbstractDriverException;

class DB2Exception extends Exception
class DB2Exception extends AbstractDriverException
{
}
18 changes: 10 additions & 8 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,29 @@ public function lastInsertId($name = null)
/**
* {@inheritdoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
$this->conn->query('START TRANSACTION');

return true;
}

/**
* {@inheritdoc}
*/
public function commit()
public function commit() : void
{
return $this->conn->commit();
if (! $this->conn->commit()) {
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
}
}

/**
* {@inheritdoc}non-PHPdoc)
* {@inheritdoc}
*/
public function rollBack()
public function rollBack() : void
{
return $this->conn->rollback();
if (! $this->conn->rollback()) {
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
}
}

/**
Expand Down
14 changes: 5 additions & 9 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,37 +177,33 @@ public function getExecuteMode()
/**
* {@inheritdoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
$this->executeMode = OCI_NO_AUTO_COMMIT;

return true;
}

/**
* {@inheritdoc}
*/
public function commit()
public function commit() : void
{
if (! oci_commit($this->dbh)) {
throw OCI8Exception::fromErrorInfo($this->errorInfo());
}
$this->executeMode = OCI_COMMIT_ON_SUCCESS;

return true;
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
}

/**
* {@inheritdoc}
*/
public function rollBack()
public function rollBack() : void
{
if (! oci_rollback($this->dbh)) {
throw OCI8Exception::fromErrorInfo($this->errorInfo());
}
$this->executeMode = OCI_COMMIT_ON_SUCCESS;

return true;
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,25 @@ protected function createStatement(\PDOStatement $stmt) : PDOStatement
/**
* {@inheritDoc}
*/
public function beginTransaction()
public function beginTransaction() : void
{
return $this->connection->beginTransaction();
$this->connection->beginTransaction();
}

/**
* {@inheritDoc}
*/
public function commit()
public function commit() : void
{
return $this->connection->commit();
$this->connection->commit();
}

/**
* {@inheritDoc}
*/
public function rollBack()
public function rollBack() : void
{
return $this->connection->rollBack();
$this->connection->rollBack();
}

/**
Expand Down
Loading

0 comments on commit 322dc7c

Please sign in to comment.