Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What does spring.jpa.open-in-view do, what is its default in Spring Boot, and why does Boot log a warning about it?

spring.jpa.open-in-view registers OpenEntityManagerInViewInterceptor, which opens an EntityManager when a web request starts and keeps it open until the response is finished, so lazy associations can still load in controllers and views. It is true by default, and Boot logs a warning at startup when we have not set it explicitly, because the convenience hides queries that run outside any transaction and can hold database connections for much longer than we expect.

Why this question is asked

Open session in view, usually shortened to OSIV, is one of the few Spring Boot defaults that Boot itself warns us about. Many applications run with it for years without anyone noticing, and then a performance problem or a pool exhaustion incident leads straight back to it. Interviewers ask about it because the answer shows whether we understand when a persistence context lives and dies, and what lazy loading really costs. In this answer, we will recap what a persistence context and lazy loading are, see exactly what the property switches on, look at the startup warning and why it exists, and then go through the costs and the setting most teams choose.

1A Quick Recap of the Persistence Context and Lazy Loading

When we use JPA, every entity we load is tracked by an EntityManager. The set of entities it tracks is called the persistence context. While the persistence context is open, the EntityManager can load more data on demand, notice changes we make to entities and write them back.

Lazy loading builds on this. If Order has a @OneToMany list of OrderLine marked as lazy (the default for collections), loading an Order does not load its lines. Hibernate puts a placeholder collection in the field, and the first time we touch it, Hibernate runs a second query to fill it. That second query needs an open persistence context. If the EntityManager is already closed, Hibernate cannot run the query and throws LazyInitializationException.

In a typical Spring service, the EntityManager lives as long as the @Transactional method. So without anything extra, an entity returned from a service is detached by the time the controller sees it, and touching a lazy field there fails.

Java
@Transactional(readOnly = true)
public Order findOrder(long id) {
return orderRepository.findById(id).orElseThrow(); // select from orders
}
// In the controller, with open-in-view=false:
Order o = orderService.findOrder(42);
o.getLines().size(); // LazyInitializationException: could not initialize proxy - no Session

The exact message differs between Hibernate versions, but the cause is always the same: the lines were never loaded, and the session that could load them is gone.

2What the Property Switches On

When spring.jpa.open-in-view is true and the application is a servlet web application, Spring Boot's JPA auto-configuration registers OpenEntityManagerInViewInterceptor as a web request interceptor. At the start of each request, it creates an EntityManager and binds it to the current thread. Every repository and every @Transactional method that runs during the request then reuses that same EntityManager instead of creating its own. When the request completes, after the view is rendered or the JSON is written, the interceptor closes it.

So the persistence context no longer ends with the service transaction. It ends with the request. Lazy fields can be touched in the controller, in a Thymeleaf template or while Jackson serialises the response, and Hibernate quietly loads them.

Java
# application.properties
spring.jpa.open-in-view=true # the default: one EntityManager per web request
// Same controller code, now with OSIV on:
Order o = orderService.findOrder(42); // select from orders (inside the transaction)
o.getLines().size(); // select from order_line ... (outside any transaction) -> 3
Step throughOne web request with open-in-view enabled
InterceptorControllerServiceDatabase
open EntityManager, bind it to the thread
handle request
findOrder(42)
begin; select order; commit
lazy select order lines, no transaction
response written
close EntityManager

7 messages in order. Play them, or step through them one at a time.

The persistence context, and possibly the connection, lives for the whole request rather than the transaction.

Notice the second comment. The query still runs, but it runs after the service transaction has committed, in auto-commit mode, in the web layer.

3Why Boot Logs a Warning About It

Spring Boot keeps the property true by default for backward compatibility: many older applications rely on lazy loading in views, and switching it off silently would break them. But the Boot team considers it a risky default. So when the property is enabled only because we did not set it, Boot logs a warning at startup saying that open-in-view is enabled by default, that database queries may therefore run during view rendering, and that we should configure the property explicitly to silence the warning (check your version for the exact wording).

The warning is not a bug report. It is a nudge to make a decision. Setting the property to true explicitly removes it just as setting it to false does, and that is the point: the team should choose on purpose.

4What OSIV Costs Us

The first cost is queries without a transaction. Lazy loads in the controller run in auto-commit mode, each in its own implicit transaction, so they do not see a consistent snapshot together with the data the service read. In a template that loops over 50 orders and touches each order's customer, we may also run 50 extra queries without noticing.

