JdbcTemplate 开启事务

JdbcTemplate jdbcTemplate = new JdbcTemplate();
//获取DataSource
DataSource dataSource = jdbcTemplate.getDataSource();
//获取Connection
Connection connection = DataSourceUtils.getConnection(dataSource);
//关闭jdbc自动提交
connection.setAutoCommit(false);

try{

  //处理逻辑(新增、修改)
}catch(Exception e){
  //如果报错,则回滚
  connection.rollback();
}finally{
  //处理结束后开启jdbc自动提交,关闭jdbc连接
  connection.setAutoCommit(true);
  if(connection != null){
     connection.close();
  }

}
posted @ 2023-05-10 17:55  Dou豆  阅读(323)  评论(0)    收藏  举报