cloudServiceApi.saveSsmbd(t)是通过openfeign调用的,里边的service方法加了@Transactional注解。外层事务回滚了,内层事务没有回滚,这是什么原因?
不知道你这个问题是否已经解决, 如果还没有解决的话:首先,需要了解Seata的分布式事务是如何实现的。Seata基于两阶段提交协议来实现分布式事务,即XA协议。在分布式事务中,存在多个参与者(即各个服务),其中有一个协调者(即Seata Server)来控制整个事务流程,具有事务开启、提交、回滚等功能。 针对这个问题,可能是由于被调用的服务没有正确地加入Seata的事务管理中,导致内部事务未能受到外部事务的影响。解决该问题需要进行如下操作: 1. 确保所调用的服务已经引入了Seata相关的依赖和配置文件,并且已经正确地进行了服务注册和配置。 2. 在@FeignClient注解中添加configuration属性,指定Feign配置类,例如:
@FeignClient(value = "cloud-service", configuration = CloudServiceFeignConfiguration.class)
@Configuration
public class CloudServiceFeignConfiguration {
@Bean
public RequestInterceptor addXIDInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
String xid = RootContext.getXID();
if (StringUtils.isNotEmpty(xid)) {
requestTemplate.header(RootContext.KEY_XID, xid);
}
}
};
}
}
@Service
public class CloudServiceImpl implements CloudService {
@Override
@GlobalTransactional(timeoutMills = 300000, name = "cloud-service-tx")
public void saveSsmbd(Ssmbd t) {
ssmbdDao.insert(t);
}
}
@Service
public class CloudServiceImpl implements CloudService {
@Override
@GlobalTransactional(timeoutMills = 300000, name = "cloud-service-tx")
public void saveSsmbd(Ssmbd t) {
try {
ssmbdDao.insert(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
通过以上操作,应该能够解决分布式事务未回滚的问题。如果仍然存在问题,可以尝试查看Seata Server的日志,检查是否有异常或者错误信息。