Skip to content

Commit e5ac844

Browse files
committed
Rewrite locker API
1 parent ebe5363 commit e5ac844

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ $locker = new \Cog\DbLocker\Locker\PostgresAdvisoryLocker();
3838
$lockId = \Cog\DbLocker\LockId\PostgresLockId::fromKeyValue('user', '4');
3939

4040
$dbConnection->beginTransaction();
41-
$isLockAcquired = $locker->acquireTransactionLevelLock(
41+
$lock = $locker->acquireSessionLevelLockHandler(
4242
$dbConnection,
4343
$lockId,
4444
\Cog\DbLocker\Locker\PostgresAdvisoryLockWaitModeEnum::NonBlocking,
4545
\Cog\DbLocker\Locker\PostgresLockAccessModeEnum::Exclusive,
4646
);
47-
if ($isLockAcquired) {
47+
if ($lock->wasAcquired) {
4848
// Execute logic if lock was successful
4949
} else {
5050
// Execute logic if lock acquisition has been failed
@@ -60,18 +60,21 @@ $dbConnection = new PDO($dsn, $username, $password);
6060
$locker = new \Cog\DbLocker\Locker\PostgresAdvisoryLocker();
6161
$lockId = \Cog\DbLocker\LockId\PostgresLockId::fromKeyValue('user', '4');
6262

63-
$isLockAcquired = $locker->acquireSessionLevelLock(
64-
$dbConnection,
65-
$lockId,
66-
\Cog\DbLocker\Locker\PostgresAdvisoryLockWaitModeEnum::NonBlocking,
67-
\Cog\DbLocker\Locker\PostgresLockAccessModeEnum::Exclusive,
68-
);
69-
if ($isLockAcquired) {
70-
// Execute logic if lock was successful
71-
} else {
72-
// Execute logic if lock acquisition has been failed
63+
try {
64+
$lock = $locker->acquireSessionLevelLockHandler(
65+
$dbConnection,
66+
$lockId,
67+
\Cog\DbLocker\Locker\PostgresAdvisoryLockWaitModeEnum::NonBlocking,
68+
\Cog\DbLocker\Locker\PostgresLockAccessModeEnum::Exclusive,
69+
);
70+
if ($lock->wasAcquired) {
71+
// Execute logic if lock was successful
72+
} else {
73+
// Execute logic if lock acquisition has been failed
74+
}
75+
} finally {
76+
$lock->release();
7377
}
74-
$locker->releaseSessionLevelLock($dbConnection, $lockId);
7578
```
7679

7780
## Changelog

0 commit comments

Comments
 (0)