Spring-Hibernate 使用多个数据源/数据库
Posted
技术标签:
【中文标题】Spring-Hibernate 使用多个数据源/数据库【英文标题】:Spring-Hibernate using multiple datasource/database 【发布时间】:2014-07-24 21:38:04 【问题描述】:我正在开发一个使用 Spring MVC 3 和 Hibernate 的 Web 应用程序
我想为我的 Web 应用程序使用 2 个数据源 mysql 和 Oracle 数据库,
我已经阅读了很多关于“spring-hibernate multiple datasource/database”的教程和问题解决方案 例如:
directjump2java.blogspot.com
***
forum spring
等等
但是当我每次运行它时,配置只是读取我的第一个数据库配置(MySql)
并显示此错误Table 'db_prod.ksei_lookup_holiday' doesn't exist
db.prod 是我的第一个数据库(MySql),而 KSEI_LOOKUP_HOLIDAY 是我的第二个数据库(Oracle),
这是我的 spring.xml
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven transaction-manager="transactionManagerSOAAPP"/>
<context:annotation-config />
<context:component-scan base-package="prod.support" />
<!-- Database MySql, Desktop -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_prod" />
<property name="username" value="root" />
<property name="password" value="shikamaru" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="prod.support.model.splatter" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<!-- Database Oracle, Schema : SOAAPP -->
<bean id="dataSourceSOAAPP" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="splatter" />
<property name="password" value="shikamaru" />
</bean>
<bean id="sessionFactorySOAAPP"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="prod.support.model.soaapp" />
</bean>
<bean id="transactionManagerSOAAPP"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactorySOAAPP">
</bean>
这是我的第一个数据库 (MySql) 的 DAO 实现
@Repository
@Qualifier(value="sessionFactory")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao
@Autowired
private UserDaoImpl(SessionFactory sessionFactory)
setSessionFactory(sessionFactory);
这是我的第二个数据库 (Oracle) 的 DAO 实现
@Repository
@Qualifier(value="sessionFactorySOAAPP")
public class UpdateKSEIDaoImpl extends HibernateDaoSupport implements UpdateKSEIDao
@Autowired
private UpdateKSEIDaoImpl(SessionFactory sessionFactorySOAAPP)
setSessionFactory(sessionFactorySOAAPP);
任何帮助都会很高兴:)
【问题讨论】:
【参考方案1】:问题是你用过
<property name="dataSource" ref="dataSource"></property>
在sessionFactorySOAAPP
。
你应该用过<property name="dataSource" ref="dataSourceSOAAPP"></property>
【讨论】:
【参考方案2】:如果您选中“sessionFactorySOAAPP”,那么下面的属性名称应该是“dataSourceSOAAPP”而不是“dataSource”。
【讨论】:
【参考方案3】:这是我的配置文件:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/gl?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="2238295" />
</bean>
<bean id="mainDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/gl_main?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="2238295" />
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="sfAccounting"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.gl.domain.accounting" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="sfCommon"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="mainDataSource" />
<property name="packagesToScan" value="com.gl.domain.common" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txnManagerAccounting"/>
<tx:annotation-driven transaction-manager="txnManagerCommon"/>
<bean id="txnManagerAccounting"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sfAccounting" />
</bean>
<bean id="txnManagerCommon"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sfCommon" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
【讨论】:
【参考方案4】:@geoand 在他发现的错误中是正确的。除此之外,上下文 xml 似乎是正确的并且对我有用。 但是,对我来说,这仅在 @qualifier 与 @Autowired 一起使用时才有效。
@Repository
public class BusinessDaoImpl implements BusinessDao
@Autowired
@Qualifier(value="sessionFactory")
SessionFactory sessionFactory;
【讨论】:
以上是关于Spring-Hibernate 使用多个数据源/数据库的主要内容,如果未能解决你的问题,请参考以下文章