title: How to use two databases/datasources in Spring and Hibernate
body: <p>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:</p>
  
  <pre><code>@Transactional
  public abstract class HibernateDao&lt;T, ID extends Serializable&gt; implements Dao&lt;T, ID&gt;{
  
  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&lt;T&gt; find(String query) {
      return getCurrentSession().createQuery(query).list();
  }
  
  public Session getCurrentSession(){
      return sessionFactoryHSQL.getCurrentSession();
  }
  }
  
  
  &lt;bean id="sessionFactoryHSQL" 
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
  
      &lt;property name="dataSource"&gt;
        &lt;ref bean="dataSourceHSQL"/&gt;
      &lt;/property&gt;
  
      &lt;property name="hibernateProperties"&gt;
         &lt;props&gt;
           &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/prop&gt;
           &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
           &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
         &lt;/props&gt;
      &lt;/property&gt;
  
      &lt;property name="annotatedClasses"&gt;
      &lt;list&gt;
          //annotated classes
      &lt;/list&gt;
      &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;bean id="txManager" class=
      "org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
     &lt;property name="sessionFactory" ref="sessionFactoryHSQL" /&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven transaction-manager="txManagerHSQL" /&gt;
  
  &lt;bean id="configHSQL"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="location"&gt;
          &lt;value&gt;properties/HSQL.properties&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSourceHSQL" 
           class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>public void persist(T object) {
      sessionFactoryHSQL.getCurrentSession().persist(object);
      sessionFactoryMySQL.getCurrentSession().persist(object);
  }
  </code></pre>
  
  <p>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.</p>