1.Spring configuration file on the Data source section
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:context=" Http://www.springframework.org/schema/context "3Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= "Http://www.springframework.org/schema/tx"4Xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"5xsi:schemalocation="6http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd7http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd8http//Www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc.xsd9http//Www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsdTenhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd Onehttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPAhttp://www.springframework.org/schema/data/jpa/spring-jpa.xsd" A default-lazy-init= "true" > - -<!--=================================================================== the-database Connection Pool configuration --=================================================================== -<bean id= "DataSource1"class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" > -<!--basic property URL, user, password-- +<property name= "url" value= "${ds1.jdbc.url}"/> -<property name= "username" value= "${ds1.jdbc.username}"/> +<property name= "Password" value= "${ds1.jdbc.password}"/> A at<!--configuration initialization size, MIN, max-- -<property name= "initialsize" value= "/>" -<property name= "Minidle" value= "/>" -<property name= "maxactive" value= "/>" - -<!--configuration Gets the time that the connection waits timeout-- in<property name= "maxwait" value= "60000"/> - to< how long!--configuration interval to detect idle connections that need to be closed, in milliseconds-and +<property name= "Timebetweenevictionrunsmillis" value= "60000"/> - the<!--Configure the minimum time for a connection to survive in a pool, in milliseconds-- *<property name= "Minevictableidletimemillis" value= "300000"/> $ Panax Notoginseng<property name= "Validationquery" value= "select ' X '"/> -<property name= "Testwhileidle" value= "true"/> the<property name= "Testonborrow" value= "false"/> +<property name= "Testonreturn" value= "false"/> A the<!--open Pscache and specify the size of Pscache on each connection- +<property name= "Poolpreparedstatements" value= "true"/> -<property name= "maxpoolpreparedstatementperconnectionsize" value= "/>" $ $<!--configuration monitoring statistics intercept filters-- -<property name= "Filters" value= "Stat,wall"/> -</bean> the -<bean id= "DataSource2"class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" >Wuyi<!--basic property URL, user, password-- the<property name= "url" value= "${ds2.jdbc.url}"/> -<property name= "username" value= "${ds2.jdbc.username}"/> Wu<property name= "Password" value= "${ds2.jdbc.password}"/> - About<!--configuration initialization size, MIN, max-- $<property name= "initialsize" value= "/>" -<property name= "Minidle" value= "/>" -<property name= "maxactive" value= "/>" - A<!--configuration Gets the time that the connection waits timeout-- +<property name= "maxwait" value= "60000"/> the -< how long!--configuration interval to detect idle connections that need to be closed, in milliseconds-and $<property name= "Timebetweenevictionrunsmillis" value= "60000"/> the the<!--Configure the minimum time for a connection to survive in a pool, in milliseconds-- the<property name= "Minevictableidletimemillis" value= "300000"/> the -<property name= "Validationquery" value= "select ' X '"/> in<property name= "Testwhileidle" value= "true"/> the<property name= "Testonborrow" value= "false"/> the<property name= "Testonreturn" value= "false"/> About the<!--open Pscache and specify the size of Pscache on each connection- the<property name= "Poolpreparedstatements" value= "true"/> the<property name= "maxpoolpreparedstatementperconnectionsize" value= "/>" + -<!--configuration monitoring statistics intercept filters-- the<property name= "Filters" value= "Stat,wall"/>Bayi</bean> the the<bean id= "DataSource"class= "Org.matrixframework.common.router.DynamicDataSource" > -<property name= "Targetdatasources" > -<map key-type= "Java.lang.String" > the<entry value-ref= "DataSource1" key= "DataSource1" ></entry> the<entry value-ref= "DataSource2" key= "DataSource2" ></entry> the</map> the</property> -<property name= "Defaulttargetdatasource" ref= "DataSource1" ></property> the</bean> the the<bean id= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >94<property name= "DataSource" ref= "DataSource" ></property> the</bean> the<tx:annotation-driven transaction-manager= "TransactionManager"/> the 98</beans>
2. Encapsulating the data Source Tool class
1 Public classThreadlocalutil {2 Private StaticThreadlocal<string> dskeythreadlocal =NewThreadlocal<string>();3 4 Public StaticString Getdatasourcekey () {5 returndskeythreadlocal.get ();6 }7 8 Public Static voidSetdatasourcekey (String value) {9 ThreadLocalUtil.dsKeyThreadLocal.set (value);Ten } One A /** - * Switch to default data source: DataSource 1 - */ the Public Static voidSwitchDataSource1 () { -Setdatasourcekey ("DataSource1"); - } - + /** - * Switch to a custom data source: DataSource 2 + */ A Public Static voidSwitchDataSource2 () { atSetdatasourcekey ("DataSource2"); - } -}
3. Enable data source switching via Abstractroutingdatasource
1 Public class extends Abstractroutingdatasource {23 @Override4 protected Object Determinecurrentlookupkey () {5 return Threadlocalutil.getdatasourcekey (); 6 }78 }
At last, the Threadlocalutil.switchdatasource~ method can be used to switch the target data source manually before manipulating the data.
Spring configures multiple data sources