I'm trying to understand JTA and am using Bitronix as the Transaction Manager of choice (just for the sake of learning and understanding). I'm looking at the code inside the Bitronix reference guide here and am wondering to myself: If I'm using JDBC, which itself is transactional (Connection
can be committed/rolled back), why would I ever want to write code like that?!?!
Now maybe the point of that code snippet was to simply demonstrate how to use Bitronix/JTA over an existing transactional datastore, but I still don't get what inherent benefits it offers.
Then, this code snippet got me thinking: "If the only two main datasources you use are databases and message brokers, and you use JDBC/JMS to communicate with them respectively, and these two standards (JDBC/JMS) are already transactional, then why would you ever need to use JTA at all?!?!"
Is JTA some sort of an "internal" Java EE API that JDBC, JPA, JMS, etc. all use; and is only publicly-exposed for the 1%ers out there that want to do something crazy with it? Or am I missing the idea/applicability of JTA altogether?
I guess I could envision two non-JDBC and non-JMS use cases for directly hitting JTA, but since I'm so fuzzy on JTA in the first place I have no idea if these cases are off-track or not:
I guess at the root of my question is:
Thanks in advance!
A JTA transaction is controlled by the Java EE transaction manager. You may want to use a JTA transaction because it can span updates to multiple databases from different vendors.
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.
plain JDBC is a technology for accessing databases. It is what Hibernate actually uses to perform the database operations, "under the hood". It uses JDBC to send queries to the database. JTA is a transaction API, and it is optional in Hibernate.
Spring provides support for JTA-based global transaction implementations through a PlatformTransactionManager implementation called JtaTransactionManager . If you use it on a JavaEE application server, it will automatically find the correct javax.
It is more than just rolling back open transaction, JTA provides an XAResource interface which providers can implement, your JDBC driver and JMS provider will already do this and you can implement your own. This is based on an open standard and is probably worth reading about.
Now why would we want this?
Consider Matthews example in a disaster:
Begin DB Transaction
Set AccountBalance $100 lower for Account #345 in database
Add JMS Message "Transfer $100 to OtherBank Account #987" to queue
*** DB power is unplugged while committing DB Transaction ***
Unfortunately the JMS message has gone to the other bank, this is a very real problem with distributed transactions.
With XA this is how the scenario will play out:
DB Transation starts XA Transaction
Set AccountBalance $100 lower for Account #345 in database
JMS Connection joins XA Transaction
Add JMS Message "Transfer $100 to OtherBank Account #987" to queue
Everything went okay and JTA context is ready to commit.
DB and JMS both agree that they are capable of commiting.
JTA instructs DB and JMS to commit.
All members of the transaction commit.
Now you may ask what happens if the power plug is pulled out during the DB's final commit. Well the JMS queue will have commited, however the XA transaction will remain open until the DB is once again available, at which point it will again instruct the DB to commit (the DB promised us it can commit, this is part of being XA compliant).
What is REALLY great about JTA is that you can easily implement your own custom XAResource and tie into this great framework!
UPDATE
So to answer your question about when to implement custom transactions you can ask yourself the following:
If the answer to both 1, 2 & 3 is YES then you probably want a custom XAResource, otherwise I think it is probably overkill.
However if your code is a framework or library which will be enlisted by business logic in the Java EE space, you may want to implement an XAResource for it!
You can have one transaction that covers both the database and the message service. I don't know of any way to do that without JTA.
Simple conceptual example. The exact code is not important:
Begin JTA Transaction
Set AccountBalance $100 lower for Account #345 in database
Add JMS Message "Transfer $100 to OtherBank Account #987" to queue
Commit JTA Transaction
OtherBank is a separate bank, and we assume you communicate with it over JMS.
The Begin Transaction and Commit are handled by JTA. If adding it to the queue fails, the withdrawal also rolls back automatically.
Real life isn't quite so simple, but this should give you an idea.
EDIT:
JTA can be used any time part of the system can correctly implement the XAResource
. By implementing that, a resource can participate in distributed transactions. At first glance, both of your proposed examples could possibly implement XAResource
.
"a complicated I/O system in your app and have multiple threads reading to/writing from the same file on disk." - This sounds like it could be a database, if you think about it.
"Perhaps you have a state machine POJO representing the state of a system, and multiple threads can modify the machine." - This could definitely be a candidate, if changes can be isolated sufficiently.
I think the first part of the rubric is basically that the resources logically could be XAResource
s. In other words, the concept of ACID is meaningful. The second is that it's possible and worth the effort to implement that interface (where it isn't implemented already).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With