Skip to content

Commit eb0c4bb

Browse files
Ocramiusmorozov
authored andcommitted
Merge pull request #3480 from morozov/transaction-void
Transaction-related Statement methods return void
2 parents dc5e327 + 2b2f9c4 commit eb0c4bb

File tree

12 files changed

+95
-85
lines changed

12 files changed

+95
-85
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
}
@@ -1223,8 +1226,12 @@ public function commit()
12231226

12241227
if ($this->transactionNestingLevel === 1) {
12251228
$logger->startQuery('"COMMIT"');
1226-
$connection->commit();
1227-
$logger->stopQuery();
1229+
1230+
try {
1231+
$connection->commit();
1232+
} finally {
1233+
$logger->stopQuery();
1234+
}
12281235
} elseif ($this->nestTransactionsWithSavepoints) {
12291236
$logger->startQuery('"RELEASE SAVEPOINT"');
12301237
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
@@ -1234,18 +1241,19 @@ public function commit()
12341241
--$this->transactionNestingLevel;
12351242

12361243
if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
1237-
return true;
1244+
return;
12381245
}
12391246

12401247
$this->beginTransaction();
1241-
1242-
return true;
12431248
}
12441249

12451250
/**
12461251
* Commits all current nesting transactions.
1252+
*
1253+
* @throws ConnectionException
1254+
* @throws DriverException
12471255
*/
1248-
private function commitAll()
1256+
private function commitAll() : void
12491257
{
12501258
while ($this->transactionNestingLevel !== 0) {
12511259
if ($this->autoCommit === false && $this->transactionNestingLevel === 1) {
@@ -1261,11 +1269,11 @@ private function commitAll()
12611269
}
12621270

12631271
/**
1264-
* Cancels any database changes done during the current transaction.
1272+
* {@inheritDoc}
12651273
*
12661274
* @throws ConnectionException If the rollback operation failed.
12671275
*/
1268-
public function rollBack()
1276+
public function rollBack() : void
12691277
{
12701278
if ($this->transactionNestingLevel === 0) {
12711279
throw ConnectionException::noActiveTransaction();
@@ -1278,12 +1286,16 @@ public function rollBack()
12781286
if ($this->transactionNestingLevel === 1) {
12791287
$logger->startQuery('"ROLLBACK"');
12801288
$this->transactionNestingLevel = 0;
1281-
$connection->rollBack();
1282-
$this->isRollbackOnly = false;
1283-
$logger->stopQuery();
12841289

1285-
if ($this->autoCommit === false) {
1286-
$this->beginTransaction();
1290+
try {
1291+
$connection->rollBack();
1292+
} finally {
1293+
$this->isRollbackOnly = false;
1294+
$logger->stopQuery();
1295+
1296+
if ($this->autoCommit === false) {
1297+
$this->beginTransaction();
1298+
}
12871299
}
12881300
} elseif ($this->nestTransactionsWithSavepoints) {
12891301
$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)