在service中循环save数据,后面出错,前面的不回滚

kangwei_ch 2019-07-21 11:19:35
All
我配了一个全局事务,在service中循环save数据,后面出错,前面的不回滚;是什么原因呀?

全局事务如下:
package com.mobile.framework.aop;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;


@Configuration
@Aspect
public class TransactionManagerConfig {
private static final int TX_METHOD_TIMEOUT=5;
/*定义切点变量:拦截test.spring包下所有类的所有方法,返回值类型任意的方法*/
private static final String AOP_POINTCUT_EXPRESSION="execution ( public * com.mobile.*.service.impl..*(..) )";
@Autowired
private PlatformTransactionManager transactionManager;


@Bean
public TransactionInterceptor TxAdvice(){
/*事务管理规则,声明具备事务管理的方法名*/
NameMatchTransactionAttributeSource source=new NameMatchTransactionAttributeSource();
/*只读事物、不做更新删除等*/
/*当前存在事务就用当前的事务,当前不存在事务就创建一个新的事务*/
RuleBasedTransactionAttribute readOnlyRule=new RuleBasedTransactionAttribute();
/*设置当前事务是否为只读事务,true为只读*/
readOnlyRule.setReadOnly(true);
/* transactiondefinition 定义事务的隔离级别;
* PROPAGATION_NOT_SUPPORTED事务传播级别5,以非事务运行,如果当前存在事务,则把当前事务挂起*/
readOnlyRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);

RuleBasedTransactionAttribute requireRule=new RuleBasedTransactionAttribute();
/*抛出异常后执行切点回滚*/
requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(RuntimeException.class)));
/*PROPAGATION_REQUIRED:事务隔离性为1,若当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。 */
requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
/*设置事务失效时间,如果超过5秒,则回滚事务*/
requireRule.setTimeout(TX_METHOD_TIMEOUT);
Map<String,TransactionAttribute> txMap=new HashMap<>();

txMap.put("add*",requireRule);
txMap.put("save*", requireRule);
txMap.put("edit*", requireRule);
txMap.put("insert*",requireRule);
txMap.put("update*",requireRule);
txMap.put("delete*",requireRule);
txMap.put("del*",requireRule);
txMap.put("remove*",requireRule);
txMap.put("copyTbl*",requireRule);
txMap.put("grant*",requireRule);

txMap.put("get*",readOnlyRule);
txMap.put("query*", readOnlyRule);
txMap.put("find*", readOnlyRule);
txMap.put("select*",readOnlyRule);
txMap.put("*", readOnlyRule);
source.setNameMap(txMap);
TransactionInterceptor txAdvice=new TransactionInterceptor(transactionManager, source);
return txAdvice;
}


@Bean
public Advisor txAdviceAdvisor(){
/* 声明切点的面
* 切面(Aspect):切面就是通知和切入点的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。
* */
AspectJExpressionPointcut pointcut=new AspectJExpressionPointcut();
/*声明和设置需要拦截的方法,用切点语言描写*/
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
/*设置切面=切点pointcut+通知TxAdvice*/
return new DefaultPointcutAdvisor(pointcut, TxAdvice());
}
}


Service中方法为:
@Override
public void saveMorningKanbanItem(List<String[]> l) {

if (l!=null && l.size()>0 ) {
for (int i=0; i<l.size(); i++) {
String[] arr = l.get(i);
Operation_morningKanbanDO obj = new Operation_morningKanbanDO(Integer.parseInt(arr[0]), arr[1], Integer.parseInt(arr[2]), arr[3],
arr[4], StringUtils.isNotEmpty(arr[5]) ? Double.parseDouble(arr[5]) : null, Double.parseDouble(arr[6]), arr[7]) ;
morningkanbanDao.saveOrUpdate(obj);
}
}

}


为什么在service中循环save数据,后面出错,前面的不回滚??
...全文
218 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zhuang.Mr.L 2019-07-23
  • 打赏
  • 举报
回复
你说的出错,是运行时异常还是Exception? requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(RuntimeException.class))); 这里的代码,你设置了两遍回滚规则。最后的运行异常替换了原来的Exception。requireRule.setRollbackRules()这里传集合进去,把你想回滚的都放进去。如果想无论什么都回滚,那只写一个Exception就行。Exception是RuntimeException父类。
Monday_@@ 2019-07-22
  • 打赏
  • 举报
回复
是不是事务的级别配置错了

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