Spring hibernate配置双数据源动态切换失败
问题描述:需要连接到两个mysql,根据网上挺详细的教程 配置教程 进行配置,但是动态切换还是失败,所有配置都与链接中相同,
<bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- MySQL5 -->
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${zabbix_url}"></property>
<property name="user" value="${zabbix_username}"></property>
<property name="password" value="${zabbix_password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<!--<!– 使用C3P0数据源,two MySQL数据库 –>-->
<bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- MySQL5 -->
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${dataset_url}"></property>
<property name="user" value="${dataset_username}"></property>
<property name="password" value="${dataset_password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<bean id="dynamicDataSource" class="com.aa.ee.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSourceOne" key="dataSourceOne"></entry>
<entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceOne"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="packagesToScan" value="com.aa.ee.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--<tx:annotation-driven transaction-manager="transactionManager" />-->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.aa.ee.DAO..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">
<aop:pointcut id="daoOne" expression="execution(* com.aa.ee.DAO.impl.*.*(..))" />
<aop:pointcut id="daoTwo" expression="execution(* com.aa.ee.DAO.datasetImpl.*.*(..))" />
<aop:before pointcut-ref="daoOne" method="setdataSourceOne" />
<aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />
</aop:aspect>
</aop:config>
sessionFactory使用部分(DatasetSnapshotMonitor 是数据库Two中的表):
@Resource
private SessionFactory sessionFactory;
public DatasetSnapshotMonitor getById(int id) {
Criteria criteria=sessionFactory.getCurrentSession().createCriteria(DatasetSnapshotMonitor.class);
criteria.add(Restrictions.eq("id",id));
DatasetSnapshotMonitor result= (DatasetSnapshotMonitor) criteria.uniqueResult();
return result;
}
最后报错:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'one.dataset_snapshot_monitor' doesn't exist.
dataset_snapshot_monitor应该属于database two的,说明sessionFactory并没有动态绑定到数据库two,请问大神们大概什么原因?
补充一点:sessionFactory.getSessionFactoryOptions().getInterceptor()
输出为org.hibernate.EmptyInterceptor@b4f7c62,这个算异常嘛?
另外附上一张异常配置图,第二个<aop:pointcut的expression显示不正常,下面的pointcut-ref不正常红字
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决,换了一种方式,配置两个sessionFactory
然后在使用的时候通过@Qualifier指明是哪一个session
或者
另外一点需要注意,会报一个错:No bean named 'sessionFactory' is defined
解决方案:
在web.xml
后面添加: