Skip to content

Commit df9f137

Browse files
authored
feat: classify retryable transaction exceptions (#10162)
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 49fda0b commit df9f137

11 files changed

Lines changed: 438 additions & 55 deletions

File tree

‎system/Database/BaseConnection.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use Closure;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18+
use CodeIgniter\Database\Exceptions\RetryableTransactionException;
19+
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1820
use CodeIgniter\Events\Events;
1921
use CodeIgniter\I18n\Time;
2022
use Exception;
@@ -2138,6 +2140,41 @@ public function getLastException(): ?DatabaseException
21382140
return $this->lastException;
21392141
}
21402142

2143+
/**
2144+
* Checks whether the native database error represents a unique constraint violation.
2145+
*/
2146+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
2147+
{
2148+
return false;
2149+
}
2150+
2151+
/**
2152+
* Checks whether the native database code represents a retryable transaction failure.
2153+
*/
2154+
protected function isRetryableTransactionErrorCode(int|string $code): bool
2155+
{
2156+
return false;
2157+
}
2158+
2159+
/**
2160+
* Creates the appropriate database exception for a native database error.
2161+
*/
2162+
protected function createDatabaseException(
2163+
string $message,
2164+
int|string $code = 0,
2165+
?Throwable $previous = null,
2166+
): DatabaseException {
2167+
if ($this->isUniqueConstraintViolation($code, $message)) {
2168+
return new UniqueConstraintViolationException($message, $code, $previous);
2169+
}
2170+
2171+
if ($this->isRetryableTransactionErrorCode($code)) {
2172+
return new RetryableTransactionException($message, $code, $previous);
2173+
}
2174+
2175+
return new DatabaseException($message, $code, $previous);
2176+
}
2177+
21412178
/**
21422179
* Insert ID
21432180
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Exceptions;
15+
16+
/**
17+
* Thrown when a transaction failure may succeed if the whole transaction is retried.
18+
*/
19+
class RetryableTransactionException extends DatabaseException
20+
{
21+
}

‎system/Database/MySQLi/Connection.php‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1918
use CodeIgniter\Database\TableName;
2019
use CodeIgniter\Exceptions\LogicException;
2120
use mysqli;
@@ -98,6 +97,24 @@ class Connection extends BaseConnection
9897
*/
9998
protected bool $strictOn = false;
10099

100+
/**
101+
* Checks whether the native database error represents a unique constraint violation.
102+
*/
103+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
104+
{
105+
// ER_DUP_ENTRY: duplicate key value.
106+
return $code === 1062;
107+
}
108+
109+
/**
110+
* Checks whether the native database code represents a retryable transaction failure.
111+
*/
112+
protected function isRetryableTransactionErrorCode(int|string $code): bool
113+
{
114+
// ER_LOCK_DEADLOCK: InnoDB rolls back the full transaction.
115+
return $code === 1213;
116+
}
117+
101118
/**
102119
* Connect to the database.
103120
*
@@ -317,10 +334,7 @@ protected function execute(string $sql)
317334
'trace' => render_backtrace($e->getTrace()),
318335
]);
319336

320-
// MySQL error 1062: ER_DUP_ENTRY – duplicate key value
321-
$exception = $e->getCode() === 1062
322-
? new UniqueConstraintViolationException($e->getMessage(), $e->getCode(), $e)
323-
: new DatabaseException($e->getMessage(), $e->getCode(), $e);
337+
$exception = $this->createDatabaseException($e->getMessage(), $e->getCode(), $e);
324338

325339
if ($this->DBDebug) {
326340
throw $exception;

‎system/Database/OCI8/Connection.php‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1918
use CodeIgniter\Database\Query;
2019
use CodeIgniter\Database\TableName;
2120
use ErrorException;
@@ -115,6 +114,23 @@ class Connection extends BaseConnection
115114
*/
116115
public $lastInsertedTableName;
117116

