因为公司项目要求登陆时能切换数据源,所以使用了HotSwappableTargetSource这个类,现在问题是根本没办法切换数据源,不知道错在哪了,各位帮我看看:  配置文件:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
            <value>${datasource.driverClassName}</value>
        </property>
        <property name="url">
            <value>${datasource.url}</value>
        </property>
        <property name="username">
            <value>${datasource.username}</value>
        </property>
        <property name="password">
            <value>${datasource.password}</value>
        </property>
</bean>

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>${datasource.driverClassName}</value>
        </property>
        <property name="url">
            <value>${datasource.url1}</value>
        </property>
        <property name="username">
            <value>${datasource.username}</value>
        </property>
        <property name="password">
            <value>${datasource.password}</value>
        </property>
    </bean><bean id="swappableDataSource" class="org.springframework.aop.target.HotSwappableTargetSource">
<constructor-arg>
<ref local="dataSource"/>
</constructor-arg>
</bean>

<bean id="swappable" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource">
<ref local="swappableDataSource"/>
</property>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="com.strongit.finance.common.support.HibernateHelpSessionFactoryBean" depends-on="hibernateMappingFileSetUtil">
<!--<property name="dataSource">
<ref bean="dataSource"/>
</property>-->
<property name="dataSource">
<ref local="swappable"/>
</property>
<property name="mappingResources">
<!-- 收集所有系统的hibernate配置文件-->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="hibernateMappingFileSetUtil"/>
</property>
<property name="targetMethod">
<value>getLists</value>
</property>
</bean>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.query.substitutions">true 1,false 0</prop>
<prop key="hibernate.session_factory_name">hibernate.sessionFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="jndiName">
<value>hibernate/sessionFactory</value>
</property>
</bean>代码:
ApplicationContext application = new FileSystemXmlApplicationContext(
"classpath:META-INF/finance/config/applicationContext-datasource.xml");
HotSwappableTargetSource swapper = (HotSwappableTargetSource)application.getBean("swappableDataSource");
DataSource newDataSource = (DataSource)application.getBean("dataSource1");
logger.info("newDataSource:"+newDataSource.toString());
swapper.swap(newDataSource);
info = (Info) bean;
infoManager.addInfo(info);现在进行数据源切换后数据还是保存到了第一个dataSource里面,百思不得其解!!

解决方案 »

  1.   

    可热交换的目标源
    org.springframework.aop.target.HotSwappableTargetSource 允许切换一个AOP代理的目标,而调用者维持对它的引用.
    修改目标源的目标会立即起作用.并且HotSwappableTargetSource是线程安全的.
    你可以通过HotSwappableTargetSource的swap()方法 来改变目标,就象下面一样:
    HotSwappableTargetSource swapper = 
    (HotSwappableTargetSource) beanFactory.getBean("swapper");
    Object oldTarget = swapper.swap(newTarget);上面的swap()调用会修改swappable这个bean的目标.持有对这个bean应用的客户端 将不会知道这个变化,但会立刻转为使用新的目标对象.
    使用TargetSource也没必要添加通知-- 当然任何TargetSource都可以和任何一种通知一起使用.
      

  2.   

    程序和数据库的对话不是持续的!
    我觉得,你在不同的数据库之间切换之前,利用spring的依赖注入来连接不同的数据源能实现你要的功能吗?