简体   繁体   中英

connecting to databases using spring and hibernate - primary and secondary databases

Hi this is what I am looking to do -

I have three databases - each exactly the same - one is primary and two and three are back ups.

I am using hibernate and spring to connect to the data base.I have spring application context file configured with 3 session factories, and 3 corresponding data sources.

I was wondering if there is a way using spring where if the application is not able to connect to the database 1 (an exception is thrown) it will connect to database 2 - and in turn is database 3 is down it will connect to 3 and proceed.

What would be the best way to implement this? I have read about AbstractRoutingDataSource ..but not sure how to make use of that in this case..also if there are other ideas.. Would appreciate some directions. Thanks!

this is my config in application context xml -

<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="$g{jdbc.driverClassName}" />
        <property name="jdbcUrl" value="$g{url1}" />
        <property name="user" value="$l{uid1}" />
        <property name="password" value="$l{pwd1}" />
        <property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
        <property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
        <property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
        <property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
    </bean>

    <bean id="sessionFactory1"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource1" />
        <property name="packagesToScan">
            <list>
                <value>com.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="$g{jdbc.driverClassName}" />
        <property name="jdbcUrl" value="$g{url2}" />
        <property name="user" value="$l{uid2}" />
        <property name="password" value="$l{pwd2}" />
        <property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
        <property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
        <property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
        <property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
    </bean>

    <bean id="sessionFactory2"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource2" />
        <property name="packagesToScan">
            <list>
                <value>com.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="$g{jdbc.driverClassName}" />
        <property name="jdbcUrl" value="$g{url3}" />
        <property name="user" value="$l{uid3}" />
        <property name="password" value="$l{pwd3}" />
        <property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
        <property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
        <property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
        <property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
    </bean>

    <bean id="sessionFactory3"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource3" />
        <property name="packagesToScan">
            <list>
                <value>com.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager1"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory1" />
    </bean>

    <bean id="transactionManager2"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory2" />
    </bean>

    <bean id="transactionManager3"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory3" />
    </bean> 

There is a way to achieve this by using Spring Abstract Data Source Routing mechanism -

https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM