We are Borg
We are Borg

Reputation: 5311

Hibernate, PostgreSQL : Write to 2 different databases on different servers.

I am working on a Spring-MVC application which we have running on 2 servers, one is our testing server, 2nd one is our live server. Is there any way that I can configure Hibernate to write any and all DB related queries on both servers. Example : User A saved Object A on live server, then write the same object on Test-server, vice-versa is not required. Our test and live server both have same setting and same database. Thank you.

root-context.xml :

   <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver"/>
        <beans:property name="url"
                        value="jdbc:postgresql://localhost:PORT/DB_NAME"/>
        <beans:property name="username" value="USERNAME"/>
        <beans:property name="password" value="PASSWORD"/>
        <beans:property name="removeAbandoned" value="true"/>
        <beans:property name="removeAbandonedTimeout" value="20"/>
        <beans:property name="defaultAutoCommit" value="false"/>
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="packagesToScan" value="com.tooltank.spring.model"/>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <!--   <beans:prop key="hibernate.jdbc.batch_size">1000</beans:prop>-->
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
                <beans:prop key="cache.use_second_level_cache">true</beans:prop>
                <beans:prop key="cache.use_query_cache">true</beans:prop>
                <beans:prop key="hibernate.order_updates">true</beans:prop>
                <beans:prop key="show_sql">false</beans:prop>
            </beans:props>
        </beans:property>

    </beans:bean>

   <hibernate.version>4.3.9.Final</hibernate.version>

If anymore data is required, please let me know. Thank you.

Upvotes: 0

Views: 123

Answers (1)

Kayaman
Kayaman

Reputation: 73558

Not in any way that you're probably hoping for. They have completely different transactional contexts, so you can't just pretend that 2 databases are one.

What if one database throws an exception and the other one doesn't? Did the tx commit or roll back? Hibernate certainly doesn't expect it to be "half success".

You could make the test server a slave of the live server and just write to the live server. This is not a simple solution either and can be outright stupid.

Upvotes: 1

Related Questions