zoukankan      html  css  js  c++  java
  • 全局性事务控制如何在springboot中配置

    开发中,我们一般会利用AOP配置全局性的事务,对指定包下指定的方法(如add,update等)进行事务控制,在springboot中如何实现呢?

    
    @EnableTransactionManagement
    @Aspect
    @Configuration
    public class GlobalTransactionConfig {
        //写事务的超时时间为10秒
        private static final int TX_METHOD_TIMEOUT = 10;
    
        //restful包下所有service包或者service的子包的任意类的任意方法
        private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.jun.cloud.restful..*.service..*.*(..))";
    
        @Autowired
        private PlatformTransactionManager transactionManager;
    
        @Bean
        public TransactionInterceptor txAdvice() {
    
            /**
             * 这里配置只读事务
             */
            RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
            readOnlyTx.setReadOnly(true);
            readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    
            /**
             * 必须带事务
             * 当前存在事务就使用当前事务,当前不存在事务,就开启一个新的事务
             */
            RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
            //检查型异常也回滚
            requiredTx.setRollbackRules(
                    Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
            requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
            requiredTx.setTimeout(TX_METHOD_TIMEOUT);
    
            /***
             * 无事务地执行,挂起任何存在的事务
             */
            RuleBasedTransactionAttribute noTx = new RuleBasedTransactionAttribute();
            noTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    
            Map<String, TransactionAttribute> txMap = new HashMap<>();
            //只读事务
            txMap.put("get*", readOnlyTx);
            txMap.put("query*", readOnlyTx);
            txMap.put("find*", readOnlyTx);
            txMap.put("list*", readOnlyTx);
            txMap.put("count*", readOnlyTx);
            txMap.put("exist*", readOnlyTx);
            txMap.put("search*", readOnlyTx);
            txMap.put("fetch*", readOnlyTx);
            //无事务
            txMap.put("noTx*", noTx);
            //写事务
            txMap.put("add*", requiredTx);
            txMap.put("save*", requiredTx);
            txMap.put("insert*", requiredTx);
            txMap.put("update*", requiredTx);
            txMap.put("modify*", requiredTx);
            txMap.put("delete*", requiredTx);
    
            NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
            source.setNameMap(txMap);
    
            return new TransactionInterceptor(transactionManager, source);
        }
    
        @Bean
        public Advisor txAdviceAdvisor() {
            AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
            pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
            return new DefaultPointcutAdvisor(pointcut, txAdvice());
        }
    
    }
    

    如果配置了多个事务管理器,参看https://blog.csdn.net/catoop/article/details/50595702

  • 相关阅读:
    python检测挖矿特征的几种方式
    python检测当前端口是否使用
    matlab界面UI设计资料
    python中struct.pack()函数和struct.unpack()函数
    网络编程:主机字节序和网络字节序
    【原创】python中文编码问题深入分析(三):python2.7文件读写中文编码问题
    【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
    ivew定制主题 less ^3.0 时报错 .bezierEasingMixin(); Inline JavaScript is not enabled. Is it set in your options?
    Vue子组件中 data 从props中动态更新数据
    Vue 自动吸顶
  • 原文地址:https://www.cnblogs.com/seasail/p/12179370.html
Copyright © 2011-2022 走看看