Download the PHP package its-mieger/mysql-shared-locks without Composer

On this page you can find all versions of the php package its-mieger/mysql-shared-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 mysql-shared-locks

MySQL shared locks

This package implements a MySQL based shared locks for distributed applications.

Following features are provided:

The implemented locks guarantee to be released by end of the process holding them (if not done explicitly before) and they also guarantee to never block longer than their TTL, even the process holding them hangs longer.

Implementation details

Requirements

Database setup

Run the following SQL to create the locks table:

CREATE TABLE `shared_locks` (
  `name` varchar(64) NOT NULL,
  `connection_id` bigint(20) NOT NULL,
  `created` bigint(20) NOT NULL,
  `ttl` bigint(20) NOT NULL,
  `lock_acquired` tinyint(1) NOT NULL,
  UNIQUE KEY `shared_locks_ix1` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Example usage

// configure database connection
SharedLock::configureConnection(DB_HOST, DB_DATABASE, DB_USER, DB_PASSWORD);

// Acquire lock (timeout = 5s, TTL = 10s)
$lock = SharedLock::lock($lockName, 5, 10);

// Release
$lock->release();

SQL identifier quoting

By default MySQL's default identifier quotes '`' are used. However you may adapt the quoting style if using ANSI_QUOTES quotes to '"':

SharedLock::setQuoteStyle(SharedLock::QUOTE_STYLE_ANSI);

Being aware that a process rarely might exceed the lock TTL

Imagine you having a process which has acquired a lock. When this process hangs for a long time an then suddenly continues running the lock which it acquired might be taken over by another process. This case should rarely happen but nevertheless it may.

There are two strategies to handle this:

1. Using same database connection for locks and application code

You may pass the database connection for the locks using following command:

SharedLock::setConnection($pdoConnection);

Then use transactions to only commit data if lock has not exceeded meanwhile. Eg.:

// start transaction
$pdoConnection->beginTransaction();

try {
    $lock = SharedLock::lock('namedLock', 5, 10);

    /*
     * Your application code placed here
     */

    // commit transaction
    $pdoConnection->commit();

}
catch (Exception $ex) {

    // rollback transaction
    $pdoConnection->rollBack();

    throw $ex;
}
finally {

    // release lock
    if (!empty($lock))
        $lock->release();
}

If the lock would have been released meanwhile the SQL session would already have been killed and therefore your transaction will fail.

2. Using assertTTL

If using the same database connection for locks and application code in combination with transactions is not an option (or you don't use the lock for synchronizing DB operations), you can use the assertTTL function to check if the lock is still held and if there is enough time remaining for the process to complete the operation without fearing to lose lock meanwhile:

try {
    $lock = SharedLock::lock('namedLock', 5, 10);

    /*
     * Your application code placed here
     */

    // check if still locked
    $lock->assertTTL(5);

    /*
     * Your application code placed here
     */
}
finally {

    // release lock
    if (!empty($lock))
        $lock->release();
}

All versions of mysql-shared-locks with dependencies

PHP Build Version
Package Version
Requires ext-pdo Version *
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 its-mieger/mysql-shared-locks contains the following files

Loading the files please wait ...