spring boot 声明式事务

  1. 新闻资讯
  2. 技术百科
公司新闻 案例分享 技术百科 行业动态

spring boot 声明式事务

来源:奇站网络 浏览量:163 发布日期: 2020-07-05

引入xml文件

以代码的方式

  1. package com.qizhan100.demo;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.springframework.aop.Advisor;
  7. import org.springframework.aop.aspectj.AspectJExpressionPointcut;
  8. import org.springframework.aop.support.DefaultPointcutAdvisor;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import org.springframework.transaction.TransactionDefinition;
  14. import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
  15. import org.springframework.transaction.interceptor.RollbackRuleAttribute;
  16. import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
  17. import org.springframework.transaction.interceptor.TransactionAttribute;
  18. import org.springframework.transaction.interceptor.TransactionInterceptor;
  19. @Aspect
  20. @Configuration
  21. public class TxAdviceInterceptor {
  22. private static final int TX_METHOD_TIMEOUT = 5;
  23. @Autowired
  24. private PlatformTransactionManager transactionManager;
  25. @Bean
  26. public TransactionInterceptor txAdvice() {
  27. NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
  28. /* 只读事务,不做更新操作 */
  29. RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
  30. readOnlyTx.setReadOnly(true);
  31. readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
  32. /* 当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务 */
  33. RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
  34. requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
  35. requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  36. requiredTx.setTimeout(TX_METHOD_TIMEOUT);
  37. Map<String, TransactionAttribute> txMap = new HashMap<>();
  38. txMap.put("add*", requiredTx);
  39. txMap.put("save*", requiredTx);
  40. txMap.put("insert*", requiredTx);
  41. txMap.put("update*", requiredTx);
  42. txMap.put("delete*", requiredTx);
  43. txMap.put("get*", readOnlyTx);
  44. txMap.put("query*", readOnlyTx);
  45. source.setNameMap(txMap);
  46. TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
  47. return txAdvice;
  48. }
  49. @Bean
  50. public Advisor txAdviceAdvisor() {
  51. AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
  52. pointcut.setExpression("execution(* *..dao.*.*(..))");
  53. pointcut.setExpression("execution(* *..service.*.*(..))");
  54. return new DefaultPointcutAdvisor(pointcut, txAdvice());
  55. }
  56. }
标签:
下一篇 jenkins

厦门奇站网络科技有限公司

电话:13313868605

QQ:3413772931

地址:厦门软件园三期A02号

网站地图


                    扫一扫加我咨询