context.xml
《?xml version="1.0" encoding="UTF-8"?》
《beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"》
《jee:jndi-lookup id="mysqlds" jndi-name="java:comp/env/jdbc/mysqlds" /》
《jee:jndi-lookup id="orads" jndi-name="java:comp/env/jdbc/orads" /》
《bean id="dataSource" class="util.DynamicDataSource"》
《property name="targetDataSources"》
《map key-type="java.lang.String"》
《entry key="0" value-ref="mysqlds" /》
《entry key="1" value-ref="orads" /》
《/map》
《/property》
《property name="defaultTargetDataSource" ref="orads" /》
《/bean》
《/beans》
package util;
public class DbContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal();
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return (String) contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
package util;
import java.sql.SQLException;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
//static Logger log = Logger.getLogger("DynamicDataSource");
@Override
protected Object determineCurrentLookupKey() {
// TODO
return DbContextHolder.getDbType();
}
@Override
public boolean isWrapperFor(Class 《?》 iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public 《T》 T unwrap(Class 《T》 iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
测试程序:
DbContextHolder.setDbType("1");
UserDomain od = this.testService.queryUserAccount("admin");
System.out.println("username=="+od.getName());
DbContextHolder.setDbType("0");
d = this.testService.queryUserAccount("admin");
《bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"》
《!-- Connection Info --》
《property name="driverClassName" value="${jdbc.driver}" /》
《property name="url" value="${jdbc.url}" /》
《property name="username" value="${jdbc.username}" /》
《property name="password" value="${jdbc.password}" /》
《!-- Connection Pooling Info --》
《property name="initialSize" value="5" /》
《property name="maxActive" value="100" /》
《property name="maxIdle" value="30" /》
《property name="maxWait" value="1000" /》
《property name="poolPreparedStatements" value="false" /》
《property name="defaultAutoCommit" value="false" /》
《/bean》
《bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"》
《!-- Connection Info --》
《property name="driverClassName" value="${jdbc.driver}" /》
《property name="url" value="${jdbc.url}" /》
《property name="username" value="${jdbc.username}" /》
《property name="password" value="${jdbc.password}" /》
《!-- Connection Pooling Info --》
《property name="initialSize" value="5" /》
《property name="maxActive" value="100" /》
《property name="maxIdle" value="30" /》
《property name="maxWait" value="1000" /》
《property name="poolPreparedStatements" value="false" /》
《property name="defaultAutoCommit" value="false" /》
《/bean》
《bean id="dataSource" class="util.DynamicDataSource"》
《property name="targetDataSources"》
《map key-type="java.lang.String"》
《entry key="0" value-ref="dataSource1" /》
《entry key="1" value-ref="dataSource2" /》
《/map》
《/property》
《property name="defaultTargetDataSource" ref="dataSource1" /》
《/bean》
《/beans》
一个系统要用多种数据库
最新推荐文章于 2019-06-12 11:51:18 发布