摘要
工作中有使用Mybatis,碰到一种情况。
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
//dao delete from t1 where jid = 2; ----dao1
//dao insert into t1(sid, jid) where jid=2; --dao2
session.flushstatement --flush1
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
//dao insert into t1() ---dao3
session.flushstatement --- flush2
}
}
这样使用在flush2的时候,mysql报错,lock wait time out
排查中有两个问题先要明确
session嵌套是开了几个事务?
sql哪里锁住了?
开了几个事务?
参考http://blog.csdn.net/hupanfeng/article/details/9238127 分析Mybatis源码,可以发现一个session 对应着一个连接MySQL连接,所以嵌套的session应该是两个事务。并且batchexcutor模式下,session.flushstatement的时候,才会去写入mysql io中,也就是执行flush的时候,才会在mysql server端查询到该事务。