本文介绍了Hibernate 配置多个数据源和多个会话工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 和 Hibernate,spring 配置如下.我如何配置两个数据源,会话工厂.事务使用注解进行管理.请指教

I am using Spring and Hibernate the spring configuration is as below. How do i configure two data sources, session factories. The transaction is managed using annotations. Please advise

<!-- we can use annotations -->
<context:annotation-config/>

<!-- package to look for annotated classes -->
<context:component-scan base-package="com.XXX.XXX.service.impl"/>

<!-- we will manage transactions with annotations -->
<tx:annotation-driven/>


<bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- configure hibernate session factory -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    p:url="jdbc:sqlserver://DB_NAMEDB_INSTANCE:DB_PORT;databaseName=DB_NAME;username=DB_USER;password=DB_PASSWORD;" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="show_sql">false</prop>
        </props>
    </property>

</bean>

推荐答案

<!-- configure hibernate session factory for FirstDB -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    p:url="jdbc:sqlserver://${FirstDB_DB_HosName}${FirstDB_DB_instanceName}:${FirstDB_DB_PortNumber};databaseName=${FirstDB_DB_DatabaseName};username=${FirstDB_DB_UserName};password=${FirstDB_DB_Password};" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="show_sql">false</prop>
        </props>
    </property>

</bean>


 <!-- configure hibernate session factory for SecondDB database -->

<bean id="SecondDBdataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    p:url="jdbc:sqlserver://${SecondDB_DB_HOST}${SecondDB_DB_INSTANCE}:${SecondDB_DB_PORT};databaseName=${SecondDB_DB_DATABASENAME};username=${SecondDB_DB_USERNAME};password=${SecondDB_DB_PASSWORD};" />


 <bean id="secondDBSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="SecondDBdataSource" />
    <property name="configLocation">
            <value>classpath:hibernate-SecondDB.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="show_sql">false</prop>
        </props>
    </property>

</bean>

在Hibernate DAO中,我们可以使用@Qualifier注解如下连接2个会话工厂

In the Hibernate DAO we can use @Qualifier annotation as below to wire the 2 session factories

/**
 * Basic DAO operations dependent with Hibernate's specific classes
 * @see SessionFactory
 */
@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
public class HibernateDao<E, K extends Serializable> implements GenericDao<E, K> {

  @Autowired
  @Qualifier(value="sessionFactory")
  private SessionFactory sessionFactory;

  @Autowired
  @Qualifier(value="secondDBSessionFactory")
  private SessionFactory secondDBSessionFactory;


  protected Class<? extends E> daoType;

  public HibernateDao() {
    daoType = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass())
                    .getActualTypeArguments()[0];
  }

  //Remaining Code
}

这篇关于Hibernate 配置多个数据源和多个会话工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:06