在SpringBoot 中,使用事务非常简单,只需在方法上面加入 @Transactional 注解就可以实现。也可加在类上,此时则类中所有方法都支持事务。
而当我使用下面代码时,发现事务却没有回滚,异常之前的数据仍然插入了数据库
@RequestMapping("/percredential")
public String perCredential(HttpServletRequest request, HttpServletResponse response) throws IOException {
//创建session
Session session = null;
session = (Session)entityManager.unwrap(Session.class);
try{
session.createSQLQuery("INSERT into test values(6,'1')").executeUpdate();
session.createSQLQuery("INSERT into test values(6,'1')").executeUpdate();
}catch(Exception e){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
return "per/perCredential/perCredential";
}
在各种尝试之后,最后发现是mysql中的表类型是MyISAM,而MyISAM存储引擎的一个特点就是不支持事务