Requirement: We have a functionaltiy which needs to update both databases in a single transaction ( ie) make it atomic
Here is our current environment: there are two databases -->
Database1 and Database2( mapping shown below). They both reside on the same SQL server. Initially my thoughts were that we use two phase commit since I have created two session factory, also in which case I would need to use XA datasource and use JNDI.
But after talking to my SQL people, they advised me that since the two databases reside on the same SQL server, we dont need to use two phase commit but use one database connection and when updating both the databases specify the Database name.Table name then the database would understand which database and table to update and in this way we can update both the database and make it in a single transaction. I feel this is possible by using a pure JDBC connection, but we would like to use Hibernate and we dont want to bypass it.
So the question arises, is there a way to do this by hibernate? and do I need to use two phase commit?
here is my current mapping:
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDrive r</value>
</property>
<property name="url">
<value>database1</value>
</property>
<property name="username"><value>abc</value></property>
<property name="password"><value>def</value></property>
</bean>
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource"><ref local="dataSource1"/></property>
<property name="mappingResources">
<list>
<value>abc.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager1" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory1"/></property>
</bean>
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDrive r</value>
</property>
<property name="url">
<value>database2</value>
</property>
<property name="username"><value>xxx</value></property>
<property name="password"><value>yyy</value></property>
</bean>
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource"><ref local="dataSource2"/></property>
</property>
<property name="mappingResources">
<list>
<value>bbb.hbm.xml</value>
</list>
</property>
</bean>
|