What is this spring.jpa.open-in-viewtrue property in Spring Boot?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
spring.jpa.open-in-view=true keeps the JPA persistence context (the Hibernate Session) open for the entire duration of an HTTP request. This allows lazy-loaded entity associations to be fetched in the controller or view layer without throwing a LazyInitializationException. Spring Boot enables this by default, which is why you see the warning "spring.jpa.open-in-view is enabled by default" in your startup logs.
The property controls whether Spring registers an OpenEntityManagerInViewInterceptor that binds the EntityManager to the current request thread. When enabled, the session stays open from the moment the request enters the DispatcherServlet until the response is fully rendered.
How Open-in-View Works Internally
Without open-in-view, the lifecycle looks like this:
With spring.jpa.open-in-view=true:
The interceptor opens the EntityManager before the controller runs and closes it after the view is rendered. Any @Transactional service method operates within this same session, and lazy associations remain accessible even after the transaction commits.
Practical Example
Consider an Author entity with a lazy books collection:
A service that fetches authors:
And a controller that passes authors to the view:
With open-in-view=true, the Thymeleaf template can iterate over author.books even though the @Transactional method has already returned. The session is still open.
With open-in-view=false, accessing author.books in the template throws:
Why Spring Boot Enables It by Default
The Spring Boot team chose true as the default because it reduces friction for beginners. Without it, every lazy association accessed outside a @Transactional boundary requires either eager fetching, a JOIN FETCH query, an @EntityGraph, or a DTO projection. These are all valid solutions, but they require the developer to think about the session lifecycle upfront.
The tradeoff is that the default behavior can mask architectural problems and introduce subtle performance issues, which is why Spring Boot logs a warning about it on startup.
The Case Against Open-in-View
Database Connection Holding
The most serious problem is that the database connection is held for the entire request duration. If the view rendering takes 200ms (template processing, serialization), the connection sits idle for those 200ms. Under load, this exhausts the connection pool:
With open-in-view disabled, the connection is returned to the pool as soon as the @Transactional method exits. Rendering happens without holding a connection.
N+1 Query Problem in Views
Open-in-view makes N+1 queries easy to create accidentally. When a template iterates over 100 authors and accesses author.books for each one, Hibernate fires 100 additional SELECT queries, one per author. This happens silently because there is no @Transactional boundary to make the queries visible in the service layer:
Accidental Writes Outside Transactions
With the session open and entities in managed state, any modification to an entity in the controller is automatically flushed to the database when the session closes. This can cause unintended writes:
Disabling Open-in-View
To disable it, set the property in application.properties or application.yml:
After disabling, you need to ensure all data accessed by the view is fetched within the @Transactional boundary. The main strategies are:
JOIN FETCH Queries
Entity Graphs
DTO Projections
Comparison: Open-in-View On vs Off
| Aspect | open-in-view=true | open-in-view=false |
| Session lifecycle | Entire HTTP request | Within @Transactional boundary |
| Lazy loading in view | Works | Throws LazyInitializationException |
| Connection hold time | Full request duration | Service method duration only |
| N+1 risk in views | High (silent) | None (forces explicit fetching) |
| Accidental writes | Possible (auto-flush) | Not possible (detached entities) |
| Startup warning | Yes (default enabled) | No |
| Developer effort | Lower (initially) | Higher (requires fetch planning) |
When to Keep It Enabled
Open-in-view is reasonable for small internal tools, prototypes, and applications with very low concurrency where connection pool exhaustion is not a concern. It is also acceptable when the team has strong discipline around avoiding N+1 queries in templates.
For any application serving significant traffic, processing JSON API responses (where view rendering involves serialization), or running in a microservices architecture with shared connection pools, disabling open-in-view is the better architectural choice.
Common Pitfalls
Assuming the startup warning is harmless. The log message "spring.jpa.open-in-view is enabled by default" exists because the Spring Boot team considers this a footgun. Acknowledge it and make a deliberate decision rather than ignoring it.
Disabling open-in-view without updating queries. Flipping the property to false without adding JOIN FETCH or @EntityGraph annotations causes immediate LazyInitializationException crashes. Audit every controller and view for lazy association access before disabling.
Using FetchType.EAGER as a blanket fix. Making all associations eager avoids LazyInitializationException but creates a different performance problem: every query loads the full object graph whether it is needed or not. Prefer selective JOIN FETCH or DTO projections.
Thinking open-in-view only affects server-rendered views. REST APIs that return entities with Jackson serialization are equally affected. Jackson walks the entity graph during serialization, triggering lazy loads for every uninitialized association. This is the same N+1 problem, just in JSON form.
Ignoring connection pool metrics. Open-in-view's connection holding is invisible without monitoring. Add HikariCP metrics (spring.datasource.hikari.metrics-tracker-factory) and watch for connection wait times. Rising wait times under load are the first signal that open-in-view is costing you.
Summary
spring.jpa.open-in-view=truekeeps the Hibernate session open for the entire HTTP request, enabling lazy loading in controllers and views.- Spring Boot enables this by default for developer convenience, but logs a warning about it.
- The main costs are extended database connection holding, silent N+1 queries in views, and the risk of accidental entity modification outside transactions.
- Disable it with
spring.jpa.open-in-view=falseand switch toJOIN FETCH,@EntityGraph, or DTO projections for data that views need. - Monitor your connection pool metrics to detect whether open-in-view is causing connection exhaustion under load.

