解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問(wèn)題
一個(gè)項(xiàng)目中需要使用兩個(gè)數(shù)據(jù)庫(kù),Oracle 和Mysql,于是參考各個(gè)blog,實(shí)現(xiàn)此功能。寫好后才發(fā)現(xiàn),原來(lái)的事務(wù)失效了,我去...
spring-mybatis.xml 配置
<bean id="configReader" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:spring/db.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.oracle.DriverClassName}"></property> <property name="jdbcUrl" value="${jdbc.oracle.Url}"></property> <property name="user" value="${jdbc.oracle.UserName}"></property> <property name="password" value="${jdbc.oracle.UserPassword}"></property> <property name="acquireIncrement" value="5"></property> <property name="initialPoolSize" value="5"></property> <property name="maxIdleTime" value="60"></property> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="5"></property> </bean> <!-- 配置數(shù)據(jù)源:MySQL start --> <bean name="mySqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.mysql.DriverClassName}"/> <property name="url" value="${jdbc.mysql.Url}"/> <property name="username" value="${jdbc.mysql.UserName}"/> <property name="password" value="${jdbc.mysql.UserPassword}"/> <!-- 初始化連接大小 --> <property name="initialSize" value="5"/> <!-- 連接池最大使用連接數(shù)量 --> <property name="maxActive" value="30"/> <!-- 連接池最小空閑 --> <property name="minIdle" value="2"/> <!-- 獲取連接最大等待時(shí)間 --> <property name="maxWait" value="300"/> <property name="validationQuery" value="SELECT 1"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="testWhileIdle" value="true"/> <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="10000"/> <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="30000"/> <!-- 打開removeAbandoned功能 --> <property name="removeAbandoned" value="true"/> <!-- 1800秒,也就是30分鐘 --> <property name="removeAbandonedTimeout" value="1800"/> <!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 --> <property name="logAbandoned" value="true"/> <!-- 監(jiān)控?cái)?shù)據(jù)庫(kù) --> <property name="filters" value="stat"/> </bean> <bean id="multipleDataSource" class="com.we.database.MultipleDataSource"> <property name="defaultTargetDataSource" ref="dataSource"/> <property name="targetDataSources"> <map> <entry key="oracleDataSource" value-ref="dataSource"/> <entry key="mySqlDataSource" value-ref="mySqlDataSource"/> </map> </property> </bean> <!-- oracle myBatis file --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="multipleDataSource"/> <!--<property name="configLocation" value="classpath:configuration.xml" /> --> <property name="mapperLocations" value="classpath:com/we/dao/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.we.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- configure transaction --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- annotation transaction --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- interception transatcion --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置數(shù)據(jù)庫(kù)注解aop --> <bean id="dataSourceAspect" class="com.we.database.DataSourceAspect"/> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.wewe.licai.service..*Impl.*(..))"/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="2"/> <!--數(shù)據(jù)源選擇切面,保證在事務(wù)開始之前執(zhí)行--> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="dataSourceAspect" order="1" /> </aop:config>
注解切換,默認(rèn)使用oracle數(shù)據(jù)源
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD,ElementType.TYPE}) public @interface DataSource { String name() default DataSource.oracleDataSource; String mySqlDataSource = "mySqlDataSource"; String oracleDataSource = "oracleDataSource"; }
注解方式實(shí)現(xiàn)切換數(shù)據(jù)源,搜索注釋,更換注釋上面的數(shù)據(jù)源,支持類注釋和方法注釋
/** * Created by eastday on 2017/9/21. */ public class DataSourceAspect implements MethodBeforeAdvice,AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { MultipleDataSource.clearDataSource(); } @Override public void before(Method method, Object[] args, Object target) throws Throwable { //首先取類上的數(shù)據(jù)源 if(method.getDeclaringClass().isAnnotationPresent(DataSource.class) && !method.isAnnotationPresent(DataSource.class)) { DataSource datasource = method.getDeclaringClass().getAnnotation(DataSource.class); MultipleDataSource.setDataSource(datasource.name()); //方法上的數(shù)據(jù)源 優(yōu)先級(jí)高于類上的 } else if (method.isAnnotationPresent(DataSource.class)) { DataSource datasource = method.getAnnotation(DataSource.class); MultipleDataSource.setDataSource(datasource.name()); } else { MultipleDataSource.setDataSource(DataSource.oracleDataSource); } } }
繼承AbstractRoutingDataSource實(shí)現(xiàn)數(shù)據(jù)源切換
public class MultipleDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> dataSources = new InheritableThreadLocal<String>(); public static void setDataSource(String dataSource) { dataSources.set(dataSource); } //清除數(shù)據(jù)源 public static void clearDataSource() { dataSources.remove(); } @Override protected Object determineCurrentLookupKey() { return dataSources.get(); } }
使用demo
@DataSource(name = DataSource.mySqlDataSource) public class ContentServiceImpl implements IContentService { @Autowired private IContentDao contentDao; @Override public Content queryOne(String type) { return contentDao.queryOne(type); } }
以上這篇解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot2+JPA之悲觀鎖和樂觀鎖實(shí)戰(zhàn)教程
這篇文章主要介紹了Spring Boot2+JPA之悲觀鎖和樂觀鎖實(shí)戰(zhàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10關(guān)于SpringCloudStream配置問(wèn)題
這篇文章主要介紹了關(guān)于SpringCloudStream配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12SpringBoot整合Mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置與跨數(shù)據(jù)源事務(wù)實(shí)例
開發(fā)中經(jīng)常有這樣的需要: 讀寫分離。微服務(wù)環(huán)境下可以實(shí)現(xiàn)一個(gè)服務(wù)讀取一個(gè)數(shù)據(jù)庫(kù),另一個(gè)服務(wù)寫庫(kù)。但是在實(shí)際應(yīng)用中有時(shí)也需要在一個(gè)服務(wù)中讀寫不同的數(shù)據(jù)庫(kù)??梢栽谝粋€(gè)SpringBoot單體項(xiàng)目中配置多個(gè)數(shù)據(jù)源解決讀寫庫(kù)分離2022-11-11java算法之Math.random()隨機(jī)概率玩法實(shí)例演示
最近打算整理排序算法,發(fā)現(xiàn)很有必要準(zhǔn)備一下生成隨機(jī)數(shù)的工具類,下面這篇文章主要給大家介紹了關(guān)于java算法之Math.random()隨機(jī)概率玩法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Java中Thread和Runnable創(chuàng)建線程的方式對(duì)比
本文主要介紹了Java中Thread和Runnable創(chuàng)建線程的方式對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07