Skip to content

Commit df6a8f6

Browse files
authored
Drop TransactionalMutex class (#73)
1 parent c9ad6c1 commit df6a8f6

File tree

6 files changed

+0
-458
lines changed

6 files changed

+0
-458
lines changed

README.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ implementations or create/extend your own implementation.
167167
- [`MemcachedMutex`](#memcachedmutex)
168168
- [`RedisMutex`](#redismutex)
169169
- [`SemaphoreMutex`](#semaphoremutex)
170-
- [`TransactionalMutex`](#transactionalmutex)
171170
- [`MySQLMutex`](#mysqlmutex)
172171
- [`PostgreSQLMutex`](#PostgreSQLMutex)
173172

@@ -261,36 +260,6 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
261260
});
262261
```
263262

264-
#### TransactionalMutex
265-
266-
The **TransactionalMutex**
267-
delegates the serialization to the database. The exclusive code is executed within
268-
a transaction. It's up to you to set the correct transaction isolation level.
269-
However if the transaction fails (i.e. a `PDOException` was thrown), the code
270-
will be executed again in a new transaction. Therefore the code must not have
271-
any side effects besides SQL statements. Also the isolation level should be
272-
conserved for the repeated transaction. If the code throws an exception,
273-
the transaction is rolled back and not replayed again.
274-
275-
Example:
276-
```php
277-
$mutex = new TransactionalMutex($pdo);
278-
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
279-
$select = $pdo->prepare(
280-
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
281-
);
282-
$select->execute([$accountId]);
283-
$balance = $select->fetchColumn();
284-
285-
$balance -= $amount;
286-
if ($balance < 0) {
287-
throw new \DomainException('You have no credit');
288-
}
289-
$pdo->prepare('UPDATE account SET balance = ? WHERE id = ?')
290-
->execute([$balance, $accountId]);
291-
});
292-
```
293-
294263
#### MySQLMutex
295264

296265
The **MySQLMutex** uses MySQL's

phpstan.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ parameters:
1616
identifier: if.condNotBoolean
1717
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
1818
count: 1
19-
-
20-
path: 'src/Mutex/TransactionalMutex.php'
21-
identifier: if.condNotBoolean
22-
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
23-
count: 1
2419
-
2520
path: 'tests/Mutex/*Test.php'
2621
identifier: empty.notAllowed

src/Mutex/TransactionalMutex.php

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

tests/Mutex/MutexConcurrencyTest.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Malkusch\Lock\Mutex\PostgreSQLMutex;
1313
use Malkusch\Lock\Mutex\RedisMutex;
1414
use Malkusch\Lock\Mutex\SemaphoreMutex;
15-
use Malkusch\Lock\Mutex\TransactionalMutex;
1615
use Malkusch\Lock\Util\LockUtil;
1716
use PHPUnit\Framework\Attributes\DataProvider;
1817
use PHPUnit\Framework\Constraint\IsType;
@@ -32,8 +31,6 @@ class MutexConcurrencyTest extends TestCase
3231
{
3332
/** @var list<string> */
3433
protected static $temporaryFiles = [];
35-
/** @var \PDO|null */
36-
private static $pdo;
3734

3835
#[\Override]
3936
public static function tearDownAfterClass(): void
@@ -43,24 +40,9 @@ public static function tearDownAfterClass(): void
4340
}
4441
self::$temporaryFiles = [];
4542

46-
self::$pdo = null;
47-
4843
parent::tearDownAfterClass();
4944
}
5045

51-
/**
52-
* Gets a PDO instance.
53-
*/
54-
private static function getPDO(string $dsn, string $user, string $password): \PDO
55-
{
56-
if (self::$pdo === null) {
57-
self::$pdo = new \PDO($dsn, $user, $password);
58-
self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
59-
}
60-
61-
return self::$pdo;
62-
}
63-
6446
/**
6547
* Forks, runs code in the children and wait until all finished.
6648
*
@@ -135,63 +117,6 @@ static function () use ($filename): void {
135117
},
136118
];
137119
}
138-
139-
$makePDOCase = static function (string $dsn, string $user, string $password, string $vendor) {
140-
$pdo = self::getPDO($dsn, $user, $password);
141-
142-
$options = ['mysql' => 'engine=InnoDB'];
143-
$option = $options[$vendor] ?? '';
144-
$pdo->exec('CREATE TABLE IF NOT EXISTS counter(id INT PRIMARY KEY, counter INT) ' . $option);
145-
146-
self::$pdo = null;
147-
148-
return [
149-
static function (int $increment) use ($dsn, $user, $password) {
150-
// This prevents using a closed connection from a child.
151-
if ($increment === 0) {
152-
self::$pdo = null;
153-
}
154-
$pdo = self::getPDO($dsn, $user, $password);
155-
$id = 1;
156-
$select = $pdo->prepare('SELECT counter FROM counter WHERE id = ? FOR UPDATE');
157-
$select->execute([$id]);
158-
$counter = $select->fetchColumn();
159-
160-
$counter += $increment;
161-
162-
$pdo->prepare('UPDATE counter SET counter = ? WHERE id = ?')
163-
->execute([$counter, $id]);
164-
165-
return $counter;
166-
},
167-
static function ($timeout) use ($dsn, $user, $password) {
168-
self::$pdo = null;
169-
$pdo = self::getPDO($dsn, $user, $password);
170-
171-
return new TransactionalMutex($pdo, $timeout);
172-
},
173-
static function () use ($pdo): void {
174-
$pdo->beginTransaction();
175-
$pdo->exec('DELETE FROM counter');
176-
$pdo->exec('INSERT INTO counter VALUES (1, 0)');
177-
$pdo->commit();
178-
},
179-
];
180-
};
181-
182-
if (getenv('MYSQL_DSN')) {
183-
$dsn = getenv('MYSQL_DSN');
184-
$user = getenv('MYSQL_USER');
185-
$password = getenv('MYSQL_PASSWORD');
186-
yield 'mysql' => $makePDOCase($dsn, $user, $password, 'mysql');
187-
}
188-
189-
if (getenv('PGSQL_DSN')) {
190-
$dsn = getenv('PGSQL_DSN');
191-
$user = getenv('PGSQL_USER');
192-
$password = getenv('PGSQL_PASSWORD');
193-
yield 'postgres' => $makePDOCase($dsn, $user, $password, 'postgres');
194-
}
195120
}
196121

197122
/**

tests/Mutex/MutexTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Malkusch\Lock\Mutex\PostgreSQLMutex;
1616
use Malkusch\Lock\Mutex\RedisMutex;
1717
use Malkusch\Lock\Mutex\SemaphoreMutex;
18-
use Malkusch\Lock\Mutex\TransactionalMutex;
1918
use org\bovigo\vfs\vfsStream;
2019
use PHPUnit\Framework\Attributes\DataProvider;
2120
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
@@ -51,13 +50,6 @@ public static function provideMutexFactoriesCases(): iterable
5150
return new NoMutex();
5251
}];
5352

54-
yield 'TransactionalMutex' => [static function (): Mutex {
55-
$pdo = new \PDO('sqlite::memory:');
56-
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
57-
58-
return new TransactionalMutex($pdo, self::TIMEOUT);
59-
}];
60-
6153
yield 'FlockMutex' => [static function (): Mutex {
6254
$file = fopen(vfsStream::url('test/lock'), 'w');
6355

0 commit comments

Comments
 (0)