WJP
919497158@qq.com
🐎!代码级事务 模板方法

代码级事务

@Resource
private DataSourceTransactionManager transactionManager;

public void test(){
  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);
		}
	}
}

wjp

文章作者

发表评论

textsms
account_circle
email

🐎!代码级事务 模板方法
代码级事务 @Resource private DataSourceTransactionManager transactionManager; public void test(){   DefaultTransactionDefinition def = new DefaultTransactionDefinition(…
扫描二维码继续阅读
2021-01-26