Skip to content

Commit 9c08b96

Browse files
committed
Leave only *Handler API
1 parent a2a2118 commit 9c08b96

File tree

4 files changed

+23
-69
lines changed

4 files changed

+23
-69
lines changed

src/Postgres/LockHandle/SessionLevelLockHandle.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,4 @@ public function release(): bool
5858

5959
return $wasReleased;
6060
}
61-
62-
/**
63-
* Automatically release the lock when the handle is destroyed.
64-
*/
65-
public function __destruct()
66-
{
67-
// TODO: Do we need to
68-
$this->release();
69-
}
7061
}

src/Postgres/LockHandle/TransactionLevelLockHandle.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Postgres/PostgresAdvisoryLocker.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Cog\DbLocker\Postgres\Enum\PostgresLockLevelEnum;
1818
use Cog\DbLocker\Postgres\Enum\PostgresLockWaitModeEnum;
1919
use Cog\DbLocker\Postgres\LockHandle\SessionLevelLockHandle;
20-
use Cog\DbLocker\Postgres\LockHandle\TransactionLevelLockHandle;
2120
use LogicException;
2221
use PDO;
2322

@@ -31,31 +30,19 @@ public function acquireTransactionLevelLock(
3130
PostgresLockKey $key,
3231
PostgresLockWaitModeEnum $waitMode = PostgresLockWaitModeEnum::NonBlocking,
3332
PostgresLockAccessModeEnum $accessMode = PostgresLockAccessModeEnum::Exclusive,
34-
): TransactionLevelLockHandle {
35-
return new TransactionLevelLockHandle(
36-
wasAcquired: $this->acquireLock(
37-
$dbConnection,
38-
$key,
39-
PostgresLockLevelEnum::Transaction,
40-
$waitMode,
41-
$accessMode,
42-
),
33+
): bool {
34+
return $this->acquireLock(
35+
$dbConnection,
36+
$key,
37+
PostgresLockLevelEnum::Transaction,
38+
$waitMode,
39+
$accessMode,
4340
);
4441
}
4542

4643
/**
4744
* Acquire a session-level advisory lock with configurable wait and access modes.
4845
*
49-
* ⚠️ You MUST retain the returned handle in a variable.
50-
* If the handle is not stored and is immediately garbage collected,
51-
* the lock will be released in the lock handle __destruct method.
52-
* @see SessionLevelLockHandle::__destruct
53-
*
54-
* @example
55-
* $handle = $locker->acquireSessionLevelLock(...); // ✅ Lock held
56-
*
57-
* $locker->acquireSessionLevelLock(...); // ❌ Lock immediately released
58-
*
5946
* ⚠️ Transaction-level advisory locks are strongly preferred whenever possible,
6047
* as they are automatically released at the end of a transaction and are less error-prone.
6148
* Use session-level locks only when transactional context is not available.
@@ -94,7 +81,6 @@ public function acquireSessionLevelLock(
9481
* ⚠️ Transaction-level advisory locks are strongly preferred whenever possible,
9582
* as they are automatically released at the end of a transaction and are less error-prone.
9683
* Use session-level locks only when transactional context is not available.
97-
* @see acquireTransactionLevelLock() for preferred locking strategy.
9884
*
9985
* @param PDO $dbConnection Active database connection.
10086
* @param PostgresLockKey $key Lock key to be acquired.
@@ -106,6 +92,8 @@ public function acquireSessionLevelLock(
10692
* @template TReturn
10793
*
10894
* TODO: Cover with tests
95+
* @see acquireTransactionLevelLock() for preferred locking strategy.
96+
*
10997
*/
11098
public function withinSessionLevelLock(
11199
PDO $dbConnection,

test/Integration/Postgres/PostgresAdvisoryLockerTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testItCanTryAcquireLockWithinTransaction(
7171
accessMode: $accessMode,
7272
);
7373

74-
$this->assertTrue($isLockAcquired->wasAcquired);
74+
$this->assertTrue($isLockAcquired);
7575
$this->assertPgAdvisoryLocksCount(1);
7676
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $lockKey, $accessMode);
7777
}
@@ -177,8 +177,7 @@ public function testItCannotAcquireSameLockInTwoConnections(): void
177177
$dbConnection1 = $this->initPostgresPdoConnection();
178178
$dbConnection2 = $this->initPostgresPdoConnection();
179179
$lockKey = PostgresLockKey::create('test');
180-
// TODO: Fix corner-case: without variable its self-destructing instantly
181-
$connection1Lock = $locker->acquireSessionLevelLock(
180+
$locker->acquireSessionLevelLock(
182181
$dbConnection1,
183182
$lockKey,
184183
);
@@ -200,7 +199,7 @@ public function testItCanReleaseLock(
200199
$locker = $this->initLocker();
201200
$dbConnection = $this->initPostgresPdoConnection();
202201
$lockKey = PostgresLockKey::create('test');
203-
$connectionLock = $locker->acquireSessionLevelLock(
202+
$locker->acquireSessionLevelLock(
204203
$dbConnection,
205204
$lockKey,
206205
accessMode: $accessMode,
@@ -236,7 +235,7 @@ public function testItCanNotReleaseLockOfDifferentModes(
236235
$locker = $this->initLocker();
237236
$dbConnection = $this->initPostgresPdoConnection();
238237
$lockKey = PostgresLockKey::create('test');
239-
$connectionLock = $locker->acquireSessionLevelLock(
238+
$locker->acquireSessionLevelLock(
240239
$dbConnection,
241240
$lockKey,
242241
accessMode: $acquireMode,
@@ -272,11 +271,11 @@ public function testItCanReleaseLockTwiceIfAcquiredTwice(): void
272271
$locker = $this->initLocker();
273272
$dbConnection = $this->initPostgresPdoConnection();
274273
$lockKey = PostgresLockKey::create('test');
275-
$connectionLock1 = $locker->acquireSessionLevelLock(
274+
$locker->acquireSessionLevelLock(
276275
$dbConnection,
277276
$lockKey,
278277
);
279-
$connectionLock2 = $locker->acquireSessionLevelLock(
278+
$locker->acquireSessionLevelLock(
280279
$dbConnection,
281280
$lockKey,
282281
);
@@ -320,11 +319,11 @@ public function testItCannotAcquireLockInSecondConnectionAfterOneReleaseTwiceLoc
320319
$dbConnection1 = $this->initPostgresPdoConnection();
321320
$dbConnection2 = $this->initPostgresPdoConnection();
322321
$lockKey = PostgresLockKey::create('test');
323-
$connection1Lock1 = $locker->acquireSessionLevelLock(
322+
$locker->acquireSessionLevelLock(
324323
$dbConnection1,
325324
$lockKey,
326325
);
327-
$connection1Lock2 = $locker->acquireSessionLevelLock(
326+
$locker->acquireSessionLevelLock(
328327
$dbConnection1,
329328
$lockKey,
330329
);
@@ -360,7 +359,7 @@ public function testItCannotReleaseLockIfAcquiredInOtherConnection(): void
360359
$dbConnection1 = $this->initPostgresPdoConnection();
361360
$dbConnection2 = $this->initPostgresPdoConnection();
362361
$lockKey = PostgresLockKey::create('test');
363-
$connection1Lock = $locker->acquireSessionLevelLock(
362+
$locker->acquireSessionLevelLock(
364363
$dbConnection1,
365364
$lockKey,
366365
);
@@ -413,19 +412,19 @@ public function testItCanReleaseAllLocksInConnectionButKeepsOtherConnectionLocks
413412
$lockKey2 = PostgresLockKey::create('test2');
414413
$lockKey3 = PostgresLockKey::create('test3');
415414
$lockKey4 = PostgresLockKey::create('test4');
416-
$connect1Lock1 = $locker->acquireSessionLevelLock(
415+
$locker->acquireSessionLevelLock(
417416
$dbConnection1,
418417
$lockKey1,
419418
);
420-
$connect1Lock2 = $locker->acquireSessionLevelLock(
419+
$locker->acquireSessionLevelLock(
421420
$dbConnection1,
422421
$lockKey2,
423422
);
424-
$connect2Lock3 = $locker->acquireSessionLevelLock(
423+
$locker->acquireSessionLevelLock(
425424
$dbConnection2,
426425
$lockKey3,
427426
);
428-
$connect2Lock4 = $locker->acquireSessionLevelLock(
427+
$locker->acquireSessionLevelLock(
429428
$dbConnection2,
430429
$lockKey4,
431430
);
@@ -461,7 +460,7 @@ public function testItCannotAcquireLockInSecondConnectionIfTakenWithinTransactio
461460
$dbConnection2 = $this->initPostgresPdoConnection();
462461
$lockKey = PostgresLockKey::create('test');
463462
$dbConnection1->beginTransaction();
464-
$connection1Lock = $locker->acquireSessionLevelLock(
463+
$locker->acquireSessionLevelLock(
465464
$dbConnection1,
466465
$lockKey,
467466
);

0 commit comments

Comments
 (0)