Quick revision
The short answer
Calling a slow payment API inside a @Transactional method keeps a database connection and its row locks for up to 10 seconds per order, which drains the pool under load, and it ties two systems together that cannot commit together: the payment can succeed while the database commit fails, or the call can time out and leave us not knowing whether the customer paid. We restructure it into a short transaction that saves the order as PENDING, a payment call outside any transaction with an idempotency key, and a second short transaction that records the result, made reliable with an outbox or a saga.
What to remember
In this answer, we saw that a 10-second payment call inside a transaction holds a pooled connection and row locks for its whole duration, which exhausts the pool and blocks other orders, and that a database and a payment provider cannot commit together, so failures leave charges without orders or unknown states. We split the work into a short PENDING transaction, a payment call outside any transaction with an idempotency key, and a second short transaction for the result, with an outbox, a saga and a reconciliation job to make it reliable.
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.
Calling a slow payment API inside a @Transactional method keeps a database connection and its row locks for up to 10 seconds per order, which drains the pool under load, and it ties two systems together that cannot commit together: the payment can succeed while the database commit fails, or the call can time out and leave us not knowing whether the customer paid. We restructure it into a short transaction that saves the order as PENDING, a payment call outside any transaction with an idempotency key, and a second short transaction that records the result, made reliable with an outbox or a saga.
Why this question is asked
Putting an HTTP call inside a transaction is one of the easiest mistakes to make in Spring, because the annotation sits on the whole method and the code reads naturally from top to bottom. It works perfectly in testing and then fails in production under load or during a payment provider outage. Interviewers ask this scenario because it tests two things at once: resource management (connections, locks, pools) and consistency between systems that do not share a transaction. In this answer, we will look at the original code, see what it costs in connections and locks, see the consistency problems, and then restructure it step by step with idempotency keys and an outbox.
1The Original Code
Here is the method as the team wrote it. The order is saved, the payment provider is called, and the order is updated with the result, all inside one transaction.
Notice that the connection was taken from the pool at the start of the method and is only returned at the commit. Everything between, including the 10-second wait for the payment provider, happens while we hold it.
2What Holding the Transaction Costs
A connection pool, such as HikariCP in Spring Boot, has a fixed number of connections, 10 by default. If each order holds one for 10 seconds, the pool can serve one order per second per connection, 10 per second in total. At 50 orders per second, 40 requests are waiting for a connection, and after HikariCP's connectionTimeout of 30 seconds they fail with SQLTransientConnectionException. Requests that have nothing to do with payments fail too, because they share the pool.
The locks are worse. The UPDATE on the stock rows locks them until commit. Every other order for the same popular product waits behind ours for those 10 seconds. If the payment provider slows down to 30 seconds during an incident, our database is effectively locked for that product, even though the database itself is healthy.
3Two Systems That Cannot Commit Together
The deeper problem is consistency. The payment provider and our database are two separate systems, and there is no single commit that covers both. Consider the failure orders. If the charge succeeds and then our commit fails (a constraint violation at flush, a deadlock, a lost connection), the customer is charged and we have no order. If the HTTP call times out after 10 seconds, we do not know whether the provider charged the card or not; rolling back our order may throw away the record of a payment that actually happened.
Retrying makes it worse without care: a client that retries the whole request after a timeout may charge the card twice.
| Payment | Our commit | Result |
|---|---|---|
| Succeeds | Succeeds | Correct |
| Fails | Rolls back | Correct: no order, no charge |
| Succeeds | Fails | Customer charged, no order exists |
| Times out (unknown) | Rolls back | Maybe charged, no record of it |
| Succeeds, client retries | Succeeds twice | Customer charged twice |
4Restructuring: Short Transactions Around the Call
The fix is to make each database step short and to keep the HTTP call outside any transaction. We split the use case into three steps: save the order as PENDING and commit, call the payment provider with no transaction open, then record the result in a second short transaction. The orchestrating method itself is not transactional; the two database steps live in another bean so their calls go through its proxy.
6 messages in order. Play them, or step through them one at a time.
No transaction is open while we wait for the payment provider.
Now each connection is held for a few milliseconds, so the same pool of 10 serves hundreds of orders per second, and no stock row stays locked while we wait on the network. The PENDING status is honest: it says the payment outcome is not known yet, which is exactly the truth during the call.
5Making the Payment Call Safe to Retry
Splitting the transaction creates a new question: what if we crash between the charge and recordResult(), or the call times out? The answer is to make the charge idempotent, meaning that sending it twice has the same effect as sending it once. Most payment providers accept an idempotency key, a unique value we send with the request; if they see the same key again, they return the original result instead of charging again.
We generate the key when we create the order, for example a UUID stored on the order row, so it survives restarts. A reconciliation job then looks for orders that have been PENDING for more than a minute, calls the provider again with the same key (or asks for the payment's status), and records the real outcome. A timeout is no longer a mystery; it is a delay.
6Making It Reliable With an Outbox or a Saga
For stronger guarantees, we can avoid calling the provider from the request thread at all. In the outbox pattern, the first transaction inserts the order and, in the same transaction, a row in an outbox table that says 'charge order 42'. Because both inserts commit together, we can never have an order without a payment request or the reverse. A separate worker reads the outbox, calls the provider with the idempotency key, and updates the order.
When checkout involves several services (inventory, payment, shipping), this becomes a saga: a sequence of local transactions, each with a compensating action that undoes it, such as releasing reserved stock when the payment is declined. Each step is idempotent, so the worker can retry any step after a crash without doing it twice.
7Follow-up questions
Why not just add a timeout to the transaction?
A timeout limits the damage but does not fix it. With @Transactional(timeout = 3), a slow payment call still holds the connection for 3 seconds, and when the timeout fires we still do not know whether the provider charged the card. The call must be outside the transaction.
Could we use a distributed transaction with the payment provider?
No. Payment providers expose HTTP APIs, not XA resources, and even where two-phase commit is possible it holds locks across systems and blocks when one side is unavailable. Idempotency keys and reconciliation are the standard approach.
What should the user see while the order is PENDING?
A clear message that the payment is being confirmed, and a final result shortly after, either by polling or by a notification. Most payments finish in under a second, so the PENDING state is usually invisible; it matters for the rare slow or timed-out case.
8Conclusion
In this answer, we saw that a 10-second payment call inside a transaction holds a pooled connection and row locks for its whole duration, which exhausts the pool and blocks other orders, and that a database and a payment provider cannot commit together, so failures leave charges without orders or unknown states. We split the work into a short PENDING transaction, a payment call outside any transaction with an idempotency key, and a second short transaction for the result, with an outbox, a saga and a reconciliation job to make it reliable.
Open with
The main problem is that the database transaction stays open for the whole payment call. That means a pooled connection is held for up to 10 seconds per order, so with a pool of 10 we can only do about 10 orders per second, and everything else, including unrelated requests, waits and eventually times out.
Then, if they want more
Any rows we updated, like stock, stay locked for that time too. The second problem is consistency: the payment provider and our database can't commit together. If the charge succeeds and our commit fails, the customer pays for an order we don't have. If the call times out, we don't know whether they were charged.
And a retry can charge them twice. So I'd restructure it. First, a short transaction saves the order as PENDING and commits. Then I call the provider with no transaction open, passing an idempotency key stored on the order, so a retry returns the original result instead of charging again. Then a second short transaction records PAID or FAILED.
For orders that stay PENDING because of a timeout or a crash, a reconciliation job asks the provider with the same key. If I want it fully reliable, I'd write an outbox row in the first transaction and let a worker make the call, and for multi-service checkout I'd use a saga with compensating steps.
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 Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "A @Transactional service method saves an order and then calls an external payment API...": https://scrivio-interview-library.netlify.app/databases/transactions/e388ad92bb/