Reputation: 1383
I need to use two databases in my standalone application, one local HSQLDB and another remote MySQL. I use Spring 3.1.2, Hibernate 4.1.7. So far I managed to use only HSQLDB, this is how my DAO and configurations look like:
@Transactional
public abstract class HibernateDao<T, ID extends Serializable> implements Dao<T, ID>{
SessionFactory sessionFactoryHSQL;
public void setSessionFactoryHSQL(SessionFactory sessionFactory) {
this.sessionFactoryHSQL = sessionFactory;
}
public ID save(T object) {
return (ID) getCurrentSession().save(object);
}
public void persist(T object){
getCurrentSession().persist(object);
}
public void update(T object) {
getCurrentSession().update(object);
}
public void delete(T object) {
getCurrentSession().delete(object);
}
public List<T> find(String query) {
return getCurrentSession().createQuery(query).list();
}
public Session getCurrentSession(){
return sessionFactoryHSQL.getCurrentSession();
}
}
<bean id="sessionFactoryHSQL"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSourceHSQL"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
//annotated classes
</list>
</property>
</bean>
<bean id="txManager" class=
"org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryHSQL" />
</bean>
<tx:annotation-driven transaction-manager="txManagerHSQL" />
<bean id="configHSQL"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>properties/HSQL.properties</value>
</property>
</bean>
<bean id="dataSourceHSQL"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
I created separate beans for MySQL, such as sessionFactoryMySQL, dataSourceMySQL (I'm not going to paste their implementations as they are nearly the same as for HSQL). I managed to correctly inject sessionFactoryMySQL to DAO, but I can't use both sessionFactorys at the same time, for example I can't do following:
public void persist(T object) {
sessionFactoryHSQL.getCurrentSession().persist(object);
sessionFactoryMySQL.getCurrentSession().persist(object);
}
As I already discovered, it has something to do with transactions, but I can't properly configure transaction manager. I have been searching for solution for a long time but examples or tutorias was either not clear or did not refer to exaclty my case.
Upvotes: 2
Views: 1377
Reputation: 658
You need to create two Session Factories for both database. Check the simple step by step example : Two Datasource in Spring/Hiberate
Upvotes: 2