Database Lock
stellarwp/foundation-lock-database supplies DatabaseLock, which implements the shared Foundation Lock contract with a WordPress table. Choose it when every process that must coordinate can reach the same primary database and a dedicated Redis service is unnecessary.
Set up database locks
Section titled “Set up database locks”Install the backend and its Database and Lock dependencies:
Set a stable application prefix in the root config.php supplied to your application container:
For this prefix, the lock table defaults to your_plugin_foundation_locks before WordPress adds its site prefix. Register the providers below before resolving lock services.
Select the database implementation
Section titled “Select the database implementation”DatabaseProvider supplies the shared database connection. LockDatabaseProvider wires the database lock and its storage table. Select it as the application’s Lock implementation in your existing application provider, or create src/Lock/Provider.php:
The binding shares the configured DatabaseLock instance with application lock consumers. Registering the backend provider preserves any existing application Lock selection, so both database and Redis implementations can be available.
Register providers in dependency order in src/App.php:
Initialize lock storage
Section titled “Initialize lock storage”Create the configured lock table during activation or deployment by resolving DatabaseLock and calling initialize() after registering the providers:
Subsequent calls reuse the existing table. Initialize storage before services acquire locks. Normal lock operations perform no DDL and report LockUnavailableException if storage is unavailable.
Inject LockOperation into application services to run bounded work using the selected backend. The Lock guide covers complete usage, contention, renewal, and failure handling.
Database-backed locks have these operational constraints:
- Lock names must fit within 191 bytes.
- Expiration uses the database’s UTC clock, keeping ownership consistent between PHP processes.
- Every contender must read and write through the same primary database. Replica reads can report stale ownership.
- The TTL must exceed the protected operation, or the owner must refresh the token before it expires.
Customize storage
Section titled “Customize storage”Override the unprefixed table name through lock.database.table when an existing deployment needs a specific name. In root config.php, add the override only when configured:
Foundation adds the current WordPress site prefix. Physical names must contain only ASCII letters, numbers, and underscores, and fit within 64 bytes including the site prefix. Keep the configured name stable across deployments so every worker coordinates through the same table.
Testing
Section titled “Testing”Use InMemoryLock in unit and feature tests that only verify application behavior against the shared contract. Use wpunit when testing DatabaseLock itself or behavior that depends on real lock-table queries and database time.