Download the PHP package mehr-it/lara-mysql-locks without Composer

On this page you can find all versions of the php package mehr-it/lara-mysql-locks. 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 lara-mysql-locks

MySQL based locks for Laravel

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

Requirements

This package only works with MySQL database connections!

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

$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

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 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

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

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


All versions of lara-mysql-locks with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
ext-pdo Version *
doctrine/dbal Version ^2.9|^3.0
laravel/framework Version ^5.8|^6.0|^7.0|^8.0|^9.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 mehr-it/lara-mysql-locks contains the following files

Loading the files please wait ....