Official website: https://www.codingapi.com/docs/home/
principle:
Come to the official website
use:
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
to obtain transaction messages:
Execution process:
determine whether there is a transaction context,
get the parent context if it exists
, 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
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
and get the context information, set a bunch of parameters, blah blah, enter the key point :
DTXServiceExecutor # transactionRunning
Obtain transaction propagation status
Start transaction operation
pre pre-execution
Transaction initiator initializes transaction group
Transaction participants do not execute anything
Business execution
is to execute the content of the method modified by @LcnTransaction annotation
Execution completed successfully:
transaction initiator:
set the state to 1
transaction participant:
join the transaction group
Execution failed:
transaction initiator:
set the status to 0
transaction participant:
delete transaction information
Execution completed:
notify TM
to click inside:
2 How does LCN obtain the operation database connection?
DataSourceAspect
intercepts when acquiring database connections:
perform proxy, and package database connections of different types of transactions by yourself. Take
LCN as an example:
return your own data and connect proxy class:
do some own processing . The general process is
almost like this .
Let’s take a look at the TCC mode again.
If you are interested, you can discuss it together.