Mahmoud Saleh
Mahmoud Saleh

Reputation: 33635

What is the common solution for multiple datasources?

Greetings all In my spring application I will need to use hibernate with two different databases (PostgreSQL & MySQL) and I am not pretty good with configuration, so I need some guide about how to do so

I am using the following configuration for hibernate-postgresql

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.project.domain.myDomain</value>
            </list>

        </property>

        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
            </value>
        </property>


    </bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.postgresql.Driver" />

        <property name="url">
        <value>${db.url}</value> 
        </property>

        <property name="username" > 
        <value>${db.username}</value>
        </property>

        <property name="password">  
        <value>${db.password}</value>
        </property>

    </bean>

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

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

and in the DAO I make an autowire for the sessionFactory.

Upvotes: 1

Views: 3239

Answers (3)

fmucar
fmucar

Reputation: 14558

You probably need XA drivers.

See below for an example

http://docs.codehaus.org/display/BTM/Home

Upvotes: 0

lweller
lweller

Reputation: 11327

The only way is to have to Data Sources with their one stack (SessionFactory, Hibernate Template, ect.). Then you can inject the HIbernate Tempolate you want to use in you business classes (or both if you want to access both DB at same time).

here is an example DAO with explicit config ...

<bean id="db2SessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
</bean>

<bean id="db1Dao" class="ch.sbb.uno.dao.hibernate.UnoHibernateDaoSupport" scope="prototype">
    <property name="sessionFactory" ref="db1SessionFactory" />  
</bean>

<bean id="db2Dao" class="ch.sbb.uno.dao.hibernate.UnoHibernateDaoSupport" scope="prototype">
    <property name="sessionFactory" ref="db2SessionFactory" />  
</bean>

Upvotes: 1

duffymo
duffymo

Reputation: 309028

If you have two databases, and you want two-phase commit, you'd better be sure to use XA drivers for both.

Upvotes: 2

Related Questions