Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ implementations or create/extend your own implementation.
- [`MemcachedMutex`](#memcachedmutex)
- [`RedisMutex`](#redismutex)
- [`SemaphoreMutex`](#semaphoremutex)
- [`TransactionalMutex`](#transactionalmutex)
- [`MySQLMutex`](#mysqlmutex)
- [`PostgreSQLMutex`](#PostgreSQLMutex)

Expand Down Expand Up @@ -261,36 +260,6 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
});
```

#### TransactionalMutex

The **TransactionalMutex**
delegates the serialization to the database. The exclusive code is executed within
a transaction. It's up to you to set the correct transaction isolation level.
However if the transaction fails (i.e. a `PDOException` was thrown), the code
will be executed again in a new transaction. Therefore the code must not have
any side effects besides SQL statements. Also the isolation level should be
conserved for the repeated transaction. If the code throws an exception,
the transaction is rolled back and not replayed again.

Example:
```php
$mutex = new TransactionalMutex($pdo);
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
$select = $pdo->prepare(
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
);
$select->execute([$accountId]);
$balance = $select->fetchColumn();

$balance -= $amount;
if ($balance < 0) {
throw new \DomainException('You have no credit');
}
$pdo->prepare('UPDATE account SET balance = ? WHERE id = ?')
->execute([$balance, $accountId]);
});
```

#### MySQLMutex

The **MySQLMutex** uses MySQL's
Expand Down
5 changes: 0 additions & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ parameters:
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
path: 'src/Mutex/TransactionalMutex.php'
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
path: 'tests/Mutex/*Test.php'
identifier: empty.notAllowed
Expand Down
146 changes: 0 additions & 146 deletions src/Mutex/TransactionalMutex.php

This file was deleted.

75 changes: 0 additions & 75 deletions tests/Mutex/MutexConcurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Malkusch\Lock\Mutex\PostgreSQLMutex;
use Malkusch\Lock\Mutex\RedisMutex;
use Malkusch\Lock\Mutex\SemaphoreMutex;
use Malkusch\Lock\Mutex\TransactionalMutex;
use Malkusch\Lock\Util\LockUtil;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Constraint\IsType;
Expand All @@ -32,8 +31,6 @@ class MutexConcurrencyTest extends TestCase
{
/** @var list<string> */
protected static $temporaryFiles = [];
/** @var \PDO|null */
private static $pdo;

#[\Override]
public static function tearDownAfterClass(): void
Expand All @@ -43,24 +40,9 @@ public static function tearDownAfterClass(): void
}
self::$temporaryFiles = [];

self::$pdo = null;

parent::tearDownAfterClass();
}

/**
* Gets a PDO instance.
*/
private static function getPDO(string $dsn, string $user, string $password): \PDO
{
if (self::$pdo === null) {
self::$pdo = new \PDO($dsn, $user, $password);
self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}

return self::$pdo;
}

/**
* Forks, runs code in the children and wait until all finished.
*
Expand Down Expand Up @@ -135,63 +117,6 @@ static function () use ($filename): void {
},
];
}

$makePDOCase = static function (string $dsn, string $user, string $password, string $vendor) {
$pdo = self::getPDO($dsn, $user, $password);

$options = ['mysql' => 'engine=InnoDB'];
$option = $options[$vendor] ?? '';
$pdo->exec('CREATE TABLE IF NOT EXISTS counter(id INT PRIMARY KEY, counter INT) ' . $option);

self::$pdo = null;

return [
static function (int $increment) use ($dsn, $user, $password) {
// This prevents using a closed connection from a child.
if ($increment === 0) {
self::$pdo = null;
}
$pdo = self::getPDO($dsn, $user, $password);
$id = 1;
$select = $pdo->prepare('SELECT counter FROM counter WHERE id = ? FOR UPDATE');
$select->execute([$id]);
$counter = $select->fetchColumn();

$counter += $increment;

$pdo->prepare('UPDATE counter SET counter = ? WHERE id = ?')
->execute([$counter, $id]);

return $counter;
},
static function ($timeout) use ($dsn, $user, $password) {
self::$pdo = null;
$pdo = self::getPDO($dsn, $user, $password);

return new TransactionalMutex($pdo, $timeout);
},
static function () use ($pdo): void {
$pdo->beginTransaction();
$pdo->exec('DELETE FROM counter');
$pdo->exec('INSERT INTO counter VALUES (1, 0)');
$pdo->commit();
},
];
};

if (getenv('MYSQL_DSN')) {
$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
yield 'mysql' => $makePDOCase($dsn, $user, $password, 'mysql');
}

if (getenv('PGSQL_DSN')) {
$dsn = getenv('PGSQL_DSN');
$user = getenv('PGSQL_USER');
$password = getenv('PGSQL_PASSWORD');
yield 'postgres' => $makePDOCase($dsn, $user, $password, 'postgres');
}
}

/**
Expand Down
8 changes: 0 additions & 8 deletions tests/Mutex/MutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Malkusch\Lock\Mutex\PostgreSQLMutex;
use Malkusch\Lock\Mutex\RedisMutex;
use Malkusch\Lock\Mutex\SemaphoreMutex;
use Malkusch\Lock\Mutex\TransactionalMutex;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
Expand Down Expand Up @@ -51,13 +50,6 @@ public static function provideMutexFactoriesCases(): iterable
return new NoMutex();
}];

yield 'TransactionalMutex' => [static function (): Mutex {
$pdo = new \PDO('sqlite::memory:');
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

return new TransactionalMutex($pdo, self::TIMEOUT);
}];

yield 'FlockMutex' => [static function (): Mutex {
$file = fopen(vfsStream::url('test/lock'), 'w');

Expand Down
Loading