Open Session in View: why to turn it off
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
understand and disable Open Session in View (OSIV).
Planned topics
- OSIV keeps the persistence context open until the HTTP response is written.
- It hides
LazyInitializationException, but lets queries run in controllers and JSON serialization. - That means unplanned N+1 and a database connection held for the whole request.
- Spring Boot enables it by default (with a startup warning).
- Set
spring.jpa.open-in-view=falseand load data explicitly in services.
Draft outline details
Demonstration
spring:
jpa:
open-in-view: falseCheck your understanding
What does OSIV hide?
Show answer
Lazy loading outside the service layer.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.