关于 HotSwappableTargetSource 用法的疑问!

iStringTheory 2007-04-24 03:46:56
因为公司项目要求登陆时能切换数据源,所以使用了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里面,百思不得其解!!
...全文
430 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
alex_lu 2007-04-25
  • 打赏
  • 举报
回复
UP
自然80 2007-04-25
  • 打赏
  • 举报
回复
事务怎么管理?
adio0211 2007-04-25
  • 打赏
  • 举报
回复
程序和数据库的对话不是持续的!
我觉得,你在不同的数据库之间切换之前,利用spring的依赖注入来连接不同的数据源能实现你要的功能吗?
iStringTheory 2007-04-25
  • 打赏
  • 举报
回复
还没解决,期待
WIN_ANGEL 2007-04-25
  • 打赏
  • 举报
回复
帮顶
inthistime 2007-04-25
  • 打赏
  • 举报
回复
帮顶
laiwusheng 2007-04-24
  • 打赏
  • 举报
回复
可热交换的目标源
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都可以和任何一种通知一起使用.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