Skip to content

Commit a76b8f4

Browse files
Update readme on postgresql advistory locking
1 parent c79c8df commit a76b8f4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
4545

4646
[`Mutex::check()`](http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.Mutex.html#_check)
4747
performs a double-checked locking pattern. I.e. if the check fails, no lock
48-
was acquired. Else if the check was true, a lock will be acquired and the
48+
will be acquired. Else if the check was true, a lock will be acquired and the
4949
check will be perfomed as well together with the critical code.
5050

5151
Example:
@@ -72,6 +72,8 @@ The `Mutex` is an abstract class. You will have to chose an implementation:
7272
- [`SemaphoreMutex`](#semaphoremutex)
7373
- [`TransactionalMutex`](#transactionalmutex)
7474
- [`MySQLMutex`](#mysqlmutex)
75+
- [`PgAdvisoryLockMutex`](#pgadvisorylockmutex)
76+
7577

7678
#### CASMutex
7779

@@ -237,7 +239,7 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
237239

238240
The **MySQLMutex** uses MySQL's
239241
[`GET_LOCK`](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock)
240-
function to create lock back end.
242+
function.
241243

242244
It supports time outs. If the connection to the database server is lost or interrupted, the lock is
243245
automatically released.
@@ -259,6 +261,31 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
259261
$bankAccount->setBalance($balance);
260262
});
261263
```
264+
#### PgAdvisoryLockMutex
265+
266+
The **PgAdvisoryLockMutex** uses PostgreSQL's
267+
[advisory locking](https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
268+
functions.
269+
270+
Named locks are offered. PostgreSQL locking functions require integers but the conversion is handled automatically.
271+
272+
No time outs are supported. If the connection to the database server is lost or interrupted, the lock is
273+
automatically released.
274+
275+
```php
276+
$pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
277+
278+
$mutex = new PgAdvisoryLockMutex($pdo, "balance");
279+
$mutex->synchronized(function () use ($bankAccount, $amount) {
280+
$balance = $bankAccount->getBalance();
281+
$balance -= $amount;
282+
if ($balance < 0) {
283+
throw new \DomainException("You have no credit.");
284+
285+
}
286+
$bankAccount->setBalance($balance);
287+
});
288+
```
262289

263290

264291

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "malkusch/lock",
33
"type": "library",
44
"description": "Mutex library for exclusive code execution.",
5-
"keywords": ["mutex", "lock", "flock", "semaphore", "redlock", "memcache", "redis", "cas"],
5+
"keywords": ["mutex", "lock", "locking", "flock", "semaphore", "redlock", "memcache", "redis", "cas", "advisory locks", "mysql", "postgresql"],
66
"homepage": "https://github.com/malkusch/lock",
77
"license": "WTFPL",
88
"authors": [

0 commit comments

Comments
 (0)