title: Connect to multiple DB from Hibernate and Spring
body: <p>I need to connect to two DB in my spring hibernate application.</p>
  
  <p>The configuration is as following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
  xmlns:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-3.1.xsd
         http://www.springframework.org/schema/jms 
         http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"&gt;
  
  &lt;context:property-placeholder location="classpath:eloan.properties" /&gt;
  
  &lt;context:annotation-config /&gt;
  
  &lt;context:component-scan base-package="com.eloan" /&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"
      proxy-target-class="true" /&gt;
  
  
  &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
  
      &lt;property name="properties"&gt;
          &lt;props&gt;
              &lt;prop key="useUnicode"&gt;true&lt;/prop&gt;
              &lt;prop key="characterEncoding"&gt;UTF-8&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
      &lt;property name="driverClass" value="${jdbc.driver.classname}" /&gt;
      &lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;
  
      &lt;property name="initialPoolSize" value="${c3p0.connections.min}" /&gt;
      &lt;property name="minPoolSize" value="${c3p0.connections.min}" /&gt;
      &lt;property name="maxPoolSize" value="${c3p0.connections.max}" /&gt;
  
      &lt;property name="user" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  
  &lt;/bean&gt;
  
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
  
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="com.eloan" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Declare a transaction manager --&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- a test to connect to a second DB --&gt;
  &lt;bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
  
      &lt;property name="properties"&gt;
          &lt;props&gt;
              &lt;prop key="useUnicode"&gt;true&lt;/prop&gt;
              &lt;prop key="characterEncoding"&gt;UTF-8&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
      &lt;property name="driverClass" value="${jdbc.driver.classname}" /&gt;
      &lt;property name="jdbcUrl" value="${jdbc.url2}" /&gt;
  
      &lt;property name="initialPoolSize" value="${c3p0.connections.min}" /&gt;
      &lt;property name="minPoolSize" value="${c3p0.connections.min}" /&gt;
      &lt;property name="maxPoolSize" value="${c3p0.connections.max}" /&gt;
  
      &lt;property name="user" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory2"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
  
      &lt;property name="dataSource" ref="dataSource2" /&gt;
      &lt;property name="packagesToScan" value="com.eloan" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>I try to get two different session factories in the code but its not working
  I'm getting this error:</p>
  
  <pre><code>25731  [23 Jun 2013 15:41:36 512] [qtp259913092-20] [ERROR] errorId=org.hibernate.HibernateException: No Session found for current thread 
  </code></pre>
  
  <p>I tried to define my class as following:</p>
  
  <pre><code>@Repository
  public class UsersBMDao extends HibernateTemplate{
      private static Logger LOG = LoggerFactory.getLogger(UsersBMDao.class);
  
      @Autowired
      SessionFactory sessionFactory;
  
      @Autowired
      public UsersBMDao(@Qualifier("sessionFactory2") SessionFactory sessionFactory) {
          super(sessionFactory);  
      }
  
      public List&lt;UsersBM&gt; getAllUsers(){
          Criteria crit = sessionFactory.getCurrentSession().createCriteria(UsersBM.class);
          return crit.list();
      }
  }
  </code></pre>
  
  <p>But its not working - any direction will help!</p>
  
  <p>Thanx</p>
  
  <p>---------------- I've tried to add @Transactional to the class (as suggested) and getting this exception:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eloanDataController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.LoanService com.eloan.controller.EloanDataController.loanService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loanService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.SapLoanDetailsService com.eloan.service.LoanService.sldService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sapLoanDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.eloan.service.dao.UsersBMDao com.eloan.service.SapLoanDetailsService.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersBMDao' defined in file [/Users/nir/workspace/eloan/target/classes/com/eloan/service/dao/UsersBMDao.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.eloan.service.dao.UsersBMDao]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
  at javax.servlet.GenericServlet.init(GenericServlet.java:244) [javax.servlet-3.0.0.v201112011016.jar:na]
  at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:517) [jetty-servlet-8.1.7.v20120910.jar:8.1.7.v20120910]
  </code></pre>