我有一个默认的数据库,有时我必须在另一个数据库中进行选择。
我在这里搜索了许多关于这方面的博客和问题,但都不能让它工作。
已尝试http://blog.springsource.org/2007/01/23/dynamic-datasource-routing/方式。没什么。
RouterDataSource类的代码:
public class RouterDataSource extends AbstractRoutingDataSource {
@Override
protected DataSourceEnum determineCurrentLookupKey() {
return DataSourceContextHolder.getTargetDataSource();
}
}
DataSourceContextHolder类的代码:
public class DataSourceContextHolder {
private static final ThreadLocal<DataSourceEnum> contextHolder = new ThreadLocal<DataSourceEnum>();
public static void setTargetDataSource(DataSourceEnum targetDataSource) {
Assert.notNull(targetDataSource, "Target data source cannot be null");
contextHolder.set(targetDataSource);
}
public static DataSourceEnum getTargetDataSource() {
if (contextHolder.get() != null)
return (DataSourceEnum) contextHolder.get();
else
return DataSourceEnum.DB1;
}
public static void resetDefaultDataSource() {
contextHolder.remove();
}
}
修改数据库的方法调用代码:
@Override
public CodeHD getCategoryByCode(String code) throws BusinessException {
DataSourceContextHolder.setTargetDataSource(DataSourceEnum.DATABASE2);
return (CodeHD) persistency.getObject(GETOBJECT_BY_CODE, code);
}
DatasourceEnum类的代码:
public enum DataSourceEnum {
DB1,
DB2;
}
最后是我的applicationContext.xml上的配置:
<bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" abstract="true">
<property name="driverClass" value="oracle.jdbc.pool.OracleDataSource" />
<property name="acquireIncrement" value="10" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="maxStatements" value="50" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="15" />
</bean>
<bean id="database1DS" parent="parentDataSource">
<property name="jdbcUrl" value="jdbc:oracle:thin:@database1:1521:xe" />
<property name="user" value="user" />
<property name="password" value="password" />
</bean>
<bean id="database2DS" parent="parentDataSource">
<property name="jdbcUrl" value="jdbc:oracle:thin:@database2:1521:xe" />
<property name="user" value="user" />
<property name="password" value="password" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="package.RouterDataSource">
<property name="defaultTargetDataSource" ref="database1DS"/>
<property name="targetDataSources">
<map key-type="package.DataSourceEnum">
<entry key="DB1" value-ref="database1DS"/>
<entry key="DB2" value-ref="database2DS"/>
</map>
</property>
</bean>
问题是,当我将它设置为DB2时,它不会改变。
有谁可以帮我?
发布于 2012-10-24 18:53:48
尝试将静态方法设置为非静态方法,如果上下文持有者,则传递R引用。
发布于 2012-10-24 20:15:45
首先,确保database2DS工作正常。将defaultTargetDatasource设置为database2DS,并验证它是否仍在使用DB1,以及是否没有使用database2DS作为默认设置,并且没有其他错误。如果AbstractRoutingDataSource无法解析targetDataSources中的DataSource,则无法切换到它。
AbstractRoutingDataSource只会在调用getConnection时更改DataSource。无论您使用的是哪种持久性框架,都可能是在缓存连接,而不是在persistency.getObject()之间调用getConnection。无论您如何获取持久性对象,请在DataSourceContextHolder中更改数据源后尝试获取一个新的持久性对象。如果这解决了您的问题,请尝试创建一个类来维护持久性对象并处理数据源的更改。这样,当您更改数据源时,您可以在一个地方修改持久性管理器对象。
https://stackoverflow.com/questions/13055609
复制