Careeroza
Open Session in View
What It Is
Spring Boot enables open-session-in-view (OSIV) by default. It keeps the EntityManager open for the whole HTTP request, including JSON serialisation, so lazy associations load "magically" in the controller or Jackson. Spring Boot even logs a warning at startup:
Startup Warning
./mvnw spring-boot:runExample output
WARN JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default.
Therefore, database queries may be performed during view rendering.
Explicitly configure spring.jpa.open-in-view to disable this warningWhy to Turn It Off
- Hidden N+1: Jackson walks
order.items[].product.categoryand fires queries outside your service, where nobody sees them. - The database connection is held for the whole request, including slow serialisation and network writes, so the pool runs out under load.
- Lazy loads run outside a transaction (auto-commit), each in its own tiny transaction: inconsistent reads.
Fix
application.properties
Example output
spring.jpa.open-in-view=false
Then, in the service, load exactly what the response needs (JOIN FETCH, @EntityGraph, or DTO projections) and return DTOs. A LazyInitializationException after this change shows you exactly which data was missing from the query.