Quick revision
The short answer
In two-phase commit a coordinator first asks every participant to prepare and vote, and only if all vote yes does it tell them all to commit; otherwise all abort. Participants hold their locks from prepare to the final decision, so a coordinator failure can leave them blocked in doubt. @Transactional, by contrast, only manages a local resource transaction such as one JDBC connection; an HTTP call is not a transactional resource, so it is never rolled back, and atomicity across services needs patterns such as an outbox plus a saga.
What to remember
In this answer, we saw that @Transactional manages a local transaction on one connection, so an HTTP call inside it is neither part of the commit nor rolled back. Two-phase commit can make several XA resources atomic through prepare and commit phases, but participants block with locks held if the coordinator fails, and HTTP services cannot take part. If the remote call succeeds and the local commit fails, the remote change stays, so across services we use an outbox to publish reliably and a saga with compensations to reach eventual atomicity.
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.
In two-phase commit a coordinator first asks every participant to prepare and vote, and only if all vote yes does it tell them all to commit; otherwise all abort. Participants hold their locks from prepare to the final decision, so a coordinator failure can leave them blocked in doubt. @Transactional, by contrast, only manages a local resource transaction such as one JDBC connection; an HTTP call is not a transactional resource, so it is never rolled back, and atomicity across services needs patterns such as an outbox plus a saga.
Why this question is asked
Almost every developer who moves from a monolith to microservices writes this code once: a @Transactional method that saves to the database and calls another service over HTTP, expecting both to succeed or fail together. Interviewers ask about it because it reveals whether we know what a transaction actually covers, and whether we understand why distributed atomicity is hard. In this answer, we will recap what @Transactional manages, walk through two-phase commit and its weak spot, show why the HTTP call escapes the transaction with a concrete failure, and then look at what we use instead.
1A Quick Recap of What @Transactional Covers
In Spring, @Transactional tells a transaction manager to begin a transaction before the method runs and to commit or roll back when it returns or throws. With JPA or JDBC, the transaction manager binds one database connection to the current thread, calls setAutoCommit(false), and at the end calls commit() or rollback() on that connection. That is the whole mechanism.
So the transaction covers exactly what goes through that connection: inserts, updates and deletes in this one database. A resource that can join a transaction this way is called a transactional resource. An HTTP client, a file write, an email sender or a call to a payment API is not one. Spring has no way to undo them, and does not try.
2How Two-Phase Commit Works
Two-phase commit (2PC) is the classic protocol for making several transactional resources commit atomically. One component, the coordinator or transaction manager, drives it. Each resource taking part, for example two databases, is a participant.
In phase one, the coordinator sends prepare to every participant. Each one does all the work needed to guarantee it can commit, writes that to its durable log, keeps its locks, and votes yes, or votes no if it cannot. In phase two, if every vote was yes, the coordinator records the decision and sends commit to all; if any vote was no or a participant did not answer, it sends abort to all. Because no participant commits until everyone has promised it can, the outcome is all or nothing.
9 messages in order. Play them, or step through them one at a time.
Between the vote and the commit message, both databases hold their locks and cannot decide alone.
3The Weak Spot: Blocked in Doubt
Notice the window after a participant votes yes. It has promised to commit if told to, so it may not abort on its own, and it may not commit on its own either, because another participant might have voted no. It must wait for the coordinator, holding its row locks the whole time. If the coordinator crashes in that window, the participant is in doubt: it stays blocked, with locks held, until the coordinator recovers and reads its log.
This is why 2PC is called a blocking protocol. It also adds network round trips to every commit and requires every participant to support the XA interface, the standard for 2PC with databases and message brokers. Inside one data centre with a reliable transaction manager this can be acceptable, but across independently deployed services it rarely is, and a REST API cannot take part at all.
4Why the HTTP Call Escapes the Transaction
Now the code. The order service saves an order and asks the payment service to charge the card, all inside one @Transactional method.
Spring rolls back the database work when reserve() throws, but the payment service committed its own transaction the moment it returned 200. Nothing tells it to undo. The same happens if every line succeeds but the local commit itself fails, for example because of a constraint violation detected at flush time or a lost database connection: the remote side has already applied the change, and our side has not.
There is a second, subtler problem. The HTTP call happens while our transaction is open, so our database locks and connection are held for the whole network round trip. If the payment service takes 5 seconds, every row we touched stays locked for 5 seconds.
5What We Use Instead
Atomicity across services is built from local transactions plus messaging. With a transactional outbox, the order service saves the order and an event row in one local transaction, and a relay publishes the event afterwards, so the event exists if and only if the order committed. The payment service reacts to that event in its own local transaction.
When several services each make a change, a saga links those local transactions: each step commits on its own, and if a later step fails, earlier steps are undone by compensating actions, such as refunding the charge. This gives eventual atomicity: the system may be briefly in between, but it always ends up either fully done or fully compensated. Combined with idempotent handlers, so that retries are safe, this is the standard replacement for distributed transactions between services.
6Follow-up questions
What can go wrong if the HTTP call is placed after the commit instead?
Then the opposite failure appears. The order commits, and if the process crashes or the HTTP call fails before the charge happens, we have an order with no payment and nothing that will retry it. Moving the call after the commit only works with something durable that remembers it must still be made, which is exactly what an outbox row gives us: the intent is committed with the order, and a relay keeps trying until the payment service has accepted it.
Could we use JTA with XA to include the remote service?
Only if the remote resource supports XA, which databases and some brokers do and HTTP services do not. Even where it works, XA brings the blocking and latency costs we saw, and it couples the services' availability. Between microservices it is almost never the right tool.
Does 2PC guarantee isolation as well as atomicity?
Yes, within the participants, because each one holds its locks until the global decision. That is a real advantage over sagas, which commit each step on its own and therefore let other transactions see intermediate states.
7Conclusion
In this answer, we saw that @Transactional manages a local transaction on one connection, so an HTTP call inside it is neither part of the commit nor rolled back. Two-phase commit can make several XA resources atomic through prepare and commit phases, but participants block with locks held if the coordinator fails, and HTTP services cannot take part. If the remote call succeeds and the local commit fails, the remote change stays, so across services we use an outbox to publish reliably and a saga with compensations to reach eventual atomicity.
Open with
Two-phase commit has a coordinator and several participants. In the first phase the coordinator sends prepare, each participant makes its work durable, keeps its locks and votes yes or no.
Then, if they want more
In the second phase, if all voted yes, the coordinator logs the decision and tells everyone to commit; otherwise everyone aborts. The weak spot is that after voting yes a participant cannot decide alone, so if the coordinator crashes it stays in doubt with its locks held until recovery. Now, @Transactional in Spring is much smaller than that.
It manages a local transaction on one resource, typically the JDBC connection bound to the thread, and commits or rolls back that connection. An HTTP call is not a transactional resource. If I call the payment service inside the method and it returns 200, the payment service has already committed.
If my method then throws or my commit fails, Spring rolls back my database but the charge stays. It also holds my locks and connection during the network call. So for atomicity across services I would not use the annotation at all.
I would write the change and an event in one local transaction using an outbox, publish it reliably, and coordinate the steps as a saga, with idempotent handlers and compensating actions such as a refund if a later step fails.
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
- Spring Cloud documentation, Resilience4j documentation
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 "Explain how two-phase commit works and why a Spring Boot microservice that calls another...": https://scrivio-interview-library.netlify.app/spring-boot/distributed-data/9fbbbb81ca/