Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do you use PHP to complete a distributed transaction TCC

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you how to use PHP to complete a distributed transaction TCC, the content is very detailed, interested friends can refer to, hope to be helpful to you.

What is TCC,TCC is the abbreviation of Try, Confirm and Cancel, which was first put forward by a paper called "Life beyond Distributed Transactions:an Apostate's Opinion" published by Pat Helland in 2007.

TCC composition

TCC is divided into three stages.

Try phase: try to execute, complete all business checks (consistency), reserve necessary business resources (quasi-isolation)

Confirm phase: if the Try of all branches is successful, go to the Confirm phase. Confirm really executes the business, does not make any business inspection, and only uses the business resources reserved during the Try phase.

Cancel phase: if one of the Try of all branches fails, then go to the Cancel phase. Cancel releases the business resources reserved during the Try phase.

In TCC distributed transactions, there are three roles, just like classic XA distributed transactions:

An AP/ application that initiates a global transaction and defines which transaction branches the global transaction contains

RM/ resource manager, responsible for the management of branch transaction resources

TM/ transaction manager, responsible for coordinating the correct execution of global transactions, including Confirm,Cancel execution, and handling network exceptions

If we want to conduct a business similar to inter-bank transfer, TransOut and TransIn respectively in different microservices, the typical timing diagram of a successfully completed TCC transaction is as follows:

TCC practice

Let's do a specific development of a TCC transaction

At present, the open source framework available for TCC is mainly the Java language, with seata as the representative. Our example uses nodejs, and the distributed transaction framework is dtm, which supports distributed transactions with great elegance. Let's explain the composition of TCC in detail.

Let's write a specific Try/Confirm/Cancel handler.

$vega- > handleFunc ('/ api/TransOutTry', function (Mix\ Vega\ Context $ctx) {var_dump ('TransOutTry', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody ()); $ctx- > JSON (200, [' result' = > SUCCESS'];})-> methods ('POST'); $vega- > handleFunc (' / api/TransOutConfirm', function (Mix\ Vega\ Context $ctx) {var_dump ('TransOutConfirm', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody () $ctx- > JSON (200, ['result' = >' SUCCESS']);})-> methods ('POST'); $vega- > handleFunc (' / api/TransOutCancel', function (Mix\ Vega\ Context $ctx) {var_dump ('TransOutCancel', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody ()); $ctx- > JSON (200, [' result' = > 'SUCCESS']);})-> methods (' POST') $vega- > handleFunc ('/ api/TransInTry', function (Mix\ Vega\ Context $ctx) {var_dump ('TransInTry', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody ()); $ctx- > JSON (200, [' result' = > SUCCESS'];})-> methods ('POST'); $vega- > handleFunc (' / api/TransInConfirm', function (Mix\ Vega\ Context $ctx) {var_dump ('TransInConfirm', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody () $ctx- > JSON (200, ['result' = >' SUCCESS']);})-> methods ('POST'); $vega- > handleFunc (' / api/TransInCancel', function (Mix\ Vega\ Context $ctx) {var_dump ('TransInCancel', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody ()); $ctx- > JSON (200, [' result' = > 'SUCCESS']);})-> methods (' POST')

At this point, the handling functions of each subtransaction have been OK, and then the TCC transaction is opened and branch calls are made.

Dtmcli\ tccGlobalTransaction ($dtm, function ($tcc) use ($svc) {/ * * @ var Dtmcli\ Tcc $tcc * / $req = ['amount' = > 30]; $tcc- > callBranch ($req, $svc. '/ TransOutTry', $svc. '/ TransOutConfirm', $svc. '/ TransOutCancel'); $tcc- > callBranch ($req, $svc. '/ TransInTry', $svc. '/ TransInConfirm', $svc. '/ TransInCancel');})

At this point, a complete TCC distributed transaction is written.

If you want to run a successful example in its entirety, refer to this example, yedf/dtmcli-php-sample, and make it very easy to run

# deployment to start dtm# requires a rollback of git clone https://github.com/yedf/dtmcd dtmdocker-compose up# above docker version 18 from the command line https://github.com/yedf/dtmcli-php-sample.gitcd dtmcli-php-samplecomposer installphp demo.php startTCC

What if the bank finds that the account of user 2 is abnormal and the return fails when the amount is ready to be transferred to user 2? We can make TransIn return a failure to simulate this situation.

$vega- > handleFunc ('/ api/TransInTry', function (Mix\ Vega\ Context $ctx) {var_dump ('TransInTry', $ctx- > request- > getQueryParams (), $ctx- > request- > getParsedBody ()); $ctx- > JSON (200, [' result' = > 'FAILURE']);})-> methods (' POST')

We give the sequence diagram of the transaction failure interaction.

The difference between this and a successful TCC is that when a sub-transaction returns a failure, the global transaction is rolled back later, and the Cancel operation of each sub-transaction is called to ensure that all global transactions are rolled back.

In the TCC transaction mode, many readers will ask, what happens if Confirm/Cancel fails? This is a good question and indicates that you are thinking deeply about the TCC transaction model. The first is a temporary failure, such as a network failure, application or database downtime, which retries and returns success. Another case is business failure. According to TCC's protocol, resources are locked in the first stage to ensure that there are enough resources for Confirm/Cancel to execute. That is to say, logically, Confirm/Cancel is not allowed to return business failure. If a business failure occurs, it is bug, which requires developers to repair bug manually.

On how you use PHP to complete a distributed transaction TCC to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report