That leads to the second cost: N+1 problems are hidden. N+1 means one query to load a list and then one more query per element to load an association. With OSIV off, the controller would fail loudly with LazyInitializationException, and we would fix the fetch plan in the service. With OSIV on, nothing fails; the page just gets slower as data grows, and the queries show up far from the code that caused them.

The third cost is connection hold time. Once a request has used a database connection through the shared EntityManager, the connection can stay attached to it until the request ends, depending on Hibernate's connection handling mode. A slow template, a slow client reading the response, or an external HTTP call made in the controller all keep that connection out of the pool. Under load, the pool saturates even though each transaction is short.

OSIV on versus off
Concernopen-in-view=trueopen-in-view=false
Lazy field touched in controllerLoads quietly with an extra queryLazyInitializationException
Where N+1 shows upHidden in the web layerFails fast, fixed in the service
Transaction for web-layer readsNone (auto-commit)Not applicable, no reads there
Connection heldPossibly until the request endsOnly during the transaction
Startup warningYes, unless set explicitlyNo

5Turning It Off and Fetching Explicitly

Most teams set spring.jpa.open-in-view=false and make each service method return exactly the data the caller needs. We can do that with a fetch join in JPQL, with an @EntityGraph on a repository method, or by returning a DTO projection instead of an entity.

Java
spring.jpa.open-in-view=false
public interface OrderRepository extends JpaRepository<Order, Long> {
@EntityGraph(attributePaths = "lines")
Optional<Order> findWithLinesById(Long id); // one select with a join to order_line
}
Order o = orderService.findOrderWithLines(42);
o.getLines().size(); // 3, no extra query, no session needed

The reason this is better is that the data access is now visible in one place. Anyone reading the repository can see that this use case loads lines, and the SQL log shows one query instead of a scattered set. When a controller later touches something the service did not load, the exception tells us immediately rather than months later in a latency graph.

6Follow-up questions

Is OSIV the same as OpenSessionInViewFilter?

It is the same idea. OpenSessionInViewFilter and OpenSessionInViewInterceptor are the native Hibernate Session variants, and OpenEntityManagerInViewFilter and OpenEntityManagerInViewInterceptor are the JPA variants. Spring Boot uses OpenEntityManagerInViewInterceptor for servlet web applications. Reactive WebFlux applications do not get it, because there is no request-bound thread to attach an EntityManager to.

Does OSIV make lazy loading in views transactional?

No. The EntityManager stays open, but the transaction ended when the service method returned. Lazy loads after that run without a transaction, each statement on its own in auto-commit mode, so they can see data that changed after the service read its part.

What should we do for an existing application that relies on it?

Set the property to true explicitly to silence the warning, then switch it off in a test profile and run the test suite. Every LazyInitializationException points to a place where the service should fetch more explicitly. Fix those, then switch it off in production with monitoring in place.

7Conclusion

In this answer, we saw that spring.jpa.open-in-view registers OpenEntityManagerInViewInterceptor, which keeps one EntityManager open for the whole web request so lazy loading works in controllers and views. It is on by default, and Boot warns about it unless we set it explicitly, because it moves queries outside transactions, hides N+1 problems and can hold connections for the whole request. Most teams turn it off and fetch what they need in the service with fetch joins, entity graphs or projections.

What to say in the interviewAbout 80 seconds aloud

Open with

spring.jpa.open-in-view controls the open session in view pattern. When it is true, which is the default for servlet applications, Spring Boot registers OpenEntityManagerInViewInterceptor. That opens an EntityManager at the start of every web request, binds it to the thread, and closes it only after the response is rendered.

Then, if they want more

The benefit is that lazy associations can still be loaded in the controller or the view, so you do not get LazyInitializationException. Boot logs a warning at startup when the property was not set explicitly, because the default has real costs.

Lazy loads in the web layer run outside any transaction in auto-commit mode, N+1 query patterns stay hidden instead of failing, and the database connection can stay attached to the EntityManager until the request ends, so slow rendering or external calls in the controller hold connections and can exhaust the pool.

In my projects I set it to false and make service methods fetch exactly what the caller needs, with fetch joins, entity graphs or DTO projections. If an older application depends on it, I set it to true explicitly, run the tests with it off to find the lazy loading failures, fix them, and then switch it off.

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.
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 "What does spring.jpa.open-in-view do, what is its default in Spring Boot, and why does...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/6a3a98482c/