Java 未使用Spring 3和MyBatis路由数据源

Java 未使用Spring 3和MyBatis路由数据源,java,spring,datasource,mybatis,spring-transactions,Java,Spring,Datasource,Mybatis,Spring Transactions,我有一个默认数据库,有时我必须在另一个数据库中进行选择 我在这里搜索了很多关于这方面的博客和问题,但都没有成功 我试过了。没什么 RouterDataSource类的代码: public class RouterDataSource extends AbstractRoutingDataSource { @Override protected DataSourceEnum determineCurrentLookupKey() { return DataS

我有一个默认数据库,有时我必须在另一个数据库中进行选择

我在这里搜索了很多关于这方面的博客和问题,但都没有成功

我试过了。没什么

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();
     }
}
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时,它不会改变


有人能帮我吗?

尝试将静态方法设置为非静态方法,如果上下文持有者为,则传递R引用

首先确保database2DS工作正常。设置defaultTargetDatasource数据库2DS,并验证它是否仍在使用DB1,以及使用database2DS作为默认值是否存在其他错误。如果AbstractRoutingDataSource无法解析targetDataSources中的数据源,则无法切换到该数据源


AbstractRoutingDataSource仅在调用getConnection时更改数据源。您使用的任何持久性框架都可能缓存连接,而不是在persistency.getObject()之间调用getConnection。无论如何获取持久性对象,请在DataSourceContextHolder中更改数据源后尝试获取新的持久性对象。如果这解决了问题,请尝试创建一个类来维护持久性对象并处理数据源的更改。这样,当您更改数据源时,您可以在一个位置修改persistency manager对象。

R引用?我该怎么做?
<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>