PHPackages                             mehr-it/lara-mysql-locks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mehr-it/lara-mysql-locks

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mehr-it/lara-mysql-locks
========================

Distributed locks for laravel using MySQL &gt;= 5.7.5 database

2.5.0(3y ago)02.7kMITPHPPHP &gt;=7.1CI failing

Since Jun 19Pushed 3y agoCompare

[ Source](https://github.com/mehr-it/lara-mysql-locks)[ Packagist](https://packagist.org/packages/mehr-it/lara-mysql-locks)[ RSS](/packages/mehr-it-lara-mysql-locks/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (4)Versions (13)Used By (0)

MySQL based locks for Laravel
=============================

[](#mysql-based-locks-for-laravel)

This package implements MySQL based locks for distributed systems with following features:

- named logs without prior initialisation
- wait timeout for obtaining locks
- TTL (time to live) for locks
- database session is terminated when lock expired and taken over by another process
- if not released, locks are automatically released when process ends or dies unexpectedly
- waiting for locks uses blocking database requests, not polling
- locks released before TTL can immediately be acquired by other processes

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1
- MySQL &gt;= 5.7.5 (before 5.7.5 only one named lock per connection can be acquired)

**This package only works with MySQL database connections!**

Install
-------

[](#install)

```
composer require mehr-it/lara-mysql-locks

```

This package uses Laravel's package auto-discovery, so the service provider and aliases will be loaded automatically.

Usage
-----

[](#usage)

```
$lock = DbLock::lock('my-lock', 5, 10);

// do some work

$lock->release();

```

The `lock()` method expects the lock name, the time to wait (before timeout) and the maximum time to live for the lock (both in seconds).

When the lock cannot be obtained within the given timeout, a `DbLockTimeoutException` is thrown.

The `release()` method releases the lock. It fails, if the database transaction is lost or the lock's TTL has expired **and** another process acquired the lock. In such cases a `DbLockReleaseException` is thrown, indicating that the lock was not acquired anymore.

### Using callbacks

[](#using-callbacks)

The `release()` method should always be called. Therefore you usually should acquire locks using the `withLock()` method and pass a callback. `withLock()` ensures that the lock is released even if an error is thrown:

```
$return = DbLock::withLock(
		function($lock) {

		// do some work

		}, 'my-lock', 5, 10
	);

```

**The `withLock()` method executes the callback within a database transaction.** The transaction is rolled back on any thrown or lock errors.

You may pass a different database connection to create the lock and the transaction in, by passing it's name to withLock:

```
DbLock::withLock(function($lock) { }, 'my-lock', 5, 10, 'my-connection');

```

**The other connection must target the same MySQL instance, as the default connection! Otherwise the lock is immediately released!**

Locks and database connections
------------------------------

[](#locks-and-database-connections)

Locks are always bound to database connections. If no other connection is passed when creating the lock, they are bound to the default connection.

**The database connection the lock is bound to, will be terminated when the lock's TTL is expired *and* another process acquires the lock!**

This has to be considered when working with transactions. Because any SQL queries after "lock loss" within a transaction will fail. As mostly intended (you usually don't want to commit anything when lock TTL exceeded) there might be cases when this behaviour is unwanted. In such cases you must pass another database connection to create the lock on:

```
DbLock::lock('my-lock', 5, 10, 'other-connection');

```

**This other connection must target the same MySQL instance, as the default connection! Otherwise the lock is immediately released!**

If not working with transactions, laravel will gracefully reconnect to the database and your program will continue as expected in these cases.

Verify if lock is still acquired
--------------------------------

[](#verify-if-lock-is-still-acquired)

Sometimes you might have to check if the lock is still acquired. This usually happens if performing operations affecting other resources not covered by database transactions.

Following example checks that the lock is acquired for at least 5 more seconds. If not a `DbLockRemainingTTLException` is thrown:

```
$lock->assertAcquiredFor(5);

```

If you don't want an exception, you my use `remainsAcquiredFor()`:

```
if (!$lock->remainsAcquiredFor(5)) {
	// handle lock timeout
}

```

Limitations
-----------

[](#limitations)

MySQL has a maximum length of lock names. Therefore you must not use lock names with more than 50 characters.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~117 days

Recently: every ~193 days

Total

12

Last Release

1225d ago

Major Versions

1.1.0 → 2.0.02019-11-21

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/44973729?v=4)[mehr.IT GmbH](/maintainers/mehr-it)[@mehr-it](https://github.com/mehr-it)

---

Top Contributors

[![chmgr](https://avatars.githubusercontent.com/u/94837732?v=4)](https://github.com/chmgr "chmgr (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mehr-it-lara-mysql-locks/health.svg)

```
[![Health](https://phpackages.com/badges/mehr-it-lara-mysql-locks/health.svg)](https://phpackages.com/packages/mehr-it-lara-mysql-locks)
```

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[blair2004/nexopos

The Free Modern Point Of Sale System build with Laravel, TailwindCSS and Vue.js.

1.2k2.3k](/packages/blair2004-nexopos)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)[ronasit/laravel-helpers

Provided helpers function and some helper class.

1475.7k13](/packages/ronasit-laravel-helpers)[boomcms/boom-core

Core classes for BoomCMS

193.0k6](/packages/boomcms-boom-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
