Download the PHP package ahed92wakim/laravel-db-transaction-retry without Composer

On this page you can find all versions of the php package ahed92wakim/laravel-db-transaction-retry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-db-transaction-retry

Tests Packagist Version MIT License Laravel 11 or 12 PHP ^8.2 PHP CS Fixer

Resilient database transactions for Laravel applications that need to gracefully handle deadlocks, serialization failures, and any other transient database errors you configure. This helper wraps DB::transaction() with targeted retries, retry event persistence, and exponential backoff so you can keep your business logic simple while surviving temporary contention.

Highlights

Installation

The package ships with the DatabaseTransactionRetryServiceProvider, which Laravel auto-discovers. No additional setup is needed.

If you want the package to persist retry events, request logs, slow transaction logs, or query exceptions, publish the package assets and run the migrations before relying on the dashboard/telemetry features:

Usage

runWithRetry() returns the value produced by your callback, just like DB::transaction(). If every attempt fails, the last exception is re-thrown so your calling code can continue its normal error handling.

DB Macro Convenience

Prefer working through the database facade? Call the included transactionWithRetry macro and keep identical behaviour and parameters:

Need connection-specific logic? Because the macro is applied to Illuminate\Support\Facades\DB and to every resolved Illuminate\Database\Connection, you can call it on connection instances as well:

The macro is registered automatically when the service provider boots, and sets the tx.label container binding the same way as the helper.

Parameters

Parameter Default Description
maxRetries Config (default: 3) Total number of attempts (initial try + retries).
retryDelay Config (default: 2s) Base delay (seconds). Actual wait uses exponential backoff with ±25% jitter.
trxLabel '' Optional label injected into log titles and stored in the service container as tx.label for downstream consumers.

Call the helper anywhere you would normally open a transaction—controllers, jobs, console commands, or domain services.

Configuration

Publish the configuration file to tweak defaults globally:

You can also run php artisan db-transaction-retry:install to publish the config, migrations, auth provider stub, and dashboard in one step.

Database Migration

Retry events are stored in the database by default. Publish the migration and run it:

Or publish manually:

The migrations create the transaction_retry_events, db_transaction_logs, db_query_logs, db_request_logs, and db_exceptions tables by default.

Dashboard (Next.js UI)

The package ships a static Next.js dashboard that is published into your Laravel app's public/vendor/laravel-db-transaction-retry/{dashboard.path} directory. By default, it is available at:

Route tables in the dashboard link to a per-route detail view. The request metrics endpoints (/metrics/requests, /metrics/requests-duration, and /requests) accept optional method, route_name, and url filters to scope data to a single route.

Securing the dashboard

By default the dashboard uses the AuthorizeTransactionRetryDashboard middleware, which only allows access in the local environment until you define your own authorization logic (mirroring Telescope). The install command publishes an app/Providers/TransactionRetryDashboardServiceProvider.php stub you can edit.

To customize access, define a gate and register the authorization callback in one of your application service providers:

Register the published service provider in bootstrap/providers.php (Laravel 11/12):

You can also swap the middleware stack for the dashboard and API routes in config/database-transaction-retry.php:

Define the viewTransactionRetryDashboard gate in your application (or swap in any other middleware).

Publish the dashboard assets:

The db-transaction-retry:install command publishes the dashboard too.

Rebuilding the UI (package contributors)

If you are working on the package itself and want to rebuild the dashboard:

This writes static assets to dashboard/out, which are published to the host app's public/vendor/laravel-db-transaction-retry/{dashboard.path} directory.

Uninstall

When you remove the package with Composer, the service provider listens for the composer_package.ahed92wakim/laravel-db-transaction-retry:pre_uninstall event and cleans up published assets (similar to Telescope). It also removes the published dashboard service provider from bootstrap/providers.php when available. It deletes:

Database tables are not dropped automatically. If you want to remove them, drop the tables manually or run your own cleanup migration.

Partition Maintenance (MySQL)

The migration creates hourly partitions for MySQL. Keep partitions rolling by scheduling the command to run hourly:

Make sure your scheduler is running (for example, the standard schedule:run cron).

Retry Conditions

Retries are attempted when the caught exception matches one of the configured conditions:

Everything else (e.g., constraint violations, syntax errors, application exceptions) is surfaced immediately without logging or sleeping. If no attempt succeeds and all retries are exhausted, the last exception is re-thrown. In the rare case nothing is thrown but the loop exits, a RuntimeException is raised to signal exhaustion.

Lock Wait Timeout

When lock_wait_timeout_seconds is configured, the retrier issues SET SESSION innodb_lock_wait_timeout = {seconds} on the active connection before each attempt, but only when retry_on_lock_wait_timeout is enabled. This keeps the timeout predictable even after reconnects or pool reuse, and on drivers that do not support the statement the helper safely ignores the failure.

Retry Event Storage

Retry events are stored in the transaction_retry_events table. Each retryable exception attempt is persisted, plus a final success or failure entry once the retrier finishes:

Each retry event stores:

Runtime Toggle

Use the built-in Artisan commands to temporarily disable or re-enable retries without touching configuration files:

The commands write a small marker file inside the package (storage/runtime/retry-disabled.marker). As long as that file exists retries stay off; removing it or running db-transaction-retry:start brings them back. You can still set the DB_TRANSACTION_RETRY_ENABLED environment variable for a permanent default.

Heads up: The db-transaction-retry:start command only removes the disable marker—it does not override an explicit database-transaction-retry.enabled=false configuration (including the DB_TRANSACTION_RETRY_ENABLED=false environment variable). Update that setting to true if you want retries to remain enabled after the current process.

Helper Utilities

The package exposes dedicated support classes you can reuse in your own instrumentation:

For testing scenarios, the retrier looks for a namespaced DatabaseTransactions\RetryHelper\sleep() function before falling back to PHP's global sleep(), making it easy to assert backoff intervals without introducing delays.

Testing the Package

Run the test suite with:

Tests cover the retry flow, database event persistence, exponential backoff jitter, and non-retryable scenarios using database fakes.

Requirements

Changelog

Notable changes are tracked in CHANGELOG.md.

Contributing

Bugs, ideas, and pull requests are welcome. Feel free to open an issue describing the problem or improvement before submitting a PR so we can collaborate on scope.

License

This package is open-sourced software released under the MIT License.


All versions of laravel-db-transaction-retry with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version ^11.0 || ^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ahed92wakim/laravel-db-transaction-retry contains the following files

Loading the files please wait ...