Quick revision
The short answer
With OSIV on, the request's EntityManager stays open until the response is written, and once it has acquired a JDBC connection it can keep holding it until it closes, depending on Hibernate's connection handling mode. So the slow external call in the controller runs while the request still holds a pooled connection, and connection hold time becomes transaction time plus the external call. Under a traffic spike, every waiting request holds a connection doing nothing, and the pool runs dry.
What to remember
In this answer, we saw that OSIV keeps the EntityManager, and with Spring's default connection handling the JDBC connection, alive until the request ends. A slow external call in the controller therefore extends every connection's hold time from the transaction's 15 ms to the whole request, and Little's law shows how quickly that exhausts a pool of 10. We confirm it by comparing pool usage time with transaction time and seeing idle sessions on the database, and we fix it by disabling OSIV, fetching inside short transactions and calling external services outside any persistence context.
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.
With OSIV on, the request's EntityManager stays open until the response is written, and once it has acquired a JDBC connection it can keep holding it until it closes, depending on Hibernate's connection handling mode. So the slow external call in the controller runs while the request still holds a pooled connection, and connection hold time becomes transaction time plus the external call. Under a traffic spike, every waiting request holds a connection doing nothing, and the pool runs dry.
Why this question is asked
Pool saturation with short transactions looks like a contradiction, and that is why this is a good debugging question. The pool only cares about how long a connection is borrowed, not how long a transaction runs, and OSIV can make those two numbers very different. Interviewers want to see whether we can connect a framework default to a production symptom and prove it with measurements. In this answer, we will recap how connections move between the pool and the EntityManager, walk through one request with OSIV enabled, do the arithmetic that turns a slow API into an exhausted pool, then look at how to confirm it with metrics and how to fix it.
1A Quick Recap of Connections, Pools and the EntityManager
A connection pool such as HikariCP, Spring Boot's default, keeps a fixed number of open database connections, 10 by default. Code borrows one, uses it and gives it back. If all are borrowed, the next caller waits up to connectionTimeout, 30 seconds by default, and then fails with an exception saying the connection is not available.
The EntityManager does not own a connection permanently. Hibernate acquires one from the pool when it first needs to talk to the database and releases it according to its connection handling mode. With Spring's HibernateJpaVendorAdapter, the mode is set to delayed acquisition and hold, which means the connection is taken lazily but then kept until the Session closes (check your version for the exact default). Inside a normal @Transactional method that is fine, because the Session closes when the transaction ends. With OSIV, the Session does not close until the request ends.
2Following One Request Through the System
Let us follow one request. The controller calls a service method that reads a customer in a 15 ms transaction, then calls a partner API that takes 2 seconds during the spike, and then returns the response.
5 messages in order. Play them, or step through them one at a time.
The transaction lasted 15 ms, but the connection was out of the pool for 2,015 ms.
Notice that the code looks innocent. Nothing in it touches the database during the HTTP call. But the EntityManager bound to the request thread still holds the connection it acquired in the service, so from the pool's point of view this request borrowed a connection for about 2,015 ms. A lazy load after the service call would have the same effect even if the transaction itself had released its connection.
3Why Short Transactions Still Saturate the Pool
The number of connections a service needs is roughly the arrival rate times the hold time. This is Little's law, and it makes the problem concrete. Suppose one instance receives 100 requests per second at the spike. Without OSIV, each request holds a connection for 15 ms, so on average 100 x 0.015 = 1.5 connections are busy, and a pool of 10 is plenty.
With OSIV and a 2 second external call, the hold time is about 2.015 seconds, so 100 x 2.015 = about 200 connections would be needed. The pool has 10. After the first 10 requests, every new request waits for a connection in the service call, waiting requests pile up on the server's thread pool, and soon requests fail with connection timeouts while the database itself is almost idle.
So the link is simple once we see it: OSIV makes the connection's lifetime equal to the request's lifetime, and the slow external call stretches the request. The database is not slow and the transactions are not long. The pool is just lending connections to requests that are waiting on something else.
| Setup | Hold time per request | Connections busy at 100 req/s |
|---|---|---|
| OSIV off | 15 ms (transaction only) | about 1.5 |
| OSIV on, fast partner (50 ms) | about 65 ms | about 6.5 |
| OSIV on, slow partner (2 s) | about 2,015 ms | about 200, pool of 10 saturated |
4Proving It With Pool Metrics
Before changing anything, we should confirm the theory. Spring Boot exposes HikariCP metrics through Micrometer: hikaricp.connections.active, hikaricp.connections.pending, hikaricp.connections.usage (how long each connection was borrowed) and hikaricp.connections.acquire. We compare those with transaction-level timing, for example from traces or from Spring's transaction metrics if we record them.
The signature of this problem is clear. Usage time per borrow is about the same as the request latency, not the transaction latency. Active connections sit at the maximum while database CPU and active sessions on the database side, for example in pg_stat_activity, show most connections as idle. And pending threads rise exactly when the partner API slows down.
Idle on the database while active in the pool means the connections are borrowed but not used. That is the evidence that the hold time comes from the application, not from the queries.
5Fixing It
The main fix is spring.jpa.open-in-view=false. The EntityManager then closes when the service transaction ends, and the connection goes back to the pool after 15 ms. Any lazy field the controller touched will now fail with LazyInitializationException, which is the signal to fetch that data inside the service with a fetch join, an entity graph or a DTO projection.
The second fix is structural: make external calls outside any open persistence context and outside any transaction. If the service itself calls the partner API inside @Transactional, turning off OSIV does not help, because the transaction holds the connection during the call. Split the work: read what we need in one short transaction, call the API with no transaction, and if we must write the result, do it in a second short transaction.
Finally, protect the pool against the next slow dependency: put a timeout on the HTTP client (for example 500 ms with a fallback), and do not simply raise the pool size. A pool of 200 would hide the symptom while putting 200 idle connections on the database per instance.
After the change, hikaricp.connections.usage should drop to roughly the transaction time, and active connections should fall well below the maximum even when the partner is slow.
6Follow-up questions
Would increasing the pool size fix it?
It would delay the failure, not fix it. The required number of connections grows with the external API's latency, which we do not control. Each connection also costs memory and a backend process on the database, so 20 instances with 200 connections each could overwhelm PostgreSQL long before the application improves.
Can we keep OSIV but release the connection after each transaction?
Hibernate has a connection handling mode that releases the connection after each transaction, and a Spring Boot property can set it through spring.jpa.properties.hibernate.connection.handling_mode. That reduces the hold time, but lazy loads in the web layer would still acquire connections and run outside transactions, and N+1 would stay hidden. Turning OSIV off is the cleaner fix; check your version before relying on the handling mode.
7Conclusion
In this answer, we saw that OSIV keeps the EntityManager, and with Spring's default connection handling the JDBC connection, alive until the request ends. A slow external call in the controller therefore extends every connection's hold time from the transaction's 15 ms to the whole request, and Little's law shows how quickly that exhausts a pool of 10. We confirm it by comparing pool usage time with transaction time and seeing idle sessions on the database, and we fix it by disabling OSIV, fetching inside short transactions and calling external services outside any persistence context.
Open with
The key is that the pool cares about how long a connection is borrowed, not how long the transaction runs. With open session in view on, the EntityManager is bound to the request and closes only when the response is written.
Then, if they want more
Spring configures Hibernate to acquire the connection lazily and hold it until the session closes, so once the service has used the database, the connection stays attached to the request. The controller then calls a slow external API, and during that call the request still holds a pooled connection doing nothing. Hold time becomes the transaction plus the external call.
At 100 requests a second with a two second call, you need around 200 connections, and Hikari has 10, so requests queue and time out while the database is idle. I would prove it with Hikari metrics: connection usage time close to request latency, active at the maximum, pending rising, while transaction spans are short and pg_stat_activity shows idle sessions.
The fix is to set open-in-view to false, fetch everything the response needs inside the service transaction, and make external calls outside any transaction or persistence context, with a client timeout. I would not just raise the pool size.
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 service's connection pool saturates during traffic spikes, although transactions are...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/d31dffacee/