Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between JTA and a local transaction?

Tags:

jta

What is the difference between JTA and a local transaction?

An example that shows when to use JTA and when to use a local transaction would be great.

like image 290
user1127214 Avatar asked Mar 04 '12 05:03

user1127214


People also ask

What is meant by JTA transaction?

The Java™ Transaction API (JTA) allows applications to perform distributed transactions, that is, transactions that access and update data on two or more networked computer resources.

What is a local transaction?

Whenever your application connected to a database using JDBC or a SQL server, you were creating a transaction. However, the transaction involved only the single database and all updates made to the database were committed at the end of these changes. This is referred to as a local transaction.

What is the difference between a local and a global transaction?

Global Transactions. Local transactions are specific to a single transactional resource like a JDBC connection, whereas global transactions can span multiple transactional resources like transaction in a distributed system.

What is resource local transaction?

A resource local transaction is a transaction that you have with a specific single resource using its own specific API. Such a transaction typically does not propagate to other methods in a call-stack and you are required to pass some explicit context object around.


2 Answers

JTA is a general API for managing transactions in Java. It allows you to start, commit and rollback transactions in a resource neutral way. Transactional status is typically stored in TLS (Thread Local Storage) and can be propagated to other methods in a call-stack without needing some explicit context object to be passed around. Transactional resources can join the ongoing transaction. If there is more than one resource participating in such a transaction, at least one of them has to be a so-called XA resource.

A resource local transaction is a transaction that you have with a specific single resource using its own specific API. Such a transaction typically does not propagate to other methods in a call-stack and you are required to pass some explicit context object around. In the majority of the resource local transactions it's not possible to have multiple resources participating in the same transaction.

You would use a resource local transaction in for instance low-level JDBC code in Java SE. Here the context object is expressed by an instance of java.sql.Connection. Other examples of resource local transactions are developers creating enterprise applications around 2002. Since transaction managers (used by JTA) were expensive, closed source and complicated things to setup around that era, people went with the cheaper and easier to obtain resource local variants.

You would use a JTA transaction in basically every other scenario. Very simple, small, free and open-source servers like TomEE (25MB) or GlassFish (35MB) have JTA support out of the box. There's nothing to setup and they Just Work.

Finally, technologies like EJB and Spring make even JTA easier to use by offering declarative transactions. In most cases it's advised to use those as they are easier, cleaner and less error prone. Both EJB and Spring can use JTA under the covers.

like image 83
Arjan Tijms Avatar answered Sep 28 '22 13:09

Arjan Tijms


Transaction-type should be set to "RESOURCE_LOCAL" for Java SE application and to "JTA" for Java EE application. "RESOURCE_LOCAL" may work fine on some web application deployed on Tomcat, but may cause issues when you run your application under glassfish environment.

If you are working on distributed transactions you must use "JTA" as your transaction manager.

like image 35
Mayank Jalotra Avatar answered Sep 28 '22 13:09

Mayank Jalotra