-
Notifications
You must be signed in to change notification settings - Fork 44
Closed
Description
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use _PHPStan_048a4e187\Nette\Neon\Exception;
use DtmClient\Annotation\Barrier;
use DtmClient\Api\ApiInterface;
use DtmClient\TCC;
use DtmClient\TransContext;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Throwable;
class TccController extends AbstractController
{
#[Inject]
protected TCC $tcc;
#[Inject]
protected ApiInterface $api;
public function successCase()
{
try {
$this->tcc->globalTransaction(function (TCC $tcc) {
$tcc->callBranch(
['trans_name' => 'trans_A'],
$this->serviceUri . '/tcc/transA/try',
$this->serviceUri . '/tcc/transA/confirm',
$this->serviceUri . '/tcc/transA/cancel'
);
$tcc->callBranch(
['trans_name' => 'trans_B'],
$this->serviceUri . '/tcc/transB/try/fail',
$this->serviceUri . '/tcc/transB/confirm',
$this->serviceUri . '/tcc/transB/cancel'
);
});
} catch (Throwable $e) {
var_dump($e->getMessage(), $e->getTraceAsString());
}
return TransContext::getGid();
}
public function queryAllCase()
{
$result = $this->api->queryAll(['last_id' => '']);
var_dump($result);
}
public function rollbackCase()
{
try {
$this->tcc->globalTransaction(function (TCC $tcc) {
$tcc->callBranch(
['trans_name' => 'trans_A'],
$this->serviceUri . '/tcc/transA/try',
$this->serviceUri . '/tcc/transA/confirm',
$this->serviceUri . '/tcc/transA/cancel'
);
$tcc->callBranch(
['trans_name' => 'trans_B'],
$this->serviceUri . '/tcc/transB/try/fail',
$this->serviceUri . '/tcc/transB/confirm',
$this->serviceUri . '/tcc/transB/cancel'
);
});
} catch (Throwable $exception) {
// Do Nothing
}
}
#[Barrier]
public function transATry(RequestInterface $request): array
{
echo 'transATry'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
public function transAConfirm(RequestInterface $request): array
{
echo 'transAConfirm'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
#[Barrier]
public function transACancel(RequestInterface $request): array
{
echo 'transACancel'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
#[Barrier]
public function transBTry(): array
{
echo 'transBTry'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
#[Barrier]
public function transBTryFail(ResponseInterface $response)
{
echo 'transBTryFail'.PHP_EOL;
return $response->withStatus(409);
}
public function transBConfirm(): array
{
echo 'transBConfirm'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
#[Barrier]
public function transBCancel(): array
{
echo 'transBCancel'.PHP_EOL;
return [
'dtm_result' => 'SUCCESS',
];
}
}运行结果
transBTryFail方法返回409,但整个事务还是成功了
修改点
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels


