Seata XA mode theory learning, use and precautions | Spring Cloud54

I. Introduction

Through the following series of chapters:

docker-compose implements high-availability deployment of Seata Server | Spring Cloud 51

Seata AT mode theory study, transaction isolation and partial source code analysis | Spring Cloud 52

Spring Boot integrates Seata with AT mode distributed transaction example | Spring Cloud 53

We have a deep understanding of the theory and use of Seataits business model, and today we will continue to study the theory of the business model.ATSeataXA

The theoretical part comes from Seatathe official website: http://seata.io/zh-cn/docs/dev/mode/xa-mode.html

Two, the premise

  • A database that supports XAtransactions.
  • Javaapplication, JDBCaccessing .

3. The overall mechanism

SeataWithin the framework of distributed transactions defined by , a transaction modeXA that utilizes transaction resources (databases, message services, etc.) to support the protocol and uses the mechanism of XAthe protocol to manage branch transactions .

insert image description here

  • Execution phase:

    • Rollback: Business SQLoperations XAare carried out in the branch, which XAis guaranteed to be rolled back by the resource's support for the protocol
    • Persistence: XAAfter the branch is completed, the execution XA prepareis also guaranteed by the resource's support for XAthe protocol Persistence (that is, any accidents will not cause the situation that cannot be rolled back)
  • Completion stage:

    • Branch commit: Execute XAthe branch'scommit
    • Branch Rollback: Execute XAthe branchrollback

The timing of branch registration in the Seatacurrent mode needs to be before , see chapter 4.2 branch registration for detailsXAXA start

4. Working mechanism

4.1 Data source proxy

XAmode required XAConnection.

There XAConnectionare two :

  • Method 1: Ask the developer to configureXADataSource
  • Method 2: Create according DataSourceto

The first method increases the cognitive burden on developers and XArequires learning and using XAdata sources specifically for the mode, which is contrary to the design goal of the transparent XAprogramming model.

The second method is more friendly to developers. Like using ATthe mode , developers don't have to care about XAany issues at the level, just keep the local programming model.

We give priority to the design and implementation of the second method: the data source proxy creates a corresponding connection based on the common JDBCconnection XAConnection.

The data source proxy mechanism of the analog ATmode is as follows:

insert image description here

However, the second method has limitations: Compatible correctness cannot be guaranteed.

In effect, this method is doing what a database driver would do. Different manufacturers and different versions of the database driver implementation mechanism are proprietary to the manufacturer. We can only guarantee that the driver is correct on the fully tested driver. The difference in the driver version used by the developer may cause the mechanism to fail.

This is very evident Oracleon the .

Comprehensive consideration, XAthe data source proxy design of the pattern needs to support the first method at the same time: proxy based on XAthe data source.

The data source proxy mechanism of the analog ATmode is as follows:

insert image description here

4.2 Branch registration

XA startXidThe parameter is required .

This Xidneeds to be associated with the andSeata of the global transaction so that the commit or rollback of the drive branch is driven.XIDBranchIdTCXA

SeataCurrently the BranchIdis generated TCby , so XAthe timing of the pattern branch registration needs to be XA startbefore .

A possible optimization direction in the future:
delay branch registration as much as possible. The similar ATmode registers the branch before the local transaction commits, avoiding meaningless branch registration when the branch execution fails.
This optimization direction needs to be coordinated with changes in BranchIdthe generation mechanism. BranchIdIt is not generated through the branch registration process, but is generated and then taken BranchIdto register the branch.

Five, the use of XA mode

From the programming model, XAthe mode is exactly the same as ATthe mode .

Switch from ATmode XA:

seata:
  enabled: true
  enable-auto-data-source-proxy: true
  data-source-proxy-mode: XA

ATFor a complete example of the mode, please refer to the previous chapter: Spring Boot Integration Seata Distributed transaction example using AT mode | Spring Cloud 53

Guess you like

Origin blog.csdn.net/ctwy291314/article/details/130930710