欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

 更新時間:2024年01月31日 14:38:37   作者:droidcoffee-  
這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

spring多數(shù)據(jù)源導(dǎo)致配置失效

<!-- 切換數(shù)據(jù)源 -->
	<bean id="dataSourceAdvice" class="com.xxxx.app.datasource.DataSourceAdvice" />
	<!-- 配置事務(wù)切面 -->
	<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" />

如果不配置的話 會導(dǎo)致失敗

spring配置多數(shù)據(jù)源問題,如何彼此隔離互相不影響

配置2個數(shù)據(jù)源,事物切面配置:“事物添加方法前綴”配置相同并且“事物的切入點(diǎn)”如下配置,如果其中任何一個數(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>

事物的切入點(diǎn):

<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ù)維護(hù),不為其配置事物。

方案二

如果2數(shù)據(jù)源都需要對數(shù)據(jù)維護(hù),同時保證2個數(shù)據(jù)源的事物對所有的服務(wù)層一致,可以將2個數(shù)據(jù)源的事物添加方法前綴配置的不一樣.

這種情況下,試驗(yàn)中證明:

1:單獨(dú)操作其中一數(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>

事物的切入點(diǎn):

不變,保證2個數(shù)據(jù)源事物對“所有的服務(wù)層一致”

<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>

方案三

事物添加方法前綴相同,修改不同事物的切入點(diǎn)

Spring的AOP應(yīng)該是可以把不同的事物,插入到不同的類和方法中。

<!-- 事務(wù)范圍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>

也許有更好的方案解決,我實(shí)驗(yàn)是這3種情況下的方法。

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論