Reputation:
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
Upvotes: 2
Views: 2732
Reputation: 55957
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 Java EE application server.
So, go and get one of the many available Java EE 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.
Upvotes: 0
Reputation: 5405
Some points:
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.
Upvotes: 3
Reputation: 34321
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.
Upvotes: 0