views:

146

answers:

2

I have a scenario where the unit of work is defined as:

Update table T1 in database server S1
Update table T2 in database server S2

And I want the above unit of work to happen either completely or none at all (as the case with any database transaction). How can I do this? I searched extensively and found this post close to what I am expecting but this seems to be very specific to Hibernate.

I am using Spring, iBatis and Tomcat (6.x) as the container.

+5  A: 

It really depends on how robust a solution you need. The minimal level of reliability on such a thing is XA transactions. To use that, you need a database and JDBC driver that supports it for starters, then you could configure Spring to use it (here is an outline).

If XA isn't robust enough for you (XA has failure scenarios, such as if something goes wrong in the second phase of commits, such as a hardware failure) then what you really need to do is put all the data in one database and then have a separate process propagate it. So the data may be inconsistent, but it is recoverable.

Edit: What I mean is that put the whole of the data into one database. Either the first database, or a different database for this purpose. This database would essentially become a queue from which the final data view is fed. The write to that database (assuming a decent database product) will be complete, or fail completely. Then, a separate thread would poll that database and distribute any missing data to the other databases. So if the process should fail, when that thread starts up again it will continue the distribution process. The data may not exist in every place you want it to right away, but nothing would get lost.

Yishai
"If XA isn't robust enough for you then what you really need to do is put all the data in one database and then have a separate process propagate it"Could you please explain this point? What do you mean by 'put all data in one place' and 'separate process propagate it'
peakit
+2  A: 

You want a distributed transaction manager. I like using Atomikos which can be run within a JVM.

Jherico
Is it free and open source?
peakit
@peakit, looking at their website, they have a free (registration required to actually get to the download) Apache 2.0 licensed version, and a more advanced closed source version.
Yishai