麻烦帮忙看下 貌似只有分库分表的场景下 事务中会出现不会都走主库
使用读写分离+分库分表 的配置下 同一个事务下 不会完全走主库
使用
System.out.println(“事务:” + TransactionSynchronizationManager.getCurrentTransactionName());
打印,确认是在事务下执行
这个麻烦提供一个 demo,方便调查问题。
sharding-jdbc 对一个链接是有一个标志位来表示是否在事务中
多层嵌套事务下会失效
嵌套事务失效,具体行为是什么?
事务中的查询sql 走了读库
希望提供一个可复现的demo,方便排查问题
jdbc 的嵌套事务是基于 savepoint 的,add savepoint support for sharding-jdbc by jingshanglu · Pull Request #16434 · apache/shardingsphere · GitHub 这个修复应该解决了这个问题
这个问题在5.1.2
中也没有解决哦!在结合spring声明式事务的情况下,多层嵌套事务下,会导致读写一致性路由失效。
我的代码如下:
`
@Service
public class A {
@Autowired
private B b;
@Transactional
public void aM() {
b.bM();
}
}
`
`
@Service
public class B {
@Autowired
private C c;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void bM() {
c.cM();
}
}
`
`
@Service
public class C {
@Autowired
private D d;
@Transactional
public void cM() {
d.dM();
}
}
`
`
@Service
public class D {
@Autowired
private HicModelMapper hicModelMapper;
@Transactional
public void dM() {
HicModel hicModel = new HicModel();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取为空
hicModelMapper.selectById(hicModel.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void dM2() {
HicModel hicModel = new HicModel();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取成功
hicModelMapper.selectById(hicModel.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void dM3() {
HicModel hicModel = new HicModel();
HintManager.clear();
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setWriteRouteOnly();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取成功
hicModelMapper.selectById(hicModel.getId());
}
}
}
`
这个问题在中也没有解决哦!在结合spring声明式事务的情况下,多层嵌套事务下,会导致读写一致性路由失效。
我的代码如下:
`
@Service
public class A {
@Autowired
private B b;
@Transactional
public void aM() {
b.bM();
}
}
`
`
@Service
public class B {
@Autowired
private C c;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void bM() {
c.cM();
}
}
`
`
@Service
public class C {
@Autowired
private D d;
@Transactional
public void cM() {
d.dM();
d.dM2();
d.dM3();
}
}
`
`
@Service
public class D {
@Autowired
private HicModelMapper hicModelMapper;
@Transactional
public void dM() {
HicModel hicModel = new HicModel();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取为空
hicModelMapper.selectById(hicModel.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void dM2() {
HicModel hicModel = new HicModel();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取成功
hicModelMapper.selectById(hicModel.getId());
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void dM3() {
HicModel hicModel = new HicModel();
HintManager.clear();
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setWriteRouteOnly();
// 插入成功
int insert = hicModelMapper.insert(hicModel);
// 读取成功
hicModelMapper.selectById(hicModel.getId());
}
}
}
`