Open Session In View (OSIV) is pattern where persistence context (session) is open and available in the presentation layer and closed at the end of the HTTP request.
Open Session In View (OSIV) is pattern where persistence context (session) is open and available in the presentation layer and closed at the end of the HTTP request. This pattern became prominent with Spring/Hibernate so it was possible to traverse between ORM (Hibernate/JPA) entities in order to render them without getting the famous "LazyInitializationException" (LIE) from the Hibernate framework.
Pesistence context (session) may be open at the start of the request but there are solutions how to do it on demand - which is preferred if the session is not always necessary.
Pros of the OSIV usage are:
- it's easy to make changes in the presentation layer without changing the service layer/business logic, if one can traverse to the information across entities (alternative is prefetch everything on the business layer)
Cons of the solution:
- no clean separation of responsibilities,
- presentation may fetch objects lazily, lower performance (without service layer to even know about it), often leads to famous N+1 select problem,
- typically, transaction is closed at the end of the request, which is too late for business logic to treat exceptions (this can be solved using shorter transaction around the business layer call and not around the whole session)