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:
- Perhaps you have a complicated I/O system in your app andhave multiple threads reading to/writing from the same file on disk. Perhaps you would have each thread use a transaction to write to this file. (Yes?!? No?!?)
- Perhaps you have a state machine POJO representing the state of a system, and multiple threads can modify the machine. Perhaps you would have each thread use a transaction to alter the machine's state. (Yes?!? No?!?)
I guess at the root of my question is:
- If my JPA (Hibernate) and/or JDBC calls are already transactional, why would I want to wrap them inside JTA begin->commit/rollback block? Ditto for JMS and messaging systems.
- Outside of JPA/JDBC/JMS, what use cases exist for using JTA to transact a series of operations?
Thanks in advance!
XAResource
for them? Again, for the bounty, I'm looking for a set of guidelines to use when deciding whether or not something should be transacted. Thanks again! – Anatomical