日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

全局性事务控制如何在springboot中配置

發布時間:2024/9/30 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 全局性事务控制如何在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..*.*(..))";@Autowiredprivate PlatformTransactionManager transactionManager;@Beanpublic 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);}@Beanpublic Advisor txAdviceAdvisor() {AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression(AOP_POINTCUT_EXPRESSION);return new DefaultPointcutAdvisor(pointcut, txAdvice());}}

如果配置了多個事務管理器,參看https://blog.csdn.net/catoop/article/details/50595702

總結

以上是生活随笔為你收集整理的全局性事务控制如何在springboot中配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。