Open Session In View

OSIV stands for Open Session In View, and it is a design pattern used in Hibernate to manage database transactions and sessions.

By keeping the session open, the OSIV pattern can help simplify the management of Hibernate sessions and transactions, and make it easier to work with lazy-loaded data. However, it can also have drawbacks, such as causing performance issues and potential memory leaks.

The OSIV strategy maintains persistence contexts and database connections from the beginning of the initial database connection until the end of the API response, just like the beginning of the transaction.

spring.jpa.open-in-view default value is true. Starting with version 2.0, Spring Boot now issues a warning if the Open Session In View mode is active so that you can disable it sooner than later.

The drawbacks are that the database connection is maintained for too long.

Turning off OSIV closes the persistence context at the end of the transaction and returns the database connection. Therefore, connection resources are not wasted.

For real-time APIs with a lot of traffic, it is recommended to turn off the OSIV option, and it is okay to turn on the OSIV option in places that do not occupy much connections, such as ADMIN.

Vlad Mihalcea

Vlad Mihalcea said "OSIV" is Anti-pattern.