117+
/**
118+
* Checks whether the native database error represents a unique constraint violation.
119+
*/
120+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
121+
{
122+
// ORA-00001: unique constraint violated.
123+
return $code === 1;
124+
}
125+
126+
/**
127+
* Checks whether the native database code represents a retryable transaction failure.
128+
*/
129+
protected function isRetryableTransactionErrorCode(int|string $code): bool
130+
{
131+
return in_array($code, [60, 8177], true);
132+
}
133+
118134
/**
119135
* confirm DSN format.
120136
*/
@@ -240,11 +256,8 @@ protected function execute(string $sql)
240256
$result = oci_execute($this->stmtId, $this->commitMode) ? $this->stmtId : false;
241257

242258
if ($result === false) {
243-
// ORA-00001: unique constraint violated
244259
$error = $this->error();
245-
$exception = $error['code'] === 1
246-
? new UniqueConstraintViolationException((string) $error['message'], $error['code'])
247-
: new DatabaseException((string) $error['message'], $error['code']);
260+
$exception = $this->createDatabaseException((string) $error['message'], $error['code']);
248261

249262
if ($this->DBDebug) {
250263
throw $exception;
@@ -272,11 +285,8 @@ protected function execute(string $sql)
272285
'trace' => render_backtrace($trace),
273286
]);
274287

275-
// ORA-00001: unique constraint violated
276288
$error = $this->error();
277-
$exception = $error['code'] === 1
278-
? new UniqueConstraintViolationException((string) $error['message'], $error['code'], $e)
279-
: new DatabaseException((string) $error['message'], $error['code'], $e);
289+
$exception = $this->createDatabaseException((string) $error['message'], $error['code'], $e);
280290

281291
if ($this->DBDebug) {
282292
throw $exception;

‎system/Database/Postgre/Connection.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1918
use CodeIgniter\Database\RawSql;
2019
use CodeIgniter\Database\TableName;
2120
use ErrorException;
@@ -63,6 +62,22 @@ class Connection extends BaseConnection
6362
*/
6463
private ?PgSqlResult $lastFailedResult = null;
6564

65+
/**
66+
* Checks whether the native database error represents a unique constraint violation.
67+
*/
68+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
69+
{
70+
return $code === '23505';
71+
}
72+
73+
/**
74+
* Checks whether the native database code represents a retryable transaction failure.
75+
*/
76+
protected function isRetryableTransactionErrorCode(int|string $code): bool
77+
{
78+
return in_array($code, ['40001', '40P01'], true);
79+
}
80+
6681
/**
6782
* Connect to the database.
6883
*
@@ -272,9 +287,7 @@ protected function execute(string $sql)
272287
'trace' => render_backtrace($trace),
273288
]);
274289

275-
$exception = $sqlstate === '23505'
276-
? new UniqueConstraintViolationException($message, $sqlstate)
277-
: new DatabaseException($message, $sqlstate);
290+
$exception = $this->createDatabaseException($message, $sqlstate);
278291

279292
if ($this->DBDebug) {
280293
throw $exception;

‎system/Database/SQLSRV/Connection.php‎

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1918
use CodeIgniter\Database\TableName;
2019
use stdClass;
2120

@@ -90,6 +89,44 @@ class Connection extends BaseConnection
9089
*/
9190
protected $_reserved_identifiers = ['*'];
9291

92+
/**
93+
* Checks whether the native database error represents a unique constraint violation.
94+
*/
95+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
96+
{
97+
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
98+
if (! is_array($errors)) {
99+
return false;
100+
}
101+
102+
foreach ($errors as $error) {
103+
// SQLSTATE 23000 (integrity constraint violation) with SQL Server error
104+
// 2627 (UNIQUE CONSTRAINT or PRIMARY KEY violation) or 2601 (UNIQUE INDEX violation).
105+
if (($error['SQLSTATE'] ?? '') === '23000'
106+
&& in_array($error['code'] ?? 0, [2627, 2601], true)) {
107+
return true;
108+
}
109+
}
110+
111+
return false;
112+
}
113+
114+
/**
115+
* Checks whether the native database code represents a retryable transaction failure.
116+
*/
117+
protected function isRetryableTransactionErrorCode(int|string $code): bool
118+
{
119+
$vendorCode = (string) (is_string($code) && str_contains($code, '/')
120+
? substr($code, strrpos($code, '/') + 1)
121+
: $code);
122+
123+
if (preg_match('/^\d+$/', $vendorCode) !== 1) {
124+
return false;
125+
}
126+
127+
return in_array((int) $vendorCode, [1205, 3960], true);
128+
}
129+
93130
/**
94131
* Class constructor
95132
*/
@@ -538,9 +575,7 @@ protected function execute(string $sql)
538575
]);
539576

