Java Spring-Hibernate 使用多个数据源/数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24034015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Hibernate using multiple datasource/database
提问by splatter_fadli
I'm working on a web application that uses Spring MVC 3 and Hibernate
我正在开发一个使用 Spring MVC 3 和 Hibernate 的 Web 应用程序
I want to use 2 datasource MySql and Oracle databases for my web application,
我想为我的 Web 应用程序使用 2 个数据源 MySql 和 Oracle 数据库,
I've been read many tutorial and problem solving for "spring-hibernate multiple datasource/database" for example :
我已经阅读了许多有关“spring-hibernate 多数据源/数据库”的教程和问题解决方法,例如:
and etc.
等等。
but when every single time I run it, the config just read my first database config (MySql)
and show this error Table 'db_prod.ksei_lookup_holiday' doesn't exist
db.prod is my first database(MySql) and KSEI_LOOKUP_HOLIDAY is my second database (Oracle),
但是当我每次运行它时,配置只是读取我的第一个数据库配置(MySql)并显示此错误Table 'db_prod.ksei_lookup_holiday' doesn't exist
db.prod 是我的第一个数据库(MySql),而 KSEI_LOOKUP_HOLIDAY 是我的第二个数据库(Oracle),
this is my spring.xml
这是我的 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>
this is my DAO Implementation for my first database (MySql)
这是我的第一个数据库 (MySql) 的 DAO 实现
@Repository
@Qualifier(value="sessionFactory")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
@Autowired
private UserDaoImpl(SessionFactory sessionFactory){
setSessionFactory(sessionFactory);
}
this is my DAO Implementation for my second database (Oracle)
这是我的第二个数据库 (Oracle) 的 DAO 实现
@Repository
@Qualifier(value="sessionFactorySOAAPP")
public class UpdateKSEIDaoImpl extends HibernateDaoSupport implements UpdateKSEIDao{
@Autowired
private UpdateKSEIDaoImpl(SessionFactory sessionFactorySOAAPP){
setSessionFactory(sessionFactorySOAAPP);
}
any help will be pleasure :)
任何帮助都会很高兴:)
采纳答案by geoand
The problem is that you have used
问题是你用过
<property name="dataSource" ref="dataSource"></property>
in sessionFactorySOAAPP
.
<property name="dataSource" ref="dataSource"></property>
在sessionFactorySOAAPP
。
You should have used <property name="dataSource" ref="dataSourceSOAAPP"></property>
你应该用过 <property name="dataSource" ref="dataSourceSOAAPP"></property>
回答by Praveen Tyagi
If you check "sessionFactorySOAAPP" then the below property name should be 'dataSourceSOAAPP' not the 'dataSource'.
如果您选中“sessionFactorySOAAPP”,那么下面的属性名称应该是“dataSourceSOAAPP”而不是“dataSource”。
回答by Iman Namvar
this my configuration file:
这是我的配置文件:
<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" />
回答by Jake
@geoand is correct in the error he spotted. Other than that the context xml seems correct and works for me. However, for me, this only works if @qualifier is with the @Autowired.
@geoand 在他发现的错误中是正确的。除此之外,上下文 xml 似乎是正确的并且对我有用。但是,对我来说,这仅在@qualifier 与@Autowired 一起使用时才有效。
@Repository
public class BusinessDaoImpl implements BusinessDao
{
@Autowired
@Qualifier(value="sessionFactory")
SessionFactory sessionFactory;