Java Transaction Service

I want to use Java Transaction Service to manage distributed transactions in my Java application.

I have 3 different databases that I have to connect to using 3 different Connection objects. I want to insert specific data into each of the three databases. My requirement is that atomicity must be maintained. Therefore, either the data must be inserted into all 3 databases, or it must not be inserted into any of the databases. I searched the net for this kind of transaction and I got Java Transaction service. I could find its API here http://java.sun.com/products/jts/javadoc/index.html But I still don't understand how to implement transactions using this. Can anyone provide me with links to sample code or Java Transaction Service tutorials.

Thanks, Advance, Aniket Kedari

+2


source to share


3 answers


Some moments:

  • XA is not the only possible solution to solve this problem, although it is probably the simplest one due to the maturity of the tools around it (CAP and BASE theorem search - mostly available Soft-state is ultimately consistent).
  • XA transactions have failure modes, and you can still get windows of inconsistency between participating databases.
  • You don't say which databases you are using - perhaps one or all of them do not support XA transactions.
  • It is unlikely that you want to use the raw JTS / JTA APIs in Java, you can instead use any of the well-known Java application servers as they contain a transaction manager (which uses JTS / JTA).


Therefore, assuming you have decided to use a Java Application Server, I would suggest using Spring, EJB 3.0 or something similar to interact with the database, as they will allow you to do declarative transactions, which are much cleaner than manually writing the transaction logic yourself.

Here's a link to the Spring transaction documentation.

+3


source


The topic you need to find is XA Transactions.

What you need to know is clearly visible here:



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

Distributed Transaction Processing Systems are designed for transactions that span heterogeneous, transactional resources in a distributed environment. Using distributed transactions, an application can perform 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 describes some of the use cases for transaction (XA), and how an application can transactional processing using JTA along with the best breeds of technology. The focus is on using Spring as a server-side framework and how you can integrate different JTA implementations for enterprise-level distributed transactions.

0


source


For simple situations using a single database, you don't worry about it, a combination of JDBC and native database capabilities is sufficient. Your situation is more complicated. There should be a transaction manager that keeps track of all the details of managing distributed transactions across databases. Thus, you need an implementation that offers the JTA aspect.

While you could in principle develop this self, it is a very difficult job, so in practice you need to use an existing implementation. This is one of the things you get when you use Java EE Application Server.

So go and get one of the many Java EE App servers available, there are good zero costs.

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

Whichever you choose, make sure you use your JDBC connection pools for your JDBC connections (very easy to do) and I would suggest using a simple Bean session (very simple in EJB 3 too) to mark up your trades.

In all, you need to write about 4 new lines of code (or annotations). over what you have now, and "bingo!" you are doing 2PC transactions. There is a bit of a learning curve in getting application servers and learning how to use these tools, but if you want distributed Java transactions, you need infrastructure services and infrastructure for programming, so some level of learning is inevitable.

0


source







All Articles