Spring多數(shù)據(jù)源導致配置失效的解決
spring多數(shù)據(jù)源導致配置失效
<!-- 切換數(shù)據(jù)源 --> <bean id="dataSourceAdvice" class="com.xxxx.app.datasource.DataSourceAdvice" /> <!-- 配置事務切面 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.xxxx..*.*(..))" /> <aop:advisor pointcut-ref="serviceOperation" advice-ref="dataSourceAdvice" /> </aop:config>
如果在springApplication.xml中配置的, 那么expression配置的相關(guān)的 包,必須在springmvc.xml中被掃描到
<context:component-scan base-package="com.xxxx.xxxxxx" />
如果不配置的話 會導致失敗
spring配置多數(shù)據(jù)源問題,如何彼此隔離互相不影響
配置2個數(shù)據(jù)源,事物切面配置:“事物添加方法前綴”配置相同并且“事物的切入點”如下配置,如果其中任何一個數(shù)據(jù)源連接不上事物提交不了,數(shù)據(jù)保存不成功
事物添加方法前綴:
- 第一個:方法前綴:save
<tx:advice id="shardTxAdvice" transaction-manager="shardTransactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> .......... .......... </tx:attributes> </tx:advice>
- 第二個:方法前綴:save
<tx:advice id="qualTxAdvice" transaction-manager="qualTransactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> ......... ......... </tx:attributes> </tx:advice>
事物的切入點:
<aop:config> <aop:pointcut id="bizMethods" expression="execution(* com.cn.*.service.*.*(..)) or execution(* com.cn.*.*.service.*.*(..)) or execution(* com.cn.*.*.*.*(..))" /> <aop:advisor advice-ref="shardTxAdvice" pointcut-ref="bizMethods" /> <aop:advisor advice-ref="qualTxAdvice" pointcut-ref="bizMethods" /> </aop:config>
方案一
如果確定了,其中一個數(shù)據(jù)源只查詢使用沒有數(shù)據(jù)維護,不為其配置事物。
方案二
如果2數(shù)據(jù)源都需要對數(shù)據(jù)維護,同時保證2個數(shù)據(jù)源的事物對所有的服務層一致,可以將2個數(shù)據(jù)源的事物添加方法前綴配置的不一樣.
這種情況下,試驗中證明:
1:單獨操作其中一數(shù)據(jù)源的方法開啟事物,其他數(shù)據(jù)源連接失敗也不相互影響,
2:操作2個數(shù)據(jù)源的方法,其中任何一個數(shù)據(jù)連接失敗,事物仍然會回滾。
事物添加方法前綴:
- 第一個:方法前綴:save
<tx:advice id="shardTxAdvice" transaction-manager="shardTransactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> .......... .......... </tx:attributes> </tx:advice>
- 第二個:方法前綴:qual_save
<tx:advice id="qualTxAdvice" transaction-manager="qualTransactionManager"> <tx:attributes> <tx:method name="qual_save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> ......... ......... </tx:attributes> </tx:advice>
事物的切入點:
不變,保證2個數(shù)據(jù)源事物對“所有的服務層一致”
<aop:config> <aop:pointcut id="bizMethods" expression="execution(* com.cn.*.service.*.*(..)) or execution(* com.cn.*.*.service.*.*(..)) or execution(* com.cn.*.*.*.*(..))" /> <aop:advisor advice-ref="shardTxAdvice" pointcut-ref="bizMethods" /> <aop:advisor advice-ref="qualTxAdvice" pointcut-ref="bizMethods" /> </aop:config>
方案三
事物添加方法前綴相同,修改不同事物的切入點
Spring的AOP應該是可以把不同的事物,插入到不同的類和方法中。
<!-- 事務范圍aop--> 指定包下某個類的方法使用新的數(shù)據(jù)源事物 <aop:config> <aop:pointcut id="bigbizMethods" expression="execution(* com.cn.sp.sc.service..CouponBigDataService.*(..))" /> <aop:advisor advice-ref="qualTxAdvice" pointcut-ref="bigbizMethods" /> </aop:config>
也許有更好的方案解決,我實驗是這3種情況下的方法。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot Web詳解靜態(tài)資源規(guī)則與定制化處理
這篇文章主要介紹了SpringBoot web場景的靜態(tài)資源規(guī)則與定制化,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
Spring框架中一個有用的小組件之Spring Retry組件詳解
Spring Retry 是從 Spring batch 中獨立出來的一個功能,主要實現(xiàn)了重試和熔斷,對于那些重試后不會改變結(jié)果,毫無意義的操作,不建議使用重試,今天通過本文給大家介紹Spring Retry組件詳解,感興趣的朋友一起看看吧2021-07-07
關(guān)于@DS注解切換數(shù)據(jù)源失敗的原因?qū)崙?zhàn)記錄
項目配置了多個數(shù)據(jù)源,需要使用@DS注解來切換數(shù)據(jù)源,但是卻遇到了問題,下面這篇文章主要給大家介紹了關(guān)于@DS注解切換數(shù)據(jù)源失敗原因的相關(guān)資料,需要的朋友可以參考下2023-05-05
SpringBoot Redis配置多數(shù)據(jù)源的項目實踐
springboot中默認的redis配置是只能對單個redis庫進行操作的, 那么我們需要多個庫操作的時候這個時候就可以采用redis多數(shù)據(jù)源 ,本文就介紹了SpringBoot Redis配置多數(shù)據(jù)源,感興趣的可以了解一下2023-07-07
Java用三元運算符判斷奇數(shù)和偶數(shù)的簡單實現(xiàn)
這篇文章主要介紹了Java用三元運算符判斷奇數(shù)和偶數(shù)的簡單實現(xiàn),需要的朋友可以參考下2014-02-02
SpringMVC實現(xiàn)獲取請求參數(shù)方法詳解
Spring MVC 是 Spring 提供的一個基于 MVC 設計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當于 Servlet,Spring MVC 角色劃分清晰,分工明細,這篇文章主要介紹了SpringMVC實現(xiàn)獲取請求參數(shù)方法2022-09-09

