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 Seata
its business model, and today we will continue to study the theory of the business model.AT
Seata
XA
The theoretical part comes from
Seata
the official website: http://seata.io/zh-cn/docs/dev/mode/xa-mode.html
Two, the premise
- A database that supports
XA
transactions. Java
application,JDBC
accessing .
3. The overall mechanism
Seata
Within 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 XA
the protocol to manage branch transactions .
-
Execution phase:
-
- Rollback: Business
SQL
operationsXA
are carried out in the branch, whichXA
is guaranteed to be rolled back by the resource's support for the protocol - Persistence:
XA
After the branch is completed, the executionXA prepare
is also guaranteed by the resource's support forXA
the protocol Persistence (that is, any accidents will not cause the situation that cannot be rolled back)
- Rollback: Business
-
Completion stage:
-
- Branch commit: Execute
XA
the branch'scommit
- Branch Rollback: Execute
XA
the branchrollback
- Branch commit: Execute
The timing of branch registration in the
Seata
current mode needs to be before , see chapter 4.2 branch registration for detailsXA
XA
start
4. Working mechanism
4.1 Data source proxy
XA
mode required XAConnection
.
There XAConnection
are two :
- Method 1: Ask the developer to configure
XADataSource
- Method 2: Create according
DataSource
to
The first method increases the cognitive burden on developers and XA
requires learning and using XA
data sources specifically for the mode, which is contrary to the design goal of the transparent XA
programming model.
The second method is more friendly to developers. Like using AT
the mode , developers don't have to care about XA
any 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 JDBC
connection XAConnection
.
The data source proxy mechanism of the analog AT
mode is as follows:
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
Oracle
on the .
Comprehensive consideration, XA
the data source proxy design of the pattern needs to support the first method at the same time: proxy based on XA
the data source.
The data source proxy mechanism of the analog AT
mode is as follows:
4.2 Branch registration
XA
start
Xid
The parameter is required .
This Xid
needs to be associated with the andSeata
of the global transaction so that the commit or rollback of the drive branch is driven.XID
BranchId
TC
XA
Seata
Currently the BranchId
is generated TC
by , so XA
the timing of the pattern branch registration needs to be XA
start
before .
A possible optimization direction in the future:
delay branch registration as much as possible. The similarAT
mode 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 inBranchId
the generation mechanism.BranchId
It is not generated through the branch registration process, but is generated and then takenBranchId
to register the branch.
Five, the use of XA mode
From the programming model, XA
the mode is exactly the same as AT
the mode .
Switch from AT
mode XA
:
seata:
enabled: true
enable-auto-data-source-proxy: true
data-source-proxy-mode: XA
AT
For 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