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 warning

In Spring boot, by default OSIV (Open Session in View) is enabled. As OSIV affects the connection and performance of the database, this warning log will be displayed when starting the spring boot application.



Exception

2019-10-05 16:32:06.548  WARN 3840 --- [  restartedMain] aWebConfiguration$JpaWebMvcConfiguration : 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 warning


Root Cause

In Spring boot, by default OSIV (Open Session in View) is enabled. As OSIV affects the connection and performance of the database, this warning log will be displayed when starting the spring boot application.

If OSIV flag is enabled (default), OSIV enables OpenEntityManagerInViewInterceptor to register EntityManager to the current thread. So EntityManager is available throughout the lifespan of the web request. A database connection is created for each web request and stays untill the web request is completed.

The benefit of OSIV is that the database connection exists till the presentation layer. so Lazy fetch on associated objects in many-to-one and one-to-many association can be done even while rendering in the presentation layer. This makes developers life easier as database connection exist throughout the request results no LazyInitializationException.

The downside of OSIV is that it affects connections and performance of the database. Database connection should be created and released in service layer. OSIV retains the connection till UI layer which increases database connection lease time. The number of concurrent request to the database will be increased. Waiting time to get a connection from the pool increases. Lazy fetch problems are not identified at the time of development time. It will cause problems in production environment.

OSIV should be disabled for an enterprise application as it creates database performance issue.



Solution 1

OSIV has been enabled by default. It can be enabled in application properties using the configuration below. If OSIV is enabled, database performance should be tested and taken care in production environment.

spring.jpa.open-in-view=true


Solution 2

OSIV is disabled in application properties using the following configuration. The developer should take care of the database connection and lazy fetch association in service layer.

spring.jpa.open-in-view=false



Related Articles

2 Comments

  • характеристика колбас , January 23, 2020 @ 12:39 AM

    Hi, just wanted to say, I liked this article. It was inspiring. Keep on posting!| а

    • Yawin Tutor , January 23, 2020 @ 6:27 PM

      Thank you very much

Leave a Reply

Your email address will not be published. Required fields are marked *