Skip to content

Commit 045fbe8

Browse files
Ocramiusmorozov
authored andcommitted
Merge pull request #3480 from morozov/transaction-void
Transaction-related Statement methods return void
2 parents bcb17e0 + 21905ad commit 045fbe8

File tree

14 files changed

+96
-147
lines changed

14 files changed

+96
-147
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK Transaction-related `Statement` methods return `void`.
4+
5+
`Statement::beginTransaction()`, `::commit()` and `::rollBack()` no longer return a boolean value. They will throw a `DriverException` in case of failure.
6+
37
## MINOR BC BREAK `Statement::fetchColumn()` with an invalid index.
48

59
Similarly to `PDOStatement::fetchColumn()`, DBAL statements throw an exception in case of an invalid column index.

lib/Doctrine/DBAL/Connection.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Cache\QueryCacheProfile;
1010
use Doctrine\DBAL\Cache\ResultCacheStatement;
1111
use Doctrine\DBAL\Driver\Connection as DriverConnection;
12+
use Doctrine\DBAL\Driver\DriverException;
1213
use Doctrine\DBAL\Driver\PingableConnection;
1314
use Doctrine\DBAL\Driver\ResultStatement;
1415
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
@@ -502,12 +503,11 @@ public function isAutoCommit()
502503
*
503504
* @see isAutoCommit
504505
*
505-
* @param bool $autoCommit True to enable auto-commit mode; false to disable it.
506+
* @throws ConnectionException
507+
* @throws DriverException
506508
*/
507-
public function setAutoCommit($autoCommit)
509+
public function setAutoCommit(bool $autoCommit) : void
508510
{
509-
$autoCommit = (bool) $autoCommit;
510-
511511
// Mode not changed, no-op.
512512
if ($autoCommit === $this->autoCommit) {
513513
return;
@@ -1191,7 +1191,7 @@ protected function _getNestedTransactionSavePointName()
11911191
/**
11921192
* {@inheritDoc}
11931193
*/
1194-
public function beginTransaction()
1194+
public function beginTransaction() : void
11951195
{
11961196
$connection = $this->getWrappedConnection();
11971197

@@ -1201,15 +1201,17 @@ public function beginTransaction()
12011201

12021202
if ($this->transactionNestingLevel === 1) {
12031203
$logger->startQuery('"START TRANSACTION"');
1204-
$connection->beginTransaction();
1205-
$logger->stopQuery();
1204+
1205+
try {
1206+
$connection->beginTransaction();
1207+
} finally {
1208+
$logger->stopQuery();
1209+
}
12061210
} elseif ($this->nestTransactionsWithSavepoints) {
12071211
$logger->startQuery('"SAVEPOINT"');
12081212
$this->createSavepoint($this->_getNestedTransactionSavePointName());
12091213
$logger->stopQuery();
12101214
}
1211-
1212-
return true;
12131215
}
12141216

12151217
/**
@@ -1218,11 +1220,12 @@ public function beginTransaction()
12181220
* @throws ConnectionException If the commit failed due to no active transaction or
12191221
* because the transaction was marked for rollback only.
12201222
*/
1221-
public function commit()
1223+
public function commit() : void
12221224
{
12231225
if ($this->transactionNestingLevel === 0) {
12241226
throw ConnectionException::noActiveTransaction();
12251227
}
1228+
12261229
if ($this->isRollbackOnly) {
12271230
throw ConnectionException::commitFailedRollbackOnly();
12281231
}
@@ -1235,8 +1238,12 @@ public function commit()
12351238

12361239
if ($this->transactionNestingLevel === 1) {
12371240
$logger->startQuery('"COMMIT"');
1238-
$result = $connection->commit();
1239-
$logger->stopQuery();
1241+
1242+
try {
1243+
$connection->commit();
1244+
} finally {
1245+
$logger->stopQuery();
1246+
}
12401247
} elseif ($this->nestTransactionsWithSavepoints) {
12411248
$logger->startQuery('"RELEASE SAVEPOINT"');
12421249
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
@@ -1246,18 +1253,19 @@ public function commit()
12461253
--$this->transactionNestingLevel;
12471254

12481255
if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
1249-
return $result;
1256+
return;
12501257
}
12511258

12521259
$this->beginTransaction();
1253-
1254-
return $result;
12551260
}
12561261

12571262
/**
12581263
* Commits all current nesting transactions.
1264+
*
1265+
* @throws ConnectionException
1266+
* @throws DriverException
12591267
*/
1260-
private function commitAll()
1268+
private function commitAll() : void
12611269
{
12621270
while ($this->transactionNestingLevel !== 0) {
12631271
if ($this->autoCommit === false && $this->transactionNestingLevel === 1) {
@@ -1273,11 +1281,11 @@ private function commitAll()
12731281
}
12741282

12751283
/**
1276-
* Cancels any database changes done during the current transaction.
1284+
* {@inheritDoc}
12771285
*
12781286
* @throws ConnectionException If the rollback operation failed.
12791287
*/
1280-
public function rollBack()
1288+
public function rollBack() : void
12811289
{
12821290
if ($this->transactionNestingLevel === 0) {
12831291
throw ConnectionException::noActiveTransaction();
@@ -1290,12 +1298,16 @@ public function rollBack()
12901298
if ($this->transactionNestingLevel === 1) {
12911299
$logger->startQuery('"ROLLBACK"');
12921300
$this->transactionNestingLevel = 0;
1293-
$connection->rollBack();
1294-
$this->isRollbackOnly = false;
1295-
$logger->stopQuery();
12961301

1297-
if ($this->autoCommit === false) {
1298-
$this->beginTransaction();
1302+
try {
1303+
$connection->rollBack();
1304+
} finally {
1305+
$this->isRollbackOnly = false;
1306+
$logger->stopQuery();
1307+
1308+
if ($this->autoCommit === false) {
1309+
$this->beginTransaction();
1310+
}
12991311
}
13001312
} elseif ($this->nestTransactionsWithSavepoints) {
13011313
$logger->startQuery('"ROLLBACK TO SAVEPOINT"');

lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,31 +230,31 @@ public function executeUpdate(string $query, array $params = [], array $types =
230230
/**
231231
* {@inheritDoc}
232232
*/
233-
public function beginTransaction()
233+
public function beginTransaction() : void
234234
{
235235
$this->connect('master');
236236

237-
return parent::beginTransaction();
237+
parent::beginTransaction();
238238
}
239239

240240
/**
241241
* {@inheritDoc}
242242
*/
243-
public function commit()
243+
public function commit() : void
244244
{
245245
$this->connect('master');
246246

247-
return parent::commit();
247+
parent::commit();
248248
}
249249

250250
/**
251251
* {@inheritDoc}
252252
*/
253-
public function rollBack()
253+
public function rollBack() : void
254254
{
255255
$this->connect('master');
256256

257-
return parent::rollBack();
257+
parent::rollBack();
258258
}
259259

260260
/**

lib/Doctrine/DBAL/Driver/Connection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ public function lastInsertId($name = null);
5454
/**
5555
* Initiates a transaction.
5656
*
57-
* @return bool TRUE on success or FALSE on failure.
57+
* @throws DriverException
5858
*/
59-
public function beginTransaction();
59+
public function beginTransaction() : void;
6060

6161
/**
6262
* Commits a transaction.
6363
*
64-
* @return bool TRUE on success or FALSE on failure.
64+
* @throws DriverException
6565
*/
66-
public function commit();
66+
public function commit() : void;
6767

6868
/**
6969
* Rolls back the current transaction, as initiated by beginTransaction().
7070
*
71-
* @return bool TRUE on success or FALSE on failure.
71+
* @throws DriverException
7272
*/
73-
public function rollBack();
73+
public function rollBack() : void;
7474

7575
/**
7676
* Returns the error code associated with the last operation on the database handle.

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,39 @@ public function lastInsertId($name = null)
137137
/**
138138
* {@inheritdoc}
139139
*/
140-
public function beginTransaction()
140+
public function beginTransaction() : void
141141
{
142-
db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
142+
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) {
143+
throw new DB2Exception(db2_conn_errormsg($this->conn));
144+
}
143145
}
144146

145147
/**
146148
* {@inheritdoc}
147149
*/
148-
public function commit()
150+
public function commit() : void
149151
{
150152
if (! db2_commit($this->conn)) {
151153
throw new DB2Exception(db2_conn_errormsg($this->conn));
152154
}
153-
db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
155+
156+
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
157+
throw new DB2Exception(db2_conn_errormsg($this->conn));
158+
}
154159
}
155160

156161
/**
157162
* {@inheritdoc}
158163
*/
159-
public function rollBack()
164+
public function rollBack() : void
160165
{
161166
if (! db2_rollback($this->conn)) {
162167
throw new DB2Exception(db2_conn_errormsg($this->conn));
163168
}
164-
db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
169+
170+
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) {
171+
throw new DB2Exception(db2_conn_errormsg($this->conn));
172+
}
165173
}
166174

167175
/**

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Doctrine\DBAL\Driver\IBMDB2;
44

5-
use Exception;
5+
use Doctrine\DBAL\Driver\AbstractDriverException;
66

7-
class DB2Exception extends Exception
7+
class DB2Exception extends AbstractDriverException
88
{
99
}

lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,27 +174,29 @@ public function lastInsertId($name = null)
174174
/**
175175
* {@inheritdoc}
176176
*/
177-
public function beginTransaction()
177+
public function beginTransaction() : void
178178
{
179179
$this->conn->query('START TRANSACTION');
180-
181-
return true;
182180
}
183181

184182
/**
185183
* {@inheritdoc}
186184
*/
187-
public function commit()
185+
public function commit() : void
188186
{
189-
return $this->conn->commit();
187+
if (! $this->conn->commit()) {
188+
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
189+
}
190190
}
191191

192192
/**
193-
* {@inheritdoc}non-PHPdoc)
193+
* {@inheritdoc}
194194
*/
195-
public function rollBack()
195+
public function rollBack() : void
196196
{
197-
return $this->conn->rollback();
197+
if (! $this->conn->rollback()) {
198+
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
199+
}
198200
}
199201

200202
/**

lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,37 +177,33 @@ public function getExecuteMode()
177177
/**
178178
* {@inheritdoc}
179179
*/
180-
public function beginTransaction()
180+
public function beginTransaction() : void
181181
{
182182
$this->executeMode = OCI_NO_AUTO_COMMIT;
183-
184-
return true;
185183
}
186184

187185
/**
188186
* {@inheritdoc}
189187
*/
190-
public function commit()
188+
public function commit() : void
191189
{
192190
if (! oci_commit($this->dbh)) {
193191
throw OCI8Exception::fromErrorInfo($this->errorInfo());
194192
}
195-
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
196193

197-
return true;
194+
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
198195
}
199196

200197
/**
201198
* {@inheritdoc}
202199
*/
203-
public function rollBack()
200+
public function rollBack() : void
204201
{
205202
if (! oci_rollback($this->dbh)) {
206203
throw OCI8Exception::fromErrorInfo($this->errorInfo());
207204
}
208-
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
209205

210-
return true;
206+
$this->executeMode = OCI_COMMIT_ON_SUCCESS;
211207
}
212208

213209
/**

lib/Doctrine/DBAL/Driver/PDOConnection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,25 @@ protected function createStatement(\PDOStatement $stmt) : PDOStatement
126126
/**
127127
* {@inheritDoc}
128128
*/
129-
public function beginTransaction()
129+
public function beginTransaction() : void
130130
{
131-
return $this->connection->beginTransaction();
131+
$this->connection->beginTransaction();
132132
}
133133

134134
/**
135135
* {@inheritDoc}
136136
*/
137-
public function commit()
137+
public function commit() : void
138138
{
139-
return $this->connection->commit();
139+
$this->connection->commit();
140140
}
141141

142142
/**
143143
* {@inheritDoc}
144144
*/
145-
public function rollBack()
145+
public function rollBack() : void
146146
{
147-
return $this->connection->rollBack();
147+
$this->connection->rollBack();
148148
}
149149

150150
/**

0 commit comments

Comments
 (0)