Cranialsurge
Cranialsurge

Reputation: 6184

How to handle multiple database connections using session factories with Spring and Hibernate

I'm really new to Hibernate and have been mulling over something for a while now. I have two databases that I have JNDI connection strings defined to in Tomcat's context.xml. In my application which uses Spring and Hibernate I have 2 session factories the first one is as follows -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
  <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.connection.pool_size">10</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.transaction.auto_close_session">true</prop>
    <prop key="hibernate.transaction.flush_before_completion">true</prop>
    <prop key="current_session_context_class">true</prop>

    <!--HSQL-->
    <prop key="hibernate.connection.datasource">java:comp/env/jdbc/xxx</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>

  </props>
</property>
<property name="annotatedClasses">
  <list>
    <value>com.mytest.examples.Person</value>
    <value>com.mytest.examples.Customer</value>
    <value>com.mytest.examples.Employee</value>
  </list>
</property>

The second sessionFactory points to the second database as follows

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
  <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.connection.pool_size">10</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.transaction.auto_close_session">true</prop>
    <prop key="hibernate.transaction.flush_before_completion">true</prop>
    <prop key="current_session_context_class">true</prop>

    <!--HSQL-->
    <prop key="hibernate.connection.datasource">java:comp/env/jdbc/yzz</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>

  </props>
</property>
<property name="annotatedClasses">
  <list>
    <value>com.mytest.examples.Container</value>
    <value>com.mytest.examples.Credentials</value>
  </list>
</property>

Now when I try and make use of these factories using the following

Session session = getHibernateTemplate().getSessionFactory().openSession();

I always get the first factory by default and am able to access all the tables in its schema but not the second one. How do I specify which factory I want to use?

Upvotes: 1

Views: 7077

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

The HibernateTemplate constructor takes a SessionFactory as argument. I don't know what getHibernateTemplate() does in your code, but it should return a HibernateTemplate built with one of the SessionFactory beans you defined (either by declaring them in the spring context xml file, or by constructing them in Java from one of the injected session factories).

Note that, as the documentation of HibernateTemplate says (in bold):

As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().

I would inject the session factory directly, and use the Hibernate API directly. HibernateTemplate doesn't bring much over the Hibernate API and often gets in the way, IMHO. (For example by not providing an equivalent to Query.uniqueResult()).

Upvotes: 1

Related Questions