当前位置:网站首页>Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
2022-07-03 10:37:00 【Twilight drainage software】
One 、 Microservices are not normally obtained XID
Check the method :
Call methods in every microservice RootContext.getXID() Check XID
for example , service A Service called B And the service C
Then we can respectively serve A、 service B、 service C Add in the transaction method of
=============== service 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 Global transaction id=================>{}",RootContext.getXID());
//......
}
}
=============== service 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 Global transaction id=================>{}",RootContext.getXID());
//......
}
}
=============== service 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 Global transaction id=================>{}",RootContext.getXID());
//......
}
}
(1) see XID Is it null
(2) In every service XID atypism
If there is one of the above two , It's basically because XID Cause the transaction to fail
terms of settlement :
(1)XID Normally, it is passed to the downstream service through the parameters on the request header , Can pass seta Bag getModifyRequest Under the method headers.put Break points for analysis
(2) Directly pass parameters to downstream services in the calling method
=============== service 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);
//......
}
}
=============== service 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);
//......
}
}
=============== service 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);
//......
}
}
Two 、undo_log Dirty data in the table
When Seata There is no normal end , Each service corresponds to undo_log Table and seata Persistent database brach_table、global_table、lock_table、undo_log There may be dirty data in the table that is not deleted correctly , This causes the service to rollback all the time , But it didn't succeed
** terms of settlement :** eliminate undo_log Tables and seata Persistent database brach_table、global_table、lock_table、undo_log Dirty data in the table
3、 ... and 、Fegin The call uses Fallback Degraded or thrown exceptions are handled globally
In this case, it belongs to seata The service cannot find the exception thrown by the downstream service , The transaction will not trigger rollback
terms of settlement :
(1) adopt GlobalTransactionContext.reload(RootContext.getXID()).rollback() Do a manual rollback
=============== service 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){// stay Mybatis in , The return value is 0 Prove that the insertion failed
// Manual rollback
GlobalTransactionContext.reload(RootContext.getXID()).rollback();
return false;
}
Integer cStatus = serviceC.doC();
if(cStatus == 0){// stay Mybatis in , The return value is 0 Prove that the insertion failed
// Manual rollback
GlobalTransactionContext.reload(RootContext.getXID()).rollback();
return false;
}
//......
}
}
=============== service 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;
}
}
=============== service 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) Through cut class
See official documents for details
边栏推荐
猜你喜欢
随机推荐
Pytorch ADDA code learning notes
High imitation bosom friend manke comic app
Tensorflow—Neural Style Transfer
深度学习入门之自动求导(Pytorch)
Policy gradient Method of Deep Reinforcement learning (Part One)
Jetson TX2 brush machine
Numpy Foundation
Ut2012 learning notes
Preliminary knowledge of Neural Network Introduction (pytorch)
Leetcode刷题---977
神经网络入门之预备知识(PyTorch)
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
6、 Data definition language of MySQL (1)
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
Ut2015 learning notes
Codeup: word replacement
Knowledge map enhancement recommendation based on joint non sampling learning
Leetcode skimming ---10
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Leetcode skimming ---1