Skip to content

Commit 10028ab

Browse files
committed
Updates dependencies and improves retry logic
- Updates composer dependencies to newer versions, including Laravel 12 and PHP 8.2 - Refactors retry logic to include exponential backoff with jitter for more robust handling of deadlocks - Improves logging context with request details (URL, method, token, userId) when available, and includes full exception traces for debugging - Enhance logic to identify retryable deadlock/serialization failure errors
1 parent 3b94c14 commit 10028ab

File tree

5 files changed

+767
-455
lines changed

5 files changed

+767
-455
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
# laravel-mysql-deadlock-retry
2+
3+
A lightweight helper to run Laravel database transactions with automatic retries on MySQL deadlocks and serialization failures.
4+
5+
Features:
6+
- Retries DB::transaction on MySQL deadlocks (error 1213) and SQLSTATE 40001
7+
- Exponential backoff with jitter between attempts
8+
- Structured logging per attempt to storage/logs
9+
- Safe in HTTP, CLI and queue contexts (request info captured when available)
10+
11+
Installation:
12+
- Require the package via Composer and ensure Laravel auto-discovers the service provider (already configured).
13+
14+
Usage:
15+
16+
```
17+
use MysqlDeadlocks\RetryHelper\DBTransactionRetryHelper as Retry;
18+
19+
$result = Retry::transactionWithRetry(function () {
20+
// Your DB logic here (queries, models, etc.)
21+
// Return any value and it will be returned from transactionWithRetry
22+
}, maxRetries: 5, retryDelay: 2, logFileName: 'mysql-deadlocks-log');
23+
```
24+
25+
Parameters:
26+
- maxRetries: number of attempts (default 5)
27+
- retryDelay: base delay in seconds; actual wait uses exponential backoff with jitter (default 5)
28+
- logFileName: file prefix under storage/logs (default 'mysql-deadlocks-log')
29+
30+
Notes:
31+
- Non-deadlock QueryException is thrown immediately.
32+
- When attempts are exhausted, the last QueryException is thrown; if somehow no exception was thrown, a RuntimeException is raised.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"type": "library",
55
"minimum-stability": "stable",
66
"require": {
7-
"php": ">=8.0",
8-
"laravel/framework": ">=10.0"
7+
"php": ">=8.2",
8+
"laravel/framework": ">=11.0"
99
},
1010
"license": "MIT",
1111
"autoload": {

0 commit comments

Comments
 (0)