0
votes

I have two databases wtis the same scheme.

My hibernate 3.2/Spring 3 application have one datasource (via jndi) for one each database, and one transactino manager (HibernateTransactionManager) for each datasource.

Question: Can i use one instance of session factory, that should use datasource associated with current transaction?

Suppose, there are the following methods.

@Transactional(readOnly = true, value = "tmDBOne")
public String db1() throws IOException {
    dao.execute(); // dao uses injected session factory
}


@Transactional(readOnly = true, value = "tmDBTwo")
public void db2() throws IOException {
    dao.execute(); // dao uses injected session factory
}

trnsaction managers:

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

<bean id="tmDBTwo"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dsDBTwo/>
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
1

1 Answers

0
votes

No, you will need to have two sessionFactories. SessionFactory manages the second level cache - if data from two different sources get combined in the second level cache that will lead to problems.

If you are not using second level cache, you can consider using the "Dynamic data source routing" support provided by spring to switch the underlying data source at run time.

This will work only if both data sources have the same schema (which seems to be in your case) .