java 代码开启事务
@Resource
private DataSourceTransactionManager transactionManager;
DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus status = transactionManager.getTransaction(def); try { List<DistrictAO> districts = districtCustomizedMapper.getCodeAndName(true); if (CollectionUtils.isNotEmpty(districts)) { for (DistrictAO district : districts) { if (district.getAdcode().equals(district.getParentAdcode())) { continue; } if (!treeMap.containsKey(district.getParentAdcode())) { treeMap.put(district.getParentAdcode(), new ArrayList<DistrictAO>()); } treeMap.get(district.getParentAdcode()).add(district); } } } catch (Exception e) { transactionManager.rollback(status); LOG.error(e.getMessage(), e); } finally { try { transactionManager.commit(status); } catch (Exception e) { LOG.error("事务提交异常", e); transactionManager.rollback(status); } }
随笔看心情