一个系统要用多种数据库

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》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值