Quick revision
The short answer
We treat it as a migration with evidence, not a configuration flip: inventory the 60 services, run each test suite with OSIV disabled to find the LazyInitializationExceptions, and fix them with explicit fetch plans or DTOs. Then we roll out service by service behind a configuration flag, starting with the least critical ones and watching error rates, change the default in the shared starter so new services start with it off, and explain the reason with data from our own services.
What to remember
In this answer, we planned the removal of open session in view as a migration. We inventoried the services to find the ones that matter, ran their test suites with OSIV off to turn hidden lazy loads into a concrete list of failures, and fixed them with fetch plans or DTOs rather than eager fetching. We rolled out per service behind configuration from the least critical upward while watching errors and pool metrics, changed the shared starter's default for new services, and won support with numbers from our own systems.
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.
We treat it as a migration with evidence, not a configuration flip: inventory the 60 services, run each test suite with OSIV disabled to find the LazyInitializationExceptions, and fix them with explicit fetch plans or DTOs. Then we roll out service by service behind a configuration flag, starting with the least critical ones and watching error rates, change the default in the shared starter so new services start with it off, and explain the reason with data from our own services.
Why this question is asked
Changing one property sounds trivial, but in a service where OSIV has always been on, any controller, template or JSON serialiser may be quietly loading lazy data. Switching it off across 60 services at once could turn a hidden performance cost into hundreds of runtime errors on the same morning. Interviewers ask this to see how a senior engineer plans a wide, low-glamour change: how we find risk before production does, how we sequence the work, and how we get teams to care. In this answer, we will recap what breaks when OSIV goes off, then walk through the inventory, the fixing, the rollout, the shared default and the communication, with the reasoning behind each step.
1A Quick Recap of What Breaks
Open session in view keeps the JPA EntityManager open for the whole web request. With it on, a lazy association, one Hibernate loads only when first touched, can be loaded in a controller or while Jackson turns an entity into JSON. With it off, the EntityManager closes when the service transaction ends, and touching that association afterwards throws LazyInitializationException.
So the risk is not that something becomes slower. It is that code paths which worked yesterday now fail, and only on the paths that touch lazy data outside the service. Those paths are often the rare ones: an admin page, an export, an error response that serialises an entity. That shapes the whole plan: we need to find those paths before users do.
2Building the Inventory and Running Tests With OSIV Off
The first step is an inventory of all 60 services: which ones use JPA at all, which are servlet web applications (OSIV only applies there), whether the property is already set, how much traffic each takes and how critical it is. A small script over the repositories and the configuration server can answer most of this in a day. Typically a third of services turn out not to be affected, which already shrinks the problem.
For the rest, each team runs its test suite with spring.jpa.open-in-view=false. Every LazyInitializationException in the results is a concrete item on a list. Where test coverage of controllers is thin, we add a small check: a test that calls each endpoint with realistic data and serialises the response, because serialisation is where lazy loads usually hide.
The stack trace names the entity, the association and the line that touched it, so each failure is a small, well-defined task. We collect the counts per service; that number becomes the effort estimate and the order of work.
3Fixing Each Failure Properly
Each failure is fixed in the service layer by fetching what the use case needs. For an entity that the response needs with its children, we add a fetch join or an @EntityGraph to the repository method. For read-only screens and API responses, a DTO projection is usually better: the service returns exactly the fields the response needs, and the entity never leaves the transaction.
What we avoid is the tempting shortcut of switching associations to eager fetching. That makes the exception disappear but loads the association in every query that touches the entity, so we would trade one hidden cost for another. We also avoid Hibernate.initialize() calls scattered through controllers, which keep the dependency on an open session.
4Rolling Out Service by Service
We never flip all 60 at once. Each service gets the change as its own release, with the property controlled by configuration so we can switch back without a deploy. The order follows risk: internal tools and low-traffic services first, then medium ones, and the payment or checkout paths last, once the playbook has been proven on 40 others.
For each service, we watch error rates, specifically any LazyInitializationException in logs, and latency for a few days. We also watch the benefit: pool usage time and active connections usually fall, which gives us numbers to share. If errors appear, we switch the flag back, fix the missing fetch and try again. Because every step is reversible, a missed path costs minutes, not an incident.
- Run tests with OSIV offlist every
LazyInitializationException - Fix with fetch plans or DTOsin the service layer, never with EAGER
- Deploy with flag still on
spring.jpa.open-in-view=true, set explicitly - Switch flag off in productionvia configuration, no redeploy
- Errors or latency rise?YesSwitch back, fix, retryNoKeep it off, record the pool numbers
5 steps in order. Play them, or step through them one at a time.
Each step is reversible, so a missed lazy path costs minutes.
5Changing the Default and Telling People Why
Most organisations with 60 services have a shared starter or parent configuration. Once the first wave is done, we set open-in-view to false there, so new services start with it off. Existing services that have not migrated yet set it to true explicitly in their own configuration, which also silences Boot's warning and makes the remaining work visible: a search for open-in-view=true is the list of unfinished services.
The communication matters as much as the code. Teams will ask why they should spend time on something that works. The answer should come from our own data, not a blog post: for example, the service where pool usage time fell from 900 ms to 40 ms after the change, the incident last quarter where a slow dependency exhausted the pool, and the query counts per request before and after in a service that had hidden N+1 loads. A short guide with the three fix patterns and a test recipe lowers the cost for each team to about a day.
| Wave | Services | Goal |
|---|---|---|
| 0 | 2 volunteer teams | Prove the test recipe and fix patterns |
| 1 | 20 internal and low-traffic services | Build confidence, collect pool numbers |
| 2 | 25 medium services | Bulk of the work, starter default changed |
| 3 | 13 critical services | Only after the playbook is proven |
6Follow-up questions
What if a team has no time to fix their service?
They set the property to true explicitly and the service stays on the list. That is acceptable for a while, because nothing gets worse. We schedule it with their next piece of work in that area and track the remaining count in the engineering review, so the decision to stay on is visible rather than accidental.
How do you know the change was worth it?
We measure before and after per service: Hikari connection usage time, active connections at peak, queries per request and p99 latency. Across the fleet, a falling number of pool timeout incidents is the clearest signal for leadership.
Could an automated tool find the problems instead of tests?
Static analysis can find some patterns, such as entities returned from controllers, but it cannot see every lazy path through serialisation and templates. Tests with OSIV off are more reliable, and production error monitoring with a quick switch back covers whatever tests miss.
7Conclusion
In this answer, we planned the removal of open session in view as a migration. We inventoried the services to find the ones that matter, ran their test suites with OSIV off to turn hidden lazy loads into a concrete list of failures, and fixed them with fetch plans or DTOs rather than eager fetching. We rolled out per service behind configuration from the least critical upward while watching errors and pool metrics, changed the shared starter's default for new services, and won support with numbers from our own systems.
Open with
I would treat it as a migration, not a config flip, because with OSIV on, any controller or serialiser might be quietly loading lazy data, and turning it off everywhere at once could produce LazyInitializationExceptions across 60 services on the same day.
Then, if they want more
First I would inventory: which services use JPA in a servlet app, which already set the property, traffic and criticality. Then each team runs its test suite with open-in-view set to false, adding endpoint and serialisation tests where coverage is thin.
Each exception points to an association loaded outside the service, and we fix it with a fetch join, an entity graph or a DTO projection, not by making associations eager. The rollout is per service behind configuration, starting with internal and low-traffic services and leaving checkout and payments for last.
For each one we watch error rates and pool metrics and can switch back in minutes. Once the pattern is proven, I would change the default in our shared starter to false, and unfinished services set it to true explicitly, which makes the remaining work searchable.
To get buy-in I would show our own data, like pool usage time and query counts before and after, and give teams a short guide with the fix patterns.
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 "You want to switch off open session in view across 60 services where it has always been...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/a8f929b11d/