Skip to content

Commit 66a2998

Browse files
Fixed: Saga 单例情况下, 导致concurrent和orders持久化
Saga 单例情况下, 导致concurrent和orders持久化
2 parents 01fd152 + ded4c2c commit 66a2998

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/Saga.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
use DtmClient\Api\ApiInterface;
1313
use DtmClient\Constants\Protocol;
1414
use DtmClient\Constants\TransType;
15+
use DtmClient\Context\Context;
1516
use DtmClient\Exception\UnsupportedException;
1617
use Google\Protobuf\Internal\Message;
1718

1819
class Saga extends AbstractTransaction
1920
{
20-
protected array $orders = [];
21+
protected const CONCURRENT = self::class . '.concurrent';
2122

22-
protected bool $concurrent = false;
23+
protected const ORDERS = self::class . '.orders';
2324

2425
protected ApiInterface $api;
2526

@@ -33,6 +34,8 @@ public function init(?string $gid = null)
3334
if ($gid === null) {
3435
$gid = $this->generateGid();
3536
}
37+
Context::set(static::CONCURRENT, false);
38+
Context::set(static::ORDERS, []);
3639
TransContext::init($gid, TransType::SAGA, '');
3740
}
3841

@@ -60,13 +63,15 @@ public function add(string $action, string $compensate, array|object $payload):
6063

6164
public function addBranchOrder(int $branch, array $preBranches): static
6265
{
63-
$this->orders[$branch] = $preBranches;
66+
$orders = Context::get(static::ORDERS, []);
67+
$orders[$branch] = $preBranches;
68+
Context::set(static::ORDERS, $orders);
6469
return $this;
6570
}
6671

6772
public function enableConcurrent()
6873
{
69-
$this->concurrent = true;
74+
Context::set(static::CONCURRENT, true);
7075
}
7176

7277
public function submit()
@@ -77,10 +82,10 @@ public function submit()
7782

7883
public function addConcurrentContext()
7984
{
80-
if ($this->concurrent) {
85+
if (Context::get(static::CONCURRENT, false)) {
8186
TransContext::setCustomData(json_encode([
82-
'concurrent' => $this->concurrent,
83-
'orders' => $this->orders ?: null,
87+
'concurrent' => Context::get(static::CONCURRENT),
88+
'orders' => Context::get(static::ORDERS) ?: null,
8489
]));
8590
}
8691
}

0 commit comments

Comments
 (0)