views:

205

answers:

3

Hi all, I want to use Java Transaction service for distributed transaction management in my Java application.

I have 3 different databases to which I have to connect using 3 different Connection objects. I want to insert certain data in each of the 3 databases. My requirement is that atomicity should be maintained. So either data should get inserted in all 3 databases or it should not get inserted in any of the databases. I searched on net for this kind of transactions and I got Java Transaction service. I could find its API here http://java.sun.com/products/jts/javadoc/index.html But I am still not getting how implement transactions using it. Can somebody provide me with links to sample code or tutorials of Java Transaction Service.

Thanks in Advance, Aniket Kedari

A: 

The topic you need to search on is XA transactions.

What you need to know is covered well here:

http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html

Distributed transaction processing systems are designed to facilitate transactions that span heterogeneous, transaction-aware resources in a distributed environment. Using distributed transactions, an application can accomplish tasks such as retrieving a message from a message queue and updating one or more databases in a single transactional unit adhering to the ACID (Atomicity, Consistency, Isolation and Durability) criteria. This article outlines some of the use cases where distributed transactions (XA) could be used and how an application can achieve transactional processing using JTA along with the best of the breed technologies. The main focus is on using Spring as a server framework and how one can integrate various JTA implementations seamlessly for enterprise level distributed transactions.

Nick Holt
Hi all, Thank you for your valuable suggestions.I tried to use XAResource.http://www.datadirect.com/developer/jdbc/topics/jta/dist-trans/index.sspBut unfortunately my datasource for my databse 'hp Non Stop Databse' does not support XAresource.I will search for CAP theorem and BASE now.
A: 

For a simple situations using a single database you don't worry about this, the combination of JDBC and the database's own capabilities are sufficient. Your situation is more complex. There needs to be a Transaction Manager, which looks after all the details managing distributed transactions across the databases. So you need an implementation offering the JTA apis.

Although you could, in principle develop this your self, it's a very speciliased piece of work so practically you need to use an existing implementation. This is one of the things you get when you use a JEE application server.

So, go and get one of the many available JEE App Servers, there are good zero-cost ones.

WebSphere Community Edition is IBM's, JBOSS is a widely used server, Glassfish is available from Sun.

Which ever one you pick, make sure you use their JDBC connection pools for your JDBC connections (very easy to do) and I would suggest using simple Session Bean (in EJB 3 this is also really easy) to demark your transactions.

Overall, you'll need to write about 4 new lines of code (or annotations). over what you have now and "bingo!" you're doing 2PC transactions. There is a bit of a learning curve in getting the App Servers up and learning how to use the facilities but if you need Java distributed transactions you need infrastructure services and prgramming framework so some level of learning is inevitable.

djna
+1  A: 

Some points:

  1. XA is not the only possible solution for solving this problem, though it probably the simplest because of the maturity of tools around it (search for CAP theorem and BASE - Basically Available Soft-state Eventually consistent).
  2. XA transactions have failure modes and you can still get windows of inconsistency between the participating databases.
  3. You don't say which database(s) you are using - maybe one or all of them don't support XA transactions.
  4. It's unlikely that you want to use the raw JTS/JTA APIs in Java, instead you can use any of the well-known Java application servers as they contain a transaction manager (which uses JTS/JTA).

So assuming you decide to use a Java Application Server, I'd suggest using Spring, EJB 3.0 or something similar for your database interaction as they will allow you to do declarative transactions which is much cleaner than manually writing transaction logic yourself.

Here's a link to Spring's documentation on transactions.

hbunny