https://github.com/cybercog/php-db-locker
PHP application-level database locking mechanisms to implement concurrency control patterns.
https://github.com/cybercog/php-db-locker
advisory-locks concurrency concurrency-control database db lock locking locks mutex race-condition-prevention race-conditions
Last synced: 2 months ago
JSON representation
PHP application-level database locking mechanisms to implement concurrency control patterns.
- Host: GitHub
- URL: https://github.com/cybercog/php-db-locker
- Owner: cybercog
- License: mit
- Created: 2022-05-09T16:31:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-07-25T14:20:56.000Z (3 months ago)
- Last Synced: 2025-07-25T21:25:00.010Z (3 months ago)
- Topics: advisory-locks, concurrency, concurrency-control, database, db, lock, locking, locks, mutex, race-condition-prevention, race-conditions
- Language: PHP
- Homepage: https://komarev.com/sources
- Size: 30.3 KB
- Stars: 5
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP DB Locker

## Introduction
> WARNING! This library is currently under development and may not be stable. Use in your services at your own risk.
PHP application-level database locking mechanisms to implement concurrency control patterns.
Supported drivers:
- Postgres — [PostgreSQL Advisory Locks Documentation](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS)
## Installation
Pull in the package through [Composer](https://getcomposer.org/).
```shell
composer require cybercog/php-db-locker
```## Usage
### Postgres
#### Transaction-level advisory lock
```php
$dbConnection = new PDO($dsn, $username, $password);$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');$dbConnection->beginTransaction();
$lock = $locker->acquireSessionLevelLockHandler(
$dbConnection,
$lockId,
\Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking,
\Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum::Exclusive,
);
if ($lock->wasAcquired) {
// Execute logic if lock was successful
} else {
// Execute logic if lock acquisition has been failed
}
$dbConnection->commit();
```#### Session-level advisory lock
```php
$dbConnection = new PDO($dsn, $username, $password);$locker = new \Cog\DbLocker\Postgres\PostgresAdvisoryLocker();
$lockId = \Cog\DbLocker\Postgres\PostgresLockKey::create('user', '4');try {
$lock = $locker->acquireSessionLevelLockHandler(
$dbConnection,
$lockId,
\Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum::NonBlocking,
\Cog\DbLocker\Postgres\Enum\PostgresLockAccessModeEnum::Exclusive,
);
if ($lock->wasAcquired) {
// Execute logic if lock was successful
} else {
// Execute logic if lock acquisition has been failed
}
} finally {
$lock->release();
}
```## Changelog
Detailed changes for each release are documented in the [CHANGELOG.md](https://github.com/cybercog/php-db-locker/blob/master/CHANGELOG.md).
## License
- `PHP DB Locker` package is open-sourced software licensed under the [MIT license](LICENSE) by [Anton Komarev].
## 🌟 Stargazers over time
[](https://yhype.me?utm_source=github&utm_medium=cybercog-php-db-locker&utm_content=chart-repository-star-cumulative)
## About CyberCog
[CyberCog] is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.
- [Follow us on Twitter](https://twitter.com/cybercog)
[Anton Komarev]: https://komarev.com
[CyberCog]: https://cybercog.su