Skip to content

Commit 79ba83d

Browse files
Ocramiusmorozov
authored andcommitted
Merge pull request #3480 from morozov/transaction-void
Transaction-related Statement methods return void
2 parents 2a0fdf3 + 306587c commit 79ba83d

File tree

14 files changed

+101
-111
lines changed

14 files changed

+101
-111
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;
@@ -492,12 +493,11 @@ public function isAutoCommit()
492493
*
493494
* @see isAutoCommit
494495
*
495-
* @param bool $autoCommit True to enable auto-commit mode; false to disable it.
496+
* @throws ConnectionException
497+
* @throws DriverException
496498
*/
497-
public function setAutoCommit($autoCommit)
499+
public function setAutoCommit(bool $autoCommit) : void
498500
{
499-
$autoCommit = (bool) $autoCommit;
500-
501501
// Mode not changed, no-op.
502502
if ($autoCommit === $this->autoCommit) {
503503
return;
@@ -1181,7 +1181,7 @@ protected function _getNestedTransactionSavePointName()
11811181
/**
11821182
* {@inheritDoc}
11831183
*/
1184-
public function beginTransaction()
1184+
public function beginTransaction() : void
11851185
{
11861186
$connection = $this->getWrappedConnection();
11871187

@@ -1191,15 +1191,17 @@ public function beginTransaction()
11911191

11921192
if ($this->transactionNestingLevel === 1) {
11931193
$logger->startQuery('"START TRANSACTION"');
1194-
$connection->beginTransaction();
1195-
$logger->stopQuery();
1194+
1195+
try {
1196+
$connection->beginTransaction();
1197+
} finally {
1198+
$logger->stopQuery();
1199+
}
11961200
} elseif ($this->nestTransactionsWithSavepoints) {
11971201
$logger->startQuery('"SAVEPOINT"');
11981202
$this->createSavepoint($this->_getNestedTransactionSavePointName());
11991203
$logger->stopQuery();
12001204
}
1201-
1202-
return true;
12031205
}
12041206

12051207
/**
@@ -1208,11 +1210,12 @@ public function beginTransaction()
12081210
* @throws ConnectionException If the commit failed due to no active transaction or
12091211
* because the transaction was marked for rollback only.
12101212
*/
1211-
public function commit()
1213+
public function commit() : void
12121214
{
12131215
if ($this->transactionNestingLevel === 0) {
12141216
throw ConnectionException::noActiveTransaction();
12151217
}
1218+
12161219
if ($this->isRollbackOnly) {
12171220
throw ConnectionException::commitFailedRollbackOnly();
12181221
}
@@ -1225,8 +1228,12 @@ public function commit()
12251228

12261229
if ($this->transactionNestingLevel === 1) {
12271230
$logger->startQuery('"COMMIT"');
1228-
$result = $connection->commit();
1229-
$logger->stopQuery();
1231+
1232+
try {
1233+
$connection->commit();
1234+
} finally {
1235+
$logger->stopQuery();
1236+
}
12301237
} elseif ($this->nestTransactionsWithSavepoints) {
12311238
$logger->startQuery('"RELEASE SAVEPOINT"');
12321239
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
@@ -1236,18 +1243,19 @@ public function commit()
12361243
--$this->transactionNestingLevel;
12371244

12381245
if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
1239-
return $result;
1246+
return;
12401247
}
12411248

12421249
$this->beginTransaction();
1243-
1244-
return $result;
12451250
}
12461251

12471252
/**
12481253
* Commits all current nesting transactions.
1254+
*
1255+
* @throws ConnectionException
1256+
* @throws DriverException
12491257
*/
1250-
private function commitAll()
1258+
private function commitAll() : void
12511259
{
12521260
while ($this->transactionNestingLevel !== 0) {
12531261
if ($this->autoCommit === false && $this->transactionNestingLevel === 1) {
@@ -1263,11 +1271,11 @@ private function commitAll()
12631271
}
12641272

12651273
/**
1266-
* Cancels any database changes done during the current transaction.
1274+
* {@inheritDoc}
12671275
*
12681276
* @throws ConnectionException If the rollback operation failed.
12691277
*/
1270-
public function rollBack()
1278+
public function rollBack() : void
12711279
{
12721280
if ($this->transactionNestingLevel === 0) {
12731281
throw ConnectionException::noActiveTransaction();
@@ -1280,12 +1288,16 @@ public function rollBack()
12801288
if ($this->transactionNestingLevel === 1) {
12811289
$logger->startQuery('"ROLLBACK"');
12821290
$this->transactionNestingLevel = 0;
1283-
$connection->rollBack();
1284-
$this->isRollbackOnly = false;
1285-
$logger->stopQuery();
12861291

1287-
if ($this->autoCommit === false) {
1288-
$this->beginTransaction();
1292+
try {
1293+
$connection->rollBack();
1294+
} finally {
1295+
$this->isRollbackOnly = false;
1296+
$logger->stopQuery();
1297+
1298+
if ($this->autoCommit === false) {
1299+
$this->beginTransaction();
1300+
}
12891301
}
12901302
} elseif ($this->nestTransactionsWithSavepoints) {
12911303
$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)