Skip to content

PHP application-level database locking mechanisms to implement concurrency control patterns.

License

Notifications You must be signed in to change notification settings

cybercog/php-db-locker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a44fcb6 ยท Dec 4, 2024

History

27 Commits
Aug 26, 2023
May 10, 2022
Jul 19, 2024
Jul 19, 2024
May 10, 2022
May 9, 2022
Aug 26, 2023
May 9, 2022
May 9, 2022
Dec 4, 2024
Aug 26, 2023
Jul 19, 2024
Aug 26, 2023

Repository files navigation

PHP DB Locker

cog-php-db-locker

Releases License

Introduction

PHP application-level database locking mechanisms to implement concurrency control patterns.

Supported drivers:

  • Postgres

Installation

Pull in the package through Composer.

composer require cybercog/php-db-locker

Usage

Postgres

Transaction-level advisory lock

$dbConnection = new PDO($dsn, $username, $password);

$postgresLocker = new PostgresAdvisoryLocker();
$postgresLockId = PostgresLockId::fromLockId(new LockId('user', '4'));

$dbConnection->beginTransaction();
$isLockAcquired = $postgresLocker->acquireLockWithinTransaction($dbConnection, $postgresLockId);
if ($isLockAcquired) {
    // Execute logic if lock was successful
} else {
    // Execute logic if lock acquisition has been failed
}
$dbConnection->commit();

Session-level advisory lock

$dbConnection = new PDO($dsn, $username, $password);

$postgresLocker = new PostgresAdvisoryLocker();
$postgresLockId = PostgresLockId::fromLockId(new LockId('user', '4'));

$isLockAcquired = $postgresLocker->acquireLock($dbConnection, $postgresLockId);
if ($isLockAcquired) {
    // Execute logic if lock was successful
} else {
    // Execute logic if lock acquisition has been failed
}
$postgresLocker->releaseLock($dbConnection, $postgresLockId);

Changelog

Detailed changes for each release are documented in the CHANGELOG.md.

License

๐ŸŒŸ Stargazers over time

Stargazers over time

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion.

CyberCog