Home  >  Forum  >  Spring
Post New Query

Two Dabase connection using spring with Hibernate


joined on
October 24,2013
Asked on October 24,2013

Hi All

 I want to connect two database in my application as of now we are saving and retrieving data in single database only, now we have the requirement like some data we need pick up from one database and some data from another database. So how could I connect to different database using my application with spring and hibernate, and it has to support the transaction management for two database .

 

Thanks in advance for your valuable information.

 

Thanks

Lokee Yadav  

joined on
March 14,2013
Replied on October 24,2013

Create two hibernate1.xml and  hibernate2.xml with different database configuration and create two session factory and hibernate template. you can do as below


<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="configLocation" ><value>hibernate1.cfg.xml</value></property>
</bean>
<bean id="hibernateTemplate1" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
 <ref bean="sessionFactory1" />
</property>
</bean>
<bean id="pageDao1" class="com.concretepage.dao.PageDaoImplOne">
    <property name="hibernateTemplate">
 <ref bean="hibernateTemplate1" />
</property>
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="configLocation" ><value>hibernate2.cfg.xml</value></property>
</bean>
<bean id="hibernateTemplate2" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
 <ref bean="sessionFactory2" />
</property>
</bean>
<bean id="pageDao2" class="com.concretepage.dao.PageDaoImplTwo">
    <property name="hibernateTemplate">
 <ref bean="hibernateTemplate2" />
</property>
</bean>


joined on
October 24,2013
Replied on October 25,2013

Thanks for your quick reply ,

As we suggested we can create the two datasources objects then we can create the two sessionFactoryObjects but  how can we create the transaction Manager for  two sessionFactoryObjects  ?

 

I have done as you suggested in earlier but I failed while applying transaction manager .

Below of my code for the transaction.

 

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory1" />

<ref local="sessionFactory2" />

</property>

</bean>

 

pls suggest on the same. 

 

Write Answer



Sitewide-Feb4of4234x60





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us