Quick revision
The short answer
JTA, now Jakarta Transactions, coordinates one transaction across several resources, such as two databases or a database and a JMS broker, using XA two-phase commit, and exposes it through interfaces such as UserTransaction and TransactionManager. A Spring application needs JtaTransactionManager, which delegates to a JTA implementation from an application server or a standalone one such as Narayana, only when one transaction must span more than one resource; most services avoid XA because of its cost and complexity and instead keep one resource per transaction with patterns such as the outbox and idempotent consumers.
What to remember
In this answer, we saw that local transaction managers such as DataSourceTransactionManager and JpaTransactionManager cover one resource, while JTA coordinates several resources with XA two-phase commit through interfaces like UserTransaction and TransactionManager. Spring's JtaTransactionManager delegates to a JTA implementation from an application server or a standalone one like Narayana, and because XA is costly and complex, most services prefer one resource per transaction with an outbox and idempotent consumers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
JTA, now Jakarta Transactions, coordinates one transaction across several resources, such as two databases or a database and a JMS broker, using XA two-phase commit, and exposes it through interfaces such as UserTransaction and TransactionManager. A Spring application needs JtaTransactionManager, which delegates to a JTA implementation from an application server or a standalone one such as Narayana, only when one transaction must span more than one resource; most services avoid XA because of its cost and complexity and instead keep one resource per transaction with patterns such as the outbox and idempotent consumers.
Why this question is asked
Spring's @Transactional hides which transaction manager runs underneath, and most of the time that is DataSourceTransactionManager or JpaTransactionManager working with a single database. Then one day a requirement arrives: update the orders database and send a JMS message, and both must happen or neither. Interviewers ask about JTA to see whether we understand what a single-resource transaction can and cannot guarantee, and whether we know why most modern systems choose not to use distributed transactions. In this answer, we will recap local transactions, see what JTA and two-phase commit add, look at the main interfaces, configure JtaTransactionManager in Spring, and discuss why most services avoid XA.
1A Quick Recap of Local Transactions
A local transaction is managed by one resource. DataSourceTransactionManager begins a transaction on a JDBC connection, and commit or rollback is a call on that connection. JpaTransactionManager does the same through the EntityManager, which also uses one JDBC connection. The database guarantees atomicity, so all changes on that connection are committed together or not at all.
The limit is in the name: one resource. If a method writes to the orders database and sends a message to a broker, those are two separate resources with two separate commits. If the database commit succeeds and the broker send fails, or the process crashes between them, we end up with an order and no message, or a message about an order that was rolled back.
2What JTA and Two-Phase Commit Add
JTA, Jakarta Transactions, defines a transaction manager that coordinates several resources. Each resource that can take part offers an XA interface (XAResource), named after the X/Open XA standard. The transaction manager runs two-phase commit. In phase one, prepare, it asks every resource to get ready to commit and promise it can; each resource writes its changes durably and votes yes or no. In phase two, if all voted yes, the manager tells every resource to commit; if any voted no, it tells all of them to roll back. The manager logs its decision so that, after a crash, it can finish the job when it restarts.
11 messages in order. Play them, or step through them one at a time.
If either resource votes no, the manager tells both to roll back.
3The Main JTA Interfaces
UserTransaction is the interface for application code: begin(), commit(), rollback() and setRollbackOnly(). In a Jakarta EE server it is available through JNDI at java:comp/UserTransaction or by injection. TransactionManager is the interface for the container and frameworks: in addition to the same operations, it can suspend() and resume() a transaction, which Spring needs for propagation such as REQUIRES_NEW. Transaction represents one transaction and lets resources be enlisted with enlistResource(XAResource). Most application code never calls these directly; it uses @Transactional and lets the framework talk to JTA.
4Using JtaTransactionManager in Spring
Spring's JtaTransactionManager is a PlatformTransactionManager that does not manage transactions itself; it delegates to a JTA implementation. On an application server such as WildFly, WebLogic or Open Liberty, the server provides JTA, and Spring Boot detects it through JNDI and configures JtaTransactionManager automatically. In a standalone Spring Boot application we need an embedded JTA implementation. Narayana, the transaction manager used by WildFly, can be used standalone through its own Spring Boot integration; Atomikos is another. Spring Boot removed its built-in Atomikos and Bitronix starters in recent versions, so check your version and use the provider's own integration.
Notice that the code looks the same as with a local transaction. The difference is all in configuration: both resources must be XA-capable and enlisted with the same JTA manager, and the transaction log must be on durable storage so that recovery works after a crash.
5Why Most Services Avoid XA
Two-phase commit has real costs. Every transaction needs extra round trips for prepare and commit and a log write, which adds latency. Resources hold locks from prepare until the final commit, so a slow participant slows everyone. If the coordinator crashes after prepare, the resources are left in doubt, holding locks until recovery runs, and an operator may have to resolve heuristic outcomes by hand. Many cloud services, such as managed message brokers and NoSQL stores, do not support XA at all, and running a durable transaction log in containers that come and go is awkward.
So most services choose one resource per transaction and design for the gap. With the transactional outbox, the service writes the order and an outbox row in the same local database transaction, and a separate relay publishes the outbox rows to the broker. With idempotent consumers, receivers record processed message ids so that redelivered messages do nothing. Together these give reliable, eventually consistent behaviour without a coordinator.
| Situation | Transaction manager | Why |
|---|---|---|
| One database with JDBC | DataSourceTransactionManager | Local transaction on one connection |
| One database with JPA | JpaTransactionManager | Same, through the EntityManager |
| DB plus broker, strict atomicity | JtaTransactionManager + XA | Two-phase commit across both |
| DB plus broker, typical service | Local tx + outbox + idempotency | Reliable without XA cost |
6Follow-up questions
What is a heuristic outcome?
When a resource has prepared but cannot reach the coordinator for a long time, it may decide on its own to commit or roll back to release its locks. If that decision differs from the coordinator's, the transaction ends with a heuristic outcome, where some resources committed and others rolled back, and someone must fix the data by hand. It is rare but serious.
Can JpaTransactionManager handle two databases?
Not atomically. We can define two JpaTransactionManager beans, one per database, but each runs its own local transaction. Spring's ChainedTransactionManager tried to commit several in order, but it was deprecated because it gives no real atomicity; a failure between commits still leaves partial results.
Does Spring Boot configure JTA automatically?
On an application server, Spring Boot looks for a JTA transaction manager in JNDI and creates JtaTransactionManager. In a standalone application it needs a JTA implementation on the classpath with its integration; check your Spring Boot version, since the built-in starters changed over time.
7Conclusion
In this answer, we saw that local transaction managers such as DataSourceTransactionManager and JpaTransactionManager cover one resource, while JTA coordinates several resources with XA two-phase commit through interfaces like UserTransaction and TransactionManager. Spring's JtaTransactionManager delegates to a JTA implementation from an application server or a standalone one like Narayana, and because XA is costly and complex, most services prefer one resource per transaction with an outbox and idempotent consumers.
Open with
JTA, Jakarta Transactions, is the standard for transactions that span more than one resource, like two databases or a database and a JMS broker. Each resource exposes an XAResource, and the JTA transaction manager runs two-phase commit: first it asks every resource to prepare and vote, then, if all say yes, it tells them all to commit, otherwise to roll back, and it logs its decision so it can recover after a crash.
Then, if they want more
Application code can use UserTransaction to begin and commit, while frameworks use TransactionManager, which can also suspend and resume transactions. In Spring, DataSourceTransactionManager and JpaTransactionManager handle a single database, which is most services. I would only need JtaTransactionManager when one transaction really must include two resources atomically.
It delegates to a JTA implementation, either the application server's, which Boot finds through JNDI, or a standalone one like Narayana, and the DataSource and connection factory must be XA-capable. In practice I avoid XA in most services, because it adds latency, holds locks across resources, can leave in-doubt transactions, and many cloud services do not support it.
Instead I keep one resource per transaction and use the transactional outbox for publishing, with idempotent consumers on the receiving side.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does JTA provide, and when would a Spring application need a JTA transaction...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/08242e27cf/