20
20
final class PostgresAdvisoryLocker
21
21
{
22
22
/**
23
- * Acquire an advisory lock with configurable scope and mode .
23
+ * Acquire an advisory lock with configurable scope, mode and behavior .
24
24
*/
25
25
public function acquireLock (
26
26
PDO $ dbConnection ,
27
27
PostgresLockId $ postgresLockId ,
28
28
PostgresAdvisoryLockScopeEnum $ scope = PostgresAdvisoryLockScopeEnum::Transaction,
29
- PostgresAdvisoryLockModeEnum $ mode = PostgresAdvisoryLockModeEnum::Try,
29
+ PostgresAdvisoryLockTypeEnum $ type = PostgresAdvisoryLockTypeEnum::NonBlocking,
30
+ PostgresLockModeEnum $ mode = PostgresLockModeEnum::Exclusive,
30
31
): bool {
31
32
if ($ scope === PostgresAdvisoryLockScopeEnum::Transaction && $ dbConnection ->inTransaction () === false ) {
32
33
throw new LogicException (
33
34
"Transaction-level advisory lock ` $ postgresLockId ->humanReadableValue ` cannot be acquired outside of transaction " ,
34
35
);
35
36
}
36
37
37
- $ sql = match ([$ scope , $ mode ]) {
38
- [PostgresAdvisoryLockScopeEnum::Transaction, PostgresAdvisoryLockModeEnum::Try] =>
39
- 'SELECT PG_TRY_ADVISORY_XACT_LOCK(:class_id, :object_id); -- ' . $ postgresLockId ->humanReadableValue ,
40
- [PostgresAdvisoryLockScopeEnum::Transaction, PostgresAdvisoryLockModeEnum::Block] =>
41
- 'SELECT PG_ADVISORY_XACT_LOCK(:class_id, :object_id); -- ' . $ postgresLockId ->humanReadableValue ,
42
- [PostgresAdvisoryLockScopeEnum::Session, PostgresAdvisoryLockModeEnum::Try] =>
43
- 'SELECT PG_TRY_ADVISORY_LOCK(:class_id, :object_id); -- ' . $ postgresLockId ->humanReadableValue ,
44
- [PostgresAdvisoryLockScopeEnum::Session, PostgresAdvisoryLockModeEnum::Block] =>
45
- 'SELECT PG_ADVISORY_LOCK(:class_id, :object_id); -- ' . $ postgresLockId ->humanReadableValue ,
38
+ $ sql = match ([$ scope , $ type , $ mode ]) {
39
+ [
40
+ PostgresAdvisoryLockScopeEnum::Transaction,
41
+ PostgresAdvisoryLockTypeEnum::NonBlocking,
42
+ PostgresLockModeEnum::Exclusive,
43
+ ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK(:class_id, :object_id); ' ,
44
+ [
45
+ PostgresAdvisoryLockScopeEnum::Transaction,
46
+ PostgresAdvisoryLockTypeEnum::Blocking,
47
+ PostgresLockModeEnum::Exclusive,
48
+ ] => 'SELECT PG_ADVISORY_XACT_LOCK(:class_id, :object_id); ' ,
49
+ [
50
+ PostgresAdvisoryLockScopeEnum::Transaction,
51
+ PostgresAdvisoryLockTypeEnum::NonBlocking,
52
+ PostgresLockModeEnum::Share,
53
+ ] => 'SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARE(:class_id, :object_id); ' ,
54
+ [
55
+ PostgresAdvisoryLockScopeEnum::Transaction,
56
+ PostgresAdvisoryLockTypeEnum::Blocking,
57
+ PostgresLockModeEnum::Share,
58
+ ] => 'SELECT PG_ADVISORY_XACT_LOCK_SHARE(:class_id, :object_id); ' ,
59
+ [
60
+ PostgresAdvisoryLockScopeEnum::Session,
61
+ PostgresAdvisoryLockTypeEnum::NonBlocking,
62
+ PostgresLockModeEnum::Exclusive,
63
+ ] => 'SELECT PG_TRY_ADVISORY_LOCK(:class_id, :object_id); ' ,
64
+ [
65
+ PostgresAdvisoryLockScopeEnum::Session,
66
+ PostgresAdvisoryLockTypeEnum::Blocking,
67
+ PostgresLockModeEnum::Exclusive,
68
+ ] => 'SELECT PG_ADVISORY_LOCK(:class_id, :object_id); ' ,
69
+ [
70
+ PostgresAdvisoryLockScopeEnum::Session,
71
+ PostgresAdvisoryLockTypeEnum::NonBlocking,
72
+ PostgresLockModeEnum::Share,
73
+ ] => 'SELECT PG_TRY_ADVISORY_LOCK_SHARE(:class_id, :object_id); ' ,
74
+ [
75
+ PostgresAdvisoryLockScopeEnum::Session,
76
+ PostgresAdvisoryLockTypeEnum::Blocking,
77
+ PostgresLockModeEnum::Share,
78
+ ] => 'SELECT PG_ADVISORY_LOCK_SHARE(:class_id, :object_id); ' ,
46
79
};
80
+ $ sql .= " -- $ postgresLockId ->humanReadableValue " ;
47
81
48
82
$ statement = $ dbConnection ->prepare ($ sql );
49
83
$ statement ->execute (
@@ -57,7 +91,7 @@ public function acquireLock(
57
91
}
58
92
59
93
/**
60
- * Release session- level lock.
94
+ * Release session level advisory lock.
61
95
*/
62
96
public function releaseLock (
63
97
PDO $ dbConnection ,
@@ -84,7 +118,7 @@ public function releaseLock(
84
118
}
85
119
86
120
/**
87
- * Release all session- level locks.
121
+ * Release all session level advisory locks held by the current session .
88
122
*/
89
123
public function releaseAllLocks (
90
124
PDO $ dbConnection ,
0 commit comments