Abstractroutingdatasource dynamically switching data sources

Source: Internet
Author: User
Tags connection pooling

spring2.0 later added abstractroutingdatasource this thing provides a dynamic switching data source.

Abstractroutingdatasource inherits the Abstractdatasource,abstractdatasource is a subclass of Javax.sql.DataSource.

Here is an example of a dynamically switching data source:


1. configuration file configuration in Spring.xml

<!--Configure connection pooling--
<bean id= "Readandwritedatasouce" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "Driverclass" value= "${driver}"/> <!--Specify the drive to connect to the database
<property name= "Jdbcurl" value= "${url}"/> <!--Specify the URL of the connection database--
<property name= "user" value= "${user}"/> <!--Specify the user name to connect to the database--
<property name= "Password" value= "${password}"/> <!--Specify the password to connect to the database--
<property name= "Maxpoolsize" value= "$"/> <!--Specify the maximum number of connections to connect to the database connection pool--
<property name= "Minpoolsize" value= "three"/> <!--Specify the minimum number of connections to connect to the database connection pool (can be larger when production is used)-
<property name= "Initialpoolsize" value= "three"/> <!--Specifies the number of initial connections to connect to the database connection pool (can be changed when production is used)-
<property name= "maxidletime" value= "/> <!--Specify the maximum idle time for connections connecting to a database connection pool-
<!--when the connection pool connection is exhausted, the client calls getconnection () to wait for a new connection, and then throws SqlException after the timeout, and waits indefinitely if set to 0. Unit milliseconds. Default: 0--
<property name= "checkouttimeout" value= "/>"
<property name= "acquireincrement" value= "/> <!--when the connection in the connection pool is exhausted c3p0 the number of connections that are fetched at the same time. Default:3--
<property name= "Acquireretrydelay" value= "three"/><!--two connections in the interval, per millisecond. default:1000--
<property name= "idleconnectiontestperiod" value= "/> <!--check idle connections in all connection pools every 60 seconds. Default:0--
</bean>

<bean id= "Readdatasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "Driverclass" value= "${driver}"/>
<property name= "Jdbcurl" value= "${urlforread}"/>
<property name= "user" value= "${userforread}"/>
<property name= "Password" value= "${passwordforread}"/>
<property name= "maxpoolsize" value= "/>"
<property name= "minpoolsize" value= "/>"
<property name= "initialpoolsize" value= "/>"
<property name= "maxidletime" value= "/>"
<property name= "checkouttimeout" value= "/>"
<property name= "Acquireincrement" value= "ten"/>
<property name= "Acquireretrydelay" value= "/>"
<property name= "idleconnectiontestperiod" value= "/>"
</bean>
<!--Multipledatasource is our own class that inherits the Abstractroutingdatasource class, enabling Dynamic Data source switching--
<bean id= "Multipledatasource" class= "Cn.hicard.common.MultipleDataSource" >
<property name= "Defaulttargetdatasource" ref= "Readandwritedatasouce"/>
<property name= "Targetdatasources" >
<map>
<entry key= "Defaultdatasource" value-ref= "Readandwritedatasouce"/>
<entry key= "Readdatasource" value-ref= "Readdatasource"/>
</map>
</property>

</bean>
2. Creating an implementation class for dynamically switching data sources Multipledatasource
Import Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
 * Multiple data source support, before calling DAO, execute Multipledatasource.changeread (), change to read <br/>
 * Read and write data source as default data source
& nbsp;*/
public class Multipledatasource extends abstractroutingdatasource{
private static final threadlocal< string> Datasourcekey = new inheritablethreadlocal<string> ();
/**  Change the current data source to read data source */
    public static void Changeread () {
    Datasourcekey.set (" Readdatasource ");
   }
   /*** Change the current data source to a read-write data source, which is the default data source  */
    public static void Changereadandwirte () {
& nbsp   Datasourcekey.set ("Readandwritedatasouce");
   }

Implementing the Determinecurrentlookupkey method This is the main way to get the data source
@Override
Protected Object Determinecurrentlookupkey () {
return Datasourcekey.get ();
}
}

3. Switch to the specified data source before invoking the database

@Service
public class Logserviceimpl implements Ilogservice {
@Resource
Private Ilogdao Logdao;
@Override
Public list<log> Search (map<string, object> paramter) throws Exception {
Multipledatasource.changeread ();//Toggle Data source
Return Logdao.search (paramter);
}

}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.