bug描述
spring手动事务没能关闭,而spring默认的事务传播机制是:PROPAGATION_REQUIRED,支持当前事务;如果不存在,创建一个新的。导致后面的Spring事务都往这个事务里面去,还一直提交不了,导致系统崩溃。
@Autowired
private PlatformTransactionManager txManager;
@Autowired
private ShopGroupBuyDao shopGroupBuyDao;
@GetMapping(value = "/transactionDemo")
public void ceshi() {
// 手动开启事务 start
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus status = txManager.getTransaction(def);
// 手动开启事务 end
ShopGroupBuy shopGroupBuy = shopGroupBuyDao.selectOne(new LambdaQueryWrapper<ShopGroupBuy>()
.eq(ShopGroupBuy::getGroupBuyId, 505));
shopGroupBuy.setGroupBuyTheme("wulin11");
int i = shopGroupBuyDao.updateById(shopGroupBuy);
int a = 1 / 0;
try {
// 手动提交事务 start
txManager.commit(status);
// 手动提交事务 end
if (i > 0) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
} catch(Exception e) {
e.printStackTrace();
// 手动回滚事务 start
txManager.rollback(status);
// 手动回滚事务 end
}
}
bug查找过程
这类问题比较难查找,一开始查慢sql,查不到。查执行的process看不到sql语句,看不到具体是哪个sql。后来经大神指点,分析错误日志,才定位到此问题
SELECT * from performance_schema.events_statements_history order by LOCK_TIME desc limit 10;
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX order by trx_started asc
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS where lock_trx_id = 6037541544;
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX where trx_id = 6037541544;