spring boot 聲明式事務

  1. 新聞資訊
  2. 技術百科
行業動态 公司新聞 案例分享 技術百科

spring boot 聲明式事務

來(Come)源:奇站網絡 浏覽量:1398 發布日期: 2020-07-05

引入xml文件

以(by)代碼的(of)方式

  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. /* 隻讀事務,不(No)做更新操作(do) */
  29. RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
  30. readOnlyTx.setReadOnly(true);
  31. readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
  32. /* 當前存在(exist)事務就使用(use)當前事務,當前不(No)存在(exist)事務就創建一(one)個(indivual)新的(of)事務 */
  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. }
标簽:
下一(one)篇 jenkins

廈門極極網絡科技有限公司

電話:13313868605

QQ:3413772931

地(land)址:廈門集美區軟件園三期


                    掃一(one)掃加我(I)咨詢