Distributed Transactions: From Theory to Practice (III)

  • Distributed Transactions: From Theory to Practice (PART I)
  • Distributed Transactions: From Theory to Practice (II)

Following on from the previous two articles, let’s continue our discussion of Seata’s TCC schema.

TCC

The principle of review

A brief review of the principles of TCC refers to ant Financial’s blog [1]

Normal transaction logic

  1. try
  2. Cancel or confirm

Allow empty rollback

1 Failed to try. 2 Empty cancel is performed

A scenario in which the TCC service receives a Cancel request without receiving a Try request is called a null rollback; Empty rollback is a common occurrence in production environments, and users should implement TCC services to allow the execution of empty rollback, that is, return success upon receipt of empty rollback.

Anti-suspension control

  1. Try a timeout
  2. Cancel success
  3. Try try again
  4. Confirm or Cancel is never executed, causing suspension.

Also, in addition to the above, just like AT, you have to pay attention to idempotent control.

Code implementation

Let’s start with abstract processes and considerations

  • Define the transaction interface (TCC) for your prepare, COMMIT, and ROLLBACK methods.
  • Note the @localTCC annotation (necessary) for TCC in SpringCloud+Feign mode
  • @twophasebusinessaction (required) annotation method try. Name is the name of the method, commitMethod is the name of the commitMethod, rollbackMethod is the name of the rollbackMethod.
  • The BusinessActionContext is the transaction context of SEATA TCC, which holds some key data for TCC transactions. The BusinessActionContext object can be directly used as a parameter to the commit and rollBAKc methods, and Seata will automatically inject parameters:

  • @ BusinessActionContextParameter into the parameter of the annotations used to modify the Try method was modified into the ginseng can Commit and Rollback methods through BusinessActionContext access

Let’s string together a business scenario from the official example [2].

This is a transfer operation:

Interface definition:

Join @GlobalTransactional in the transaction invocation entry

  • The withholding participant is first prepared to deduct money, and if that fails, local and distributed transactions are rolled back

    Let’s look at the implementation of the try method for withholding money:

  • If this fails, the local and distributed transactions will be rolled back. Look at the implementation of the incrementing try method:


  • If both steps are successful, the respective commit methods will be called, and if the methods are abnormal the firstAction commit will be retried

    SecondActin submits plus money

  • If the firstAction and secondAction try methods fail, their respective rollback methods will be called automatically:



conclusion

Overall, TCC schema encoding is relatively simple, but there are a few points to note:

  • Design three approaches to TCC according to the business

  • Interface power etc.

  • Cancle (orderNo, orderNo, orderNo, orderNo, orderNo, orderNo, orderNo, orderNo, orderNo, orderNo, orderNo


  if(orderNo==null || order==null) {    return;
  }

Copy the code
  • Anti-suspension control reference [3]) can insert a transaction control record with rollback status during phase 2 execution, so that when phase 1 is executed, the record will be read first. If the record exists, it will be considered that the phase 2 rollback operation has been performed and the try method will not be executed.

The resources

[1]

The ant gold take TCC blog: https://tech.antfin.com/community/articles/519


[2]

Seata example: https://github.com/seata/seata-samples


[3]

CSDN reference: https://blog.csdn.net/hosaos/article/details/89136666

More exciting content please pay attention to the public account “small box technology sharing”

Chat 🏆 technology project stage v | distributed those things…