Quick revision
The short answer
Spring Boot turns on Open Session in View by default and logs a warning about it, which keeps the Hibernate persistence context open until the response is written. So when Jackson serialises an entity, each lazy association it touches runs another query, which gives hundreds of N+1 queries, and the database connection stays checked out for the whole request. The fix is spring.jpa.open-in-view=false, loading what the endpoint needs in the service with a fetch join, an entity graph or a DTO projection, and returning DTOs so serialisation can never trigger lazy loading.
What to remember
In this answer, we saw that the startup warning is Spring Boot telling us Open Session in View is on, which keeps the persistence context and its connection open until the response is written. Lazy associations then load during JSON serialisation, one query per item, and the connection is held for the whole request. We fixed it by setting spring.jpa.open-in-view=false, loading exactly what the endpoint needs in the service with a fetch join, an entity graph or a DTO projection, and returning DTOs so serialisation can never reach the database.
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.
Spring Boot turns on Open Session in View by default and logs a warning about it, which keeps the Hibernate persistence context open until the response is written. So when Jackson serialises an entity, each lazy association it touches runs another query, which gives hundreds of N+1 queries, and the database connection stays checked out for the whole request. The fix is spring.jpa.open-in-view=false, loading what the endpoint needs in the service with a fetch join, an entity graph or a DTO projection, and returning DTOs so serialisation can never trigger lazy loading.
Why this question is asked
This is a problem that looks fine in development, where the database is local and the data is small, and becomes a production incident when one endpoint starts exhausting the connection pool. The warning was in the startup log the whole time. Interviewers ask about it because it tests several things at once: how Hibernate's lazy loading works, how Spring Boot binds a session to a web request, and why returning entities from controllers is a design problem, not just a style choice. In this answer, we will recap lazy loading and the persistence context, see what Open Session in View does, reproduce the hundreds of queries, explain why connections are held, and then fix it properly.
1A Quick Recap of Lazy Loading and the Persistence Context
The persistence context is Hibernate's working memory for one unit of work: the set of entities it has loaded, which it tracks for changes. It normally lives as long as a transaction. As long as it is open, Hibernate can still go to the database on our behalf.
A lazy association is a relationship that Hibernate does not load until we first touch it. In JPA, @OneToMany and @ManyToMany are lazy by default. Instead of the real list, the entity holds a proxy collection, a placeholder that runs a query the first time we call a method on it. If the persistence context is already closed at that moment, there is nobody to run the query, and Hibernate throws LazyInitializationException.
2What Open Session in View Does
Open Session in View, or OSIV, is a pattern that keeps the persistence context open for the whole web request, not just the transaction. Spring Boot enables it by registering an OpenEntityManagerInViewInterceptor whenever spring.jpa.open-in-view is not set, and because the default can surprise people, Boot logs a warning at startup saying that database queries may be performed during view rendering and suggesting that we set the property explicitly.
With OSIV on, LazyInitializationException mostly disappears, which is why the default exists: it makes beginner code work. The price is that lazy loading can now happen anywhere in the request, including after the controller returns, while Jackson is turning our objects into JSON.
The exact wording may vary slightly by version, but the warning is the clue that the session outlives the service method.
3Reproducing the Hundreds of Queries
Here is an endpoint that returns a list of entities. Each Order has a lazy customer and a lazy list of lines, and each line has a lazy product.
9 messages in order. Play them, or step through them one at a time.
The connection is held while JSON is written, and lazy loading runs outside any transaction.
Notice that the service method ran one query and returned. Everything else happened after the controller returned, during serialisation, because the persistence context was still open thanks to OSIV. This is the N+1 problem: one query for the list, and then one more query for each item's association. It is invisible in the service code, which is why it survives code review.
4Why Connections Are Held So Long
Once the persistence context has taken a connection from the pool, OSIV keeps it until the request finishes. That includes the time spent serialising, the time spent running every lazy query, and even the time spent writing a large response to a slow client. If the request also calls a remote service after the transaction, the connection sits idle during that call too.
With a pool of, say, 10 connections in HikariCP, a few slow requests to this endpoint are enough to make every other endpoint wait for a connection, and eventually time out with a connection-not-available error. So the symptom in the question, connections held for long periods, is not a separate bug; it is the same OSIV behaviour seen from the pool's side.
5The Fix: Turn OSIV Off and Load Data in the Service
First we disable OSIV explicitly. This makes lazy loading outside a transaction fail fast with LazyInitializationException, which is exactly what we want: it shows us every place that relied on the hidden session.
Then the service maps the loaded data to DTOs inside the transaction, and the controller returns those DTOs. A DTO is a plain record with plain fields, so Jackson has nothing lazy to touch and cannot trigger queries. If the list endpoint does not need lines and products at all, option C is usually the best: it selects four columns instead of whole entity trees.
There is a catch with fetch joins: fetching two collections at once, or paging while fetching a collection, can multiply rows or make Hibernate page in memory. For pages of data, a common pattern is to page the ids first and then fetch the details for those ids, or to use a projection.
6Follow-up questions
Why does Spring Boot keep OSIV on by default if it is harmful?
Because it makes simple applications, especially ones rendering server-side templates, work without LazyInitializationException, and changing the default would break many existing applications. Boot chose to keep it and warn, so that teams make a deliberate choice. For REST APIs, most experienced teams set it to false.
How do you find these N+1 queries before production?
Turn on SQL logging or Hibernate statistics in tests and assert the number of statements for important endpoints. A test that calls the endpoint and checks that it runs one or two queries catches regressions immediately. In production, tracing tools that record JDBC spans show hundreds of spans under one request.
Is spring.jpa.properties.hibernate.default_batch_fetch_size a fix?
It reduces N+1 to roughly N divided by the batch size, by loading lazy associations for several parents in one IN query. It is a useful safety net, but it still lets serialisation decide what to load and still holds the connection with OSIV on. We prefer to decide the data shape in the service and use batch fetching as a backstop.
7Conclusion
In this answer, we saw that the startup warning is Spring Boot telling us Open Session in View is on, which keeps the persistence context and its connection open until the response is written. Lazy associations then load during JSON serialisation, one query per item, and the connection is held for the whole request. We fixed it by setting spring.jpa.open-in-view=false, loading exactly what the endpoint needs in the service with a fetch join, an entity graph or a DTO projection, and returning DTOs so serialisation can never reach the database.
Open with
The warning means Open Session in View is on, which Spring Boot enables by default for web applications. It keeps the Hibernate persistence context open until the response is fully written.
Then, if they want more
So this endpoint is probably returning entities: the service runs one query and returns, and then Jackson, while serialising, touches each lazy association, the customer, the lines, each line's product, and every touch runs another query. That is the N+1 problem, and it explains the hundreds of queries.
Because the persistence context keeps its connection until the end of the request, the connection is held during serialisation and while the response is written, which is why the pool runs dry. My fix has three parts. First, set spring.jpa.open-in-view to false, so any lazy access outside a transaction fails fast and shows me where the code relied on it.
Second, load exactly what the endpoint needs inside the service transaction, with a join fetch, an entity graph, or best of all a DTO projection that selects only the needed columns. Third, return DTOs from the controller, not entities, so serialisation has nothing lazy to touch.
Then I would add a test that asserts the number of SQL statements for this endpoint so the problem cannot quietly come back.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 Spring Boot application logs a warning about spring.jpa.open-in-view at startup. In...": https://scrivio-interview-library.netlify.app/java/application-architecture/9936384342/