1.什么都不设置事务是默认提交的 两次获取的连接是不是一样的
参考文献(重磅):
(542条消息) JdbcTemplate的事务控制_jdbctemplate transactionmanager_DayDayUp丶的博客-CSDN博客
@PostMapping("/pinYin22")
@CrossOrigin
@Transactional
public String pinYin22(HttpServletRequest request) throws Exception {
//开启手动事务
Connection connection = null;
connection = jdbcTemplate.getDataSource().getConnection();
connection.setAutoCommit(false);
PreparedStatement preparedStatement = null;
//try后就算加了事务注解,数据库也不会回滚,所以要手动提交事务
/* try {
if (1==1){
throw new RuntimeException("异常啦");
}
} catch (RuntimeException e) {
e.printStackTrace();
}*/
/*preparedStatement = connection.prepareStatement("INSERT INTO t_test (idd, name) VALUES (?,?)");
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setString(2, "刘备");
preparedStatement.execute();
connection.commit();*/
String importSql1 ="update tablename set name=?";
String replace = importSql1.replace("tablename", "t_test");
jdbcTemplate.update(replace,"232");
connection.rollback();
return "99999";
}
总结:1.不支持手动事务(可以参考链接设置) 2.事务注解可以接管 3.捕获异常后注解不生效
(542条消息) spring事务在try catch时候的执行_srping try catch 事务_TARS_的博客-CSDN博客