当前位置:网站首页>Seata分布式事务失效,不生效(事务不回滚)的常见场景
Seata分布式事务失效,不生效(事务不回滚)的常见场景
2022-07-03 09:25:00 【暮晓引流软件】
一、微服务没有正常获取XID
检查方法:
在每个微服务中调用方法 RootContext.getXID() 检查XID
例如,服务A调用了服务B和服务C
那么可以分别在服务A、服务B、服务C的事务方法中添加
===============服务A
@Service
public class ServiceAImpl implements IServiceA
{
private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);
@Autowired
private IServiceB serviceB;
@Autowired
private IServiceC serviceC;
@Override
@GlobalTransactional
@Transactional
public Boolean doA() {
log.info("Seata全局事务id=================>{}",RootContext.getXID());
//......
}
}
===============服务B
@Service
public class ServiceBImpl implements IServiceB
{
private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doB() {
log.info("Seata全局事务id=================>{}",RootContext.getXID());
//......
}
}
===============服务C
@Service
public class ServiceCImpl implements IServiceC
{
private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doC() {
log.info("Seata全局事务id=================>{}",RootContext.getXID());
//......
}
}
(1)查看XID是否为null
(2)每个服务中的XID不一致
如果出现以上两种其中一种,基本可以确定是因为XID导致事务失效
解决办法:
(1)XID正常情况下是通过请求头上的参数传递给下游服务的,可以通过seta包的getModifyRequest方法下的headers.put打断点进行分析
(2)在调用方法中直接传参给下游服务
===============服务A
@Service
public class ServiceAImpl implements IServiceA
{
private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);
@Autowired
private IServiceB serviceB;
@Autowired
private IServiceC serviceC;
@Override
@GlobalTransactional
@Transactional
public Boolean doA() {
String XID = RootContext.getXID();
serviceB.doB(XID);
serviceC.doC(XID);
//......
}
}
===============服务B
@Service
public class ServiceBImpl implements IServiceB
{
private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doB(XID) {
RootContext.bind(XID);
//......
}
}
===============服务C
@Service
public class ServiceCImpl implements IServiceC
{
private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doC(XID) {
RootContext.bind(XID);
//......
}
}
二、undo_log表有脏数据
当Seata没有正常结束时,每个服务对应数据库中的undo_log表和seata持久化数据库的brach_table、global_table、lock_table、undo_log表都有可能有脏数据没有正确删除,从而导致服务一直回滚,却不成功
**解决办法:**清除undo_log表以及seata持久化数据库的brach_table、global_table、lock_table、undo_log表中的脏数据
三、Fegin调用使用了Fallback降级或抛出的异常被全局处理
这种情况下属于seata服务发现不了下游服务抛出的异常,导致事务不会触发回滚
解决办法:
(1)通过 GlobalTransactionContext.reload(RootContext.getXID()).rollback() 进行手动回滚
===============服务A
@Service
public class ServiceAImpl implements IServiceA
{
private static final Logger log = LoggerFactory.getLogger(ServiceAImpl.class);
@Autowired
private IServiceB serviceB;
@Autowired
private IServiceC serviceC;
@Override
@GlobalTransactional
@Transactional
public Boolean doA() {
Integer bStatus = serviceB.doB();
if(bStatus == 0){//在Mybatis中,返回值为0证明插入失败
//手动回滚
GlobalTransactionContext.reload(RootContext.getXID()).rollback();
return false;
}
Integer cStatus = serviceC.doC();
if(cStatus == 0){//在Mybatis中,返回值为0证明插入失败
//手动回滚
GlobalTransactionContext.reload(RootContext.getXID()).rollback();
return false;
}
//......
}
}
===============服务B
@Service
public class ServiceBImpl implements IServiceB
{
private static final Logger log = LoggerFactory.getLogger(ServiceBImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doB() {
//......
Integer insertStatus = serviceBDAO.insert();
return insertStatus;
}
}
===============服务C
@Service
public class ServiceCImpl implements IServiceC
{
private static final Logger log = LoggerFactory.getLogger(ServiceCImpl.class);
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Boolean doC() {
//......
Integer insertStatus = serviceCDAO.insert();
return insertStatus;
}
}
(2)通过切面类
详见官方文档
边栏推荐
- CV learning notes ransca & image similarity comparison hash
- Realize an online examination system from zero
- Standard library header file
- What can I do to exit the current operation and confirm it twice?
- Notes - regular expressions
- Timo background management system
- LeetCode - 715. Range 模块(TreeSet) *****
- 波士顿房价预测(TensorFlow2.9实践)
- CV learning notes alexnet
- 20220602 Mathematics: Excel table column serial number
猜你喜欢

CV learning notes - camera model (Euclidean transformation and affine transformation)

A super cool background permission management system

I really want to be a girl. The first step of programming is to wear women's clothes

1. Finite Markov Decision Process

Hands on deep learning pytorch version exercise solution - 2.6 probability

GAOFAN Weibo app

Leetcode-112: path sum

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
![[LZY learning notes -dive into deep learning] math preparation 2.5-2.7](/img/57/579357f1a07dbe179f355c4a80ae27.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7

LeetCode - 900. RLE 迭代器
随机推荐
Leetcode-404: sum of left leaves
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Opencv interview guide
CV learning notes - feature extraction
CV learning notes - image filter
Synchronous vs asynchronous
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
I really want to be a girl. The first step of programming is to wear women's clothes
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
20220610其他:任务调度器
High imitation wechat
Step 1: teach you to trace the IP address of [phishing email]
Raspberry pie 4B installs yolov5 to achieve real-time target detection
LeetCode - 715. Range module (TreeSet)*****
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
20220602 Mathematics: Excel table column serial number
Out of the box high color background system