540577
$error = $this->error();
541-
$exception = $this->isUniqueConstraintViolation()
542-
? new UniqueConstraintViolationException($message, $error['code'])
543-
: new DatabaseException($message, $error['code']);
578+
$exception = $this->createDatabaseException($message, $error['code']);
544579

545580
if ($this->DBDebug) {
546581
throw $exception;
@@ -552,25 +587,6 @@ protected function execute(string $sql)
552587
return $stmt;
553588
}
554589

555-
private function isUniqueConstraintViolation(): bool
556-
{
557-
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
558-
if (! is_array($errors)) {
559-
return false;
560-
}
561-
562-
foreach ($errors as $error) {
563-
// SQLSTATE 23000 (integrity constraint violation) with SQL Server error
564-
// 2627 (UNIQUE CONSTRAINT or PRIMARY KEY violation) or 2601 (UNIQUE INDEX violation).
565-
if (($error['SQLSTATE'] ?? '') === '23000'
566-
&& in_array($error['code'] ?? 0, [2627, 2601], true)) {
567-
return true;
568-
}
569-
}
570-
571-
return false;
572-
}
573-
574590
/**
575591
* The name of the platform in use (MySQLi, mssql, etc)
576592
*/

‎system/Database/SQLite3/Connection.php‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\Database\BaseConnection;
1717
use CodeIgniter\Database\Exceptions\DatabaseException;
18-
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
1918
use CodeIgniter\Database\TableName;
2019
use CodeIgniter\Exceptions\InvalidArgumentException;
2120
use Exception;
@@ -67,6 +66,26 @@ class Connection extends BaseConnection
6766
*/
6867
protected ?int $synchronous = null;
6968

69+
/**
70+
* Checks whether the native database error represents a unique constraint violation.
71+
*/
72+
protected function isUniqueConstraintViolation(int|string $code, string $message): bool
73+
{
74+
// SQLite3 reports unique violations in two formats depending on version:
75+
// Modern: "UNIQUE constraint failed: table.column"
76+
// Legacy: "column X is not unique"
77+
return str_contains($message, 'UNIQUE constraint failed')
78+
|| str_contains($message, 'is not unique');
79+
}
80+
81+
/**
82+
* Checks whether the native database code represents a retryable transaction failure.
83+
*/
84+
protected function isRetryableTransactionErrorCode(int|string $code): bool
85+
{
86+
return $code === 5;
87+
}
88+
7089
/**
7190
* @return void
7291
*/
@@ -172,9 +191,7 @@ protected function execute(string $sql)
172191
]);
173192

174193
$error = $this->error();
175-
$exception = $this->isUniqueConstraintViolation($e->getMessage())
176-
? new UniqueConstraintViolationException($e->getMessage(), $error['code'], $e)
177-
: new DatabaseException($e->getMessage(), $error['code'], $e);
194+
$exception = $this->createDatabaseException($e->getMessage(), $error['code'], $e);
178195

179196
if ($this->DBDebug) {
180197
throw $exception;
@@ -186,15 +203,6 @@ protected function execute(string $sql)
186203
return false;
187204
}
188205

189-
private function isUniqueConstraintViolation(string $message): bool
190-
{
191-
// SQLite3 reports unique violations in two formats depending on version:
192-
// Modern: "UNIQUE constraint failed: table.column"
193-
// Legacy: "column X is not unique"
194-
return str_contains($message, 'UNIQUE constraint failed')
195-
|| str_contains($message, 'is not unique');
196-
}
197-
198206
/**
199207
* Returns the total number of rows affected by this query.
200208
*/

0 commit comments

Comments
 (0)