Quick revision
The short answer
I would introduce a small set of written standards: no remote calls inside transactions, batch work in chunks, Open Session in View disabled, and timeouts on transactional use cases. I would back them with database-side limits per service user (statement, lock and idle-in-transaction timeouts), monitoring of transaction and connection hold times with alerts, and automated checks such as ArchUnit rules that flag HTTP clients called from @Transactional methods. Each new incident is reviewed and feeds back into the standards.
What to remember
In this answer, we saw that remote calls inside transactions, giant batch transactions and OSIV all make transactions outlive their database work, and that they recur because they look harmless in review. The response is layered: short standards with reasons and examples, database timeouts per service user, monitoring of connection hold times and long transactions, automated ArchUnit and configuration checks, and an incident review that updates every layer.
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.
I would introduce a small set of written standards: no remote calls inside transactions, batch work in chunks, Open Session in View disabled, and timeouts on transactional use cases. I would back them with database-side limits per service user (statement, lock and idle-in-transaction timeouts), monitoring of transaction and connection hold times with alerts, and automated checks such as ArchUnit rules that flag HTTP clients called from @Transactional methods. Each new incident is reviewed and feeds back into the standards.
Background to this question
When the same kind of incident happens three times, the problem is no longer one bad method; it is that nothing stops the next one. A staff engineer is expected to turn incident lessons into standards that people follow without needing to remember them. In this answer, we will recap why each pattern causes incidents, then build the response in layers: standards, database backstops, monitoring, automated checks, and the feedback loop that keeps them current.
1Why These Three Patterns Keep Causing Incidents
All three have the same root: a transaction, and the pooled connection behind it, lives far longer than the database work needs. A remote call inside a transaction holds the connection while the network is slow, so a slow dependency exhausts the pool and takes the service down. A giant batch transaction holds row locks for minutes and blocks user traffic. Open Session in View, OSIV, keeps the persistence context and often the connection for the whole web request, including view rendering and serialisation, so each request holds a connection far longer than its queries take.
They keep recurring because each one looks harmless in code review. An HTTP call inside a service method looks normal, and OSIV is on by default in Spring Boot, which only logs a warning at startup. So standards alone are not enough; we need layers that catch what reviews miss.
- Written standardsWhat good looks like, with examples
- Automated checks in the buildArchUnit rules, configuration tests
- Application limitsTransaction timeouts, OSIV off, pool sizes
- Database limits per service userstatement, lock and idle-in-transaction timeouts
- Monitoring and alertsConnection hold time, long-running transactions
- Incident reviewUpdates every layer above
6 layers, from the top down. Step through them one at a time.
Each layer catches what the one above it missed.
2Layer 1: The Standards
The standards should be short enough to remember and each one should come with the reason and an example from our own incidents. No remote calls, HTTP, messaging or file uploads, inside a transaction; call first, then open a short transaction, or use after-commit hooks and an outbox. Batch jobs commit in chunks with restart support. spring.jpa.open-in-view=false in every service, with the data a view needs fetched explicitly. Every transactional use case has a timeout, with a sensible service default through spring.transaction.default-timeout. A short internal page with before and after code for each rule does more than a long policy document.
3Layer 2: Database Backstops
The database is the last place where a long transaction can be stopped, whatever the code does. In PostgreSQL we set limits on each service's own database role, so they apply to every connection the service opens and can differ between an API service and a reporting job.
These turn a slow, spreading incident into a fast, local error. A stuck transaction is cut off after 30 seconds and the service logs an exception, instead of holding locks for an hour. They are also cheap to roll out, which makes them a good first step while code changes are still in progress.
4Layer 3: Monitoring and Alerts
We need to see long transactions before users do. HikariCP exposes, through Micrometer, a histogram of how long connections are held, hikaricp.connections.usage, and how long threads wait for one, hikaricp.connections.acquire. A rising p99 usage time is often the first sign of a remote call inside a transaction. On the database side, a regular query on pg_stat_activity for transactions older than a minute, grouped by application_name, feeds an alert. Each service's dashboard shows both, so the owning team sees its own numbers.
5Layer 4: Automated Checks
Rules that a machine checks do not depend on reviewers remembering them. ArchUnit, a library that tests the structure of compiled code in ordinary unit tests, can flag a @Transactional method that calls an HTTP client directly.
Notice the limit: this checks direct calls only, not calls made several layers down, so it catches the common case rather than every case. A second check can fail the build if any service sets spring.jpa.open-in-view to true. These rules live in a shared test library so every service gets them with one dependency.
6Layer 5: Learning From Each Incident
Each incident caused by a long transaction gets a short review that asks which layer should have caught it and why it did not. The answer updates a layer: a new ArchUnit rule, a lower timeout, a new alert, or a new example in the standards. Publishing these reviews across teams spreads the lesson, and tracking the count of such incidents per quarter shows whether the layers work.
7Follow-up questions
How would you roll this out without stopping teams?
Start with the cheap, protective layers: database timeouts and dashboards. Introduce automated checks in warning mode, fix existing violations service by service, and then make the checks fail the build. Teams see the problem in their own data before the rules become strict.
What if a team really needs a long transaction?
There should be a documented exception: a separate database role with higher limits, and a written reason. Most long transactions turn out to be avoidable once someone looks, but the exception path keeps the standard credible.
Why not just increase the connection pool?
A larger pool delays the incident rather than preventing it. A slow dependency will eventually hold every connection however many there are, and a larger pool also puts more load on the database at the worst moment.
8Conclusion
In this answer, we saw that remote calls inside transactions, giant batch transactions and OSIV all make transactions outlive their database work, and that they recur because they look harmless in review. The response is layered: short standards with reasons and examples, database timeouts per service user, monitoring of connection hold times and long transactions, automated ArchUnit and configuration checks, and an incident review that updates every layer.
Open with
These incidents share one cause: transactions that live far longer than their database work. So I would respond in layers rather than with one rule. First, short written standards with examples from our own incidents: no remote calls inside transactions, batches committed in chunks, Open Session in View disabled, and timeouts on transactional use cases.
Then, if they want more
Second, database backstops, because they work whatever the code does: per-service roles in PostgreSQL with statement_timeout, lock_timeout and idle_in_transaction_session_timeout, and transaction_timeout on PostgreSQL 17, with looser limits for batch roles. Third, monitoring: Hikari's connection usage and acquire time histograms from Micrometer, plus an alert on transactions older than a minute in pg_stat_activity, per application name.
Fourth, automated checks in the build: an ArchUnit rule that fails when a @Transactional method calls RestClient or WebClient directly, and a test that open-in-view is false, shipped in a shared test library. I would roll out the database limits and dashboards first, then the checks in warning mode, then make them strict.
Finally, every incident of this kind gets a review asking which layer should have caught it, and that layer is updated.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 17
- 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 "Several incidents trace back to long transactions: external calls inside transactions...": https://scrivio-interview-library.netlify.app/databases/transactions/93dc9428a5/