Skip to content

Commit cd8c033

Browse files
committed
Adds transaction labels and enhanced logging.
Improves debugging capabilities by adding transaction labels to logs for easier identification. Enhances logging with SQL query information, improving visibility into database operations. Updates documentation with installation instructions and usage examples.
1 parent bd4e501 commit cd8c033

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,36 @@ Features:
77
- Exponential backoff with jitter between attempts
88
- Structured logging per attempt to storage/logs
99
- Safe in HTTP, CLI and queue contexts (request info captured when available)
10+
- Transaction labeling for easier debugging
11+
- Enhanced logging with SQL query information
1012

1113
Installation:
12-
- Require the package via Composer and ensure Laravel auto-discovers the service provider (already configured).
14+
- Require the package via Composer: `composer require ahed92wakim/laravel-mysql-deadlock-retry`
1315

1416
Usage:
1517

16-
```
18+
```php
1719
use MysqlDeadlocks\RetryHelper\DBTransactionRetryHelper as Retry;
1820

1921
$result = Retry::transactionWithRetry(function () {
2022
// Your DB logic here (queries, models, etc.)
2123
// Return any value and it will be returned from transactionWithRetry
22-
}, maxRetries: 3, retryDelay: 2, logFileName: 'mysql-deadlocks-log');
24+
}, maxRetries: 3, retryDelay: 2, logFileName: 'mysql-deadlocks', trxLabel: 'user-update');
2325
```
2426

2527
Parameters:
2628
- maxRetries: number of attempts (default 3)
2729
- retryDelay: base delay in seconds; actual wait uses exponential backoff with jitter (default 2)
28-
- logFileName: file prefix under storage/logs/{today date} (default 'mysql-deadlocks.log')
30+
- logFileName: file prefix under storage/logs/{today date} (default 'database/mysql-deadlocks')
31+
- trxLabel: transaction label for easier identification in logs (default '')
32+
33+
Logging:
34+
- Logs are stored in storage/logs/{date}/ directory
35+
- Successful transactions after retries are logged as warnings
36+
- Failed transactions after all retries are logged as errors
37+
- Logs include SQL queries, stack traces, and request information when available
2938

3039
Notes:
3140
- Non-deadlock QueryException is thrown immediately.
3241
- When attempts are exhausted, the last QueryException is thrown; if somehow no exception was thrown, a RuntimeException is raised.
42+
- Requires PHP 8.2+ and Laravel 11.0+

src/DBTransactionRetryHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class DBTransactionRetryHelper
2424
*/
2525
public static function transactionWithRetry(Closure $callback, int $maxRetries = 3, int $retryDelay = 2, string $logFileName = 'database/mysql-deadlocks', string $trxLabel = ''): mixed
2626
{
27+
if (is_null($trxLabel)){
28+
$trxLabel = '';
29+
}
2730
$attempt = 0;
2831
$log = [];
2932
$isDeadlock = false;
@@ -34,6 +37,7 @@ public static function transactionWithRetry(Closure $callback, int $maxRetries =
3437

3538
try {
3639
// Execute the transaction
40+
$trxLabel === '' || app()->instance('tx.label', $trxLabel);
3741
$result = DB::transaction($callback);
3842
return $result;
3943

0 commit comments

Comments
 (0)