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
Pull in the package through Composer.
composer require cybercog/php-db-locker
$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();
$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();
}
Detailed changes for each release are documented in the CHANGELOG.md.
PHP DB Locker
package is open-sourced software licensed under the MIT license by Anton Komarev.
CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.