The Lock component provides exclusive access to shared resources through many stores: files, semaphores, Redis, databases, etc. Symfony 8.2 added a #[Lock] attribute for controllers and also improves the stores themselves, focusing on database advisory locks: locks that the database server manages in memory and ties to a connection, so they need no table and are released automatically when that connection ends.

MySQL and MariaDB Advisory Locks

Ryan Pon
Contributed by Ryan Pon in #64757 and #64987

Until now, advisory locks were only available for PostgreSQL. MySQL and MariaDB required PdoStore or DoctrineDbalStore, which save locks in a database table and rely on a TTL to free the locks of crashed processes.

Symfony 8.2 adds two stores based on MySQL user-level locks: MysqlStore, which uses a PDO connection, and DoctrineDbalMysqlStore, which uses a Doctrine DBAL connection. Add +advisory to the DSN scheme to use them:

1
2
3
4
# config/packages/lock.yaml
framework:
    # a Doctrine DBAL URL creates a DoctrineDbalMysqlStore
    lock: 'mysql+advisory://db_user:db_password@127.0.0.1:3306/app'

A PDO DSN (mysql+advisory:host=127.0.0.1;dbname=app) creates a MysqlStore instead. In standalone PHP applications, create the stores yourself:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\MysqlStore;

$store = new MysqlStore('mysql:host=127.0.0.1;dbname=app', [
    'db_username' => 'db_user',
    'db_password' => 'db_password',
]);
$lock = new LockFactory($store)->createLock('invoice-'.$invoice->getId());

// waits until the lock is free, without polling the database
$lock->acquire(blocking: true);

Both stores support native blocking and their locks never expire, so you don't have to choose a TTL or refresh the lock in long tasks. They require MySQL 5.7.5 or MariaDB 10.0.2 or newer. MySQL has no shared advisory locks, so acquireRead() acquires an exclusive lock.

Advisory Locks on an Existing Connection

MD. Saiful Islam Feroz Nicolas Grekas
Contributed by MD. Saiful Islam Feroz and Nicolas Grekas in #65021 and #65098

The +advisory DSNs open their own database connection. If you passed the service id of an existing connection, you always got a table-based store. In Symfony 8.2, a lock store can also be an array with the service_id of a PDO or Doctrine DBAL connection and the new advisory option:

1
2
3
4
5
# config/packages/lock.yaml
framework:
    lock:
        service_id: 'doctrine.dbal.lock_connection'
        advisory: true

Symfony picks the right store for that connection's database (PostgreSQL, MySQL or MariaDB) and throws an exception for databases without advisory locks. In standalone PHP applications, pass true as the new second argument of the store factory:

1
2
3
4
use Symfony\Component\Lock\Store\StoreFactory;

// it must be a positional argument: "advisory: true" doesn't work
$store = StoreFactory::createStore($connection, true);

Caution

Advisory locks belong to the database session. If the connection is closed or reconnected (e.g. by the doctrine_close_connection Messenger middleware or the idle timeout of the server), the database releases all its locks without notifying your application. That's why the example uses a dedicated Doctrine connection (called lock) instead of the default one.

Removing Flock Files on Release

Jérôme Tamarelle
Contributed by Jérôme Tamarelle in #66526

The FlockStore creates one file per locked resource and keeps it after releasing the lock, so the next acquisition can reuse it. That's fine when you lock a few names again and again, but applications that lock many different resources (e.g. one per order or user) end up with thousands of files in the lock directory.

The new flock+remove DSN creates a FlockStore that removes the file when an exclusive lock is released:

1
2
3
4
5
6
7
# config/packages/lock.yaml
framework:
    # uses the same project-scoped directory as 'flock'
    lock: 'flock+remove'

    # or use your own directory
    lock: 'flock+remove:///path/to/locks'

In standalone PHP applications, use the new removeOnRelease argument:

1
2
3
use Symfony\Component\Lock\Store\FlockStore;

$store = new FlockStore('/path/to/locks', removeOnRelease: true);

Removing files makes locking the same resource repeatedly slower, because the file is created on each acquisition. That's why it's opt-in. Also, all processes sharing a lock directory must use the same setting. Otherwise, two of them could hold the same lock at once.

Published in #Living on the edge