Distributed transaction framework LCN usage, principle and source code reading

Official website: https://www.codingapi.com/docs/home/

principle:

Come to the official website
insert image description here

use:

insert image description here
A eureka.
One TM and two RMs are order service and payment service respectively.
For detailed configuration, please visit the official website.

Order Service Controller:

    @PostMapping("/add-order")
    @Transactional(rollbackFor = Exception.class)
    @LcnTransaction
    public String add(@RequestBody TblOrder bean){
    
    

        JSONObject date = new JSONObject();
        date.put("payName",bean.getOrderName()+"pay");

        restTemplate.postForEntity("http://lcn-pay/add-pay",date,String.class);
        int i = 1/0;
        tblOrderDao.insert(bean);
        return "新增订单成功";
    }

Payment Service Controller:

    @PostMapping("/add-pay")
    @Transactional(rollbackFor = Exception.class)
    @LcnTransaction
    public String addPay(@RequestBody TblPay bean){
    
    
        tblPayDao.insert(bean);
//        int i = 1/0;
        return "新增支付成功";

    }

Just add @LcnTransaction

source code

1 entry:

Use the aop aspect class to intercept
TransactionAspect
insert image description here
insert image description here
to obtain transaction messages:
insert image description here
Execution process:
determine whether there is a transaction context,
get the parent context if it exists
insert image description here
, create it if not,
and determine whether it is the transaction initiator
Put the context information in the cache,
click in and it will be a Map<String, Object> singlePropCache
insert image description here
What is the general framework of startxx
Most of them will have
endXX
clearXX
cleanXX
destoryXX
Pull down the code:
find the destruction method
After execution, delete the context information in finally
insert image description here
and get the context information, set a bunch of parameters, blah blah, enter the key point :
insert image description here
DTXServiceExecutor # transactionRunning
insert image description here
Obtain transaction propagation status
insert image description here
Start transaction operation
pre pre-execution
Transaction initiator initializes transaction group
Transaction participants do not execute anything
insert image description here
Business execution
is to execute the content of the method modified by @LcnTransaction annotation
insert image description here
insert image description here
Execution completed successfully:
insert image description here
transaction initiator:
set the state to 1
insert image description here
transaction participant:
join the transaction group
insert image description here
Execution failed:
transaction initiator:
set the status to 0
insert image description here
transaction participant:
delete transaction information
insert image description here
Execution completed:
notify TM
insert image description here
to click inside:
insert image description here

2 How does LCN obtain the operation database connection?

DataSourceAspect
intercepts when acquiring database connections:
insert image description here
perform proxy, and package database connections of different types of transactions by yourself. Take
insert image description here
LCN as an example:
return your own data and connect proxy class:
insert image description here
do some own processing . The general process is
insert image description here
insert image description here
almost like this .
Let’s take a look at the TCC mode again.
If you are interested, you can discuss it together.

Guess you like

Origin blog.csdn.net/lx9876lx/article/details/121197022