1.指定异常 捕获比当前异常小的
@Transactional(rollbackFor={RuntimeException.class, Exception.class})
2.方法抛出异常 事务捕获异常
@Transactional(rollbackFor={Exception.class}) public void renewCompany() throws Exception { try{ //1.删除机构表中的所有数据 prpdcompanywxSynDao.deleteAll(); //2.调用网销接口获取最新的机构表数据 String url = "http://localhost:7001/ebusiness/intefPropertyInsuranceServlet"+"?actionType=getPrpdCompanyInfo"; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); //准备接受返回的字符串 String response = ""; client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { //接收返回字符串 response = method.getResponseBodyAsString(); logger.warn("response:"+response); JSONObject jsonObject = JSON.parseObject(response); JSONArray jsonArray = jsonObject.getJSONArray("data"); List<PrpdcompanywxSyn> dataArr = JSONArray.parseArray(jsonArray.toString(), PrpdcompanywxSyn.class); //3.插入到机构表中 prpdcompanywxSynDao.insertAll(dataArr); /* for(PrpdcompanywxSyn data : dataArr){ prpdcompanywxSynDao.insertSelective(data); }*/ } else { logger.warn("请求网销接口getPrpdCompanyInfo失败" + method.getStatusLine()); } }catch (Exception e){ logger.warn("请求网销接口失败"); e.printStackTrace(); throw new Exception(e.getMessage()); } } }
https://blog.csdn.net/weixin_33656548/article/details/80447460
5. 自动回滚、手动回滚
5.1 大聪明教你学Java | Spring Boot 事务回滚
5.2 自动回滚 方法能够捕获异常 不用throws
@Override @Transactional(rollbackFor={Exception.class}) public void deleteMember(String id, String channelType) { //String updateTime = DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS); //mesChannelDao.updateValidStatus(id,updateTime); // 删除渠道表 mesChannelDao.deleteByPrimaryKey(id); // 删除渠道下所有的白名单 mesIpWhiteDao.deleteIpsByChanelType(channelType); int[] i = null; int i1 = i[1]; }