Skip to content

Symfony 8.2 adds MySQL and MariaDB advisory locks

The Lock component gets native advisory locking on MySQL and MariaDB, so you no longer need Postgres, Redis or a polling database table just to coordinate processes.

Source: Symfony

Symfony’s Lock component has had advisory locks for Postgres for a while, but MySQL and MariaDB users were stuck with PdoStore or DoctrineDbalStore, which write a row to a table and rely on a TTL to release locks from crashed processes. Symfony 8.2, released today, adds two new stores, MysqlStore and DoctrineDbalMysqlStore, that use MySQL’s native advisory locks instead. They need MySQL 5.7.5 or MariaDB 10.0.2 or newer.

The new stores block natively rather than polling, never expire on their own, and release automatically when the connection closes. The one catch: MySQL has no concept of a shared lock, so acquireRead() ends up taking an exclusive lock anyway.

# config/packages/lock.yaml
framework:
    lock: 'mysql+advisory://db_user:db_password@127.0.0.1:3306/app'

Symfony 8.2 also lets you run advisory locks over a connection you already have open, instead of opening a dedicated one, and adds a flock+remove DSN that deletes lock files once an exclusive lock is released.

Why it matters: if you’re using Symfony’s Lock component on MySQL or MariaDB to stop duplicate cron runs or overlapping jobs, this removes the usual TTL guesswork and the extra lock table. No Redis or Postgres needed just for coordination.

Share

Related reading

More from The Wire

All briefs