-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCombinedTransaction.php
160 lines (145 loc) · 4.49 KB
/
CombinedTransaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php declare(strict_types=1);
namespace Composite\DB;
use Composite\DB\Exceptions\DbException;
use Composite\Entity\AbstractEntity;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
class CombinedTransaction
{
/** @var \Doctrine\DBAL\Connection[] */
private array $transactions = [];
private ?string $lockKey = null;
private ?CacheInterface $cache = null;
/**
* @throws Exceptions\DbException
*/
public function save(AbstractTable $table, AbstractEntity &$entity): void
{
$this->doInTransaction($table, function () use ($table, &$entity) {
$table->save($entity);
});
}
/**
* @param AbstractTable $table
* @param AbstractEntity[] $entities
* @throws DbException
*/
public function saveMany(AbstractTable $table, array $entities): void
{
$this->doInTransaction($table, fn () => $table->saveMany($entities));
}
/**
* @param AbstractTable $table
* @param AbstractEntity[] $entities
* @throws DbException
*/
public function deleteMany(AbstractTable $table, array $entities): void
{
$this->doInTransaction($table, fn () => $table->deleteMany($entities));
}
/**
* @throws Exceptions\DbException
*/
public function delete(AbstractTable $table, AbstractEntity &$entity): void
{
$this->doInTransaction($table, function () use ($table, &$entity) {
$table->delete($entity);
});
}
/**
* @throws Exceptions\DbException
*/
public function try(callable $callback): void
{
try {
$callback();
} catch (\Throwable $e) {
$this->rollback();
throw new Exceptions\DbException($e->getMessage(), 500, $e);
}
}
public function rollback(): void
{
foreach ($this->transactions as $connection) {
$connection->rollBack();
}
$this->finish();
}
/**
* @throws Exceptions\DbException
*/
public function commit(): void
{
foreach ($this->transactions as $connectionName => $connection) {
try {
if (!$connection->commit()) {
throw new Exceptions\DbException("Could not commit transaction for database `$connectionName`");
}
// I have no idea how to simulate failed commit
// @codeCoverageIgnoreStart
} catch (\Throwable $e) {
$this->rollback();
throw new Exceptions\DbException($e->getMessage(), 500, $e);
}
// @codeCoverageIgnoreEnd
}
$this->finish();
}
/**
* Pessimistic lock
* @param string[] $keyParts
* @throws DbException
* @throws InvalidArgumentException
*/
public function lock(CacheInterface $cache, array $keyParts, int $duration = 10): void
{
$this->cache = $cache;
$this->lockKey = $this->buildLockKey($keyParts);
if ($this->cache->get($this->lockKey)) {
throw new DbException("Failed to get lock `{$this->lockKey}`");
}
if (!$this->cache->set($this->lockKey, 1, $duration)) {
throw new DbException("Failed to save lock `{$this->lockKey}`");
}
}
public function releaseLock(): void
{
if (!$this->cache || !$this->lockKey) {
return;
}
$this->cache->delete($this->lockKey);
}
private function doInTransaction(AbstractTable $table, callable $callback): void
{
try {
$connectionName = $table->getConnectionName();
if (empty($this->transactions[$connectionName])) {
$connection = ConnectionManager::getConnection($connectionName);
$connection->beginTransaction();
$this->transactions[$connectionName] = $connection;
}
$callback();
} catch (\Throwable $e) {
$this->rollback();
throw new Exceptions\DbException($e->getMessage(), 500, $e);
}
}
/**
* @param string[] $keyParts
* @return string
*/
private function buildLockKey(array $keyParts): string
{
$keyParts = array_merge(['composite', 'lock'], $keyParts);
$result = implode('.', $keyParts);
if (strlen($result) > 64) {
$result = sha1($result);
}
return $result;
}
private function finish(): void
{
$this->transactions = [];
$this->releaseLock();
}
}