Conditions | 5 |
Paths | 9 |
Total Lines | 43 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
28 | public function transaction(Closure $program) |
||
29 | { |
||
30 | /** |
||
31 | * @var Pool $pool |
||
32 | * @var TransAPI|Executable|Poolable $link |
||
33 | */ |
||
34 | $pool = $this->assigned(); |
||
|
|||
35 | $link = yield $pool->select(); |
||
36 | |||
37 | // assigned waiters |
||
38 | $start = Promise::deferred(); |
||
39 | $commit = Promise::deferred(); |
||
40 | $rollback = Promise::deferred(); |
||
41 | |||
42 | // automatic release after commit |
||
43 | $commit->then(static function () use ($link) { |
||
44 | $link->release(); |
||
45 | }); |
||
46 | |||
47 | // automatic release after rollback |
||
48 | $rollback->then(static function () use ($link) { |
||
49 | $link->release(); |
||
50 | }); |
||
51 | |||
52 | // create session for transaction |
||
53 | $session = new TransEXE($link, $start, $commit, $rollback); |
||
54 | |||
55 | // wait for transaction started |
||
56 | yield $session->begin(); |
||
57 | |||
58 | // flows |
||
59 | try { |
||
60 | // call user program |
||
61 | $finished = yield $program($session); |
||
62 | // automatic commit |
||
63 | $commit->pended() && $rollback->pended() && yield $session->commit(); |
||
64 | // finished |
||
65 | return $finished; |
||
66 | } catch (Throwable $e) { |
||
67 | // automatic rollback |
||
68 | $rollback->pended() && yield $session->rollback(); |
||
69 | // throw to next |
||
70 | throw $e; |
||
71 | } |
||
74 |