Instead of letting the business layer decide how it’s best to fetch all the associations that are needed by the View layer, OSIV (Open Session in View) forces the Persistence Context to stay open so that the View layer can trigger the Proxy initialization, as illustrated by the following diagram.
openSessionmethod of the underlying
SessionFactoryand obtains a new
Sessionis bound to the
javax.servlet.FilterChainobject reference and the request is further processed
DispatcherServletis called, and it routes the HTTP request to the underlying
PostServiceto get a list of
PostServiceopens a new transaction, and the
HibernateTransactionManagerreuses the same
Sessionthat was opened by the
PostDAOfetches the list of
Postentities without initializing any lazy association.
PostServicecommits the underlying transaction, but the
Sessionis not closed because it was opened externally.
DispatcherServletstarts rendering the UI, which, in turn, navigates the lazy associations and triggers their initialization.
OpenSessionInViewFiltercan close the
Session, and the underlying database connection is released as well.
At first glance, this might not look like a terrible thing to do, but, once you view it from a database perspective, a series of flaws start to become more obvious.
The service layer opens and closes a database transaction, but afterward, there is no explicit transaction going on. For this reason, every additional statement issued from the UI rendering phase is executed in auto-commit mode. Auto-commit puts pressure on the database server because each transaction issues a commit at end, which can trigger a transaction log flush to disk. One optimization would be to mark the
Connection as read-only which would allow the database server to avoid writing to the transaction log.
There is no separation of concerns anymore because statements are generated both by the service layer and by the UI rendering process. Writing integration tests that assert the number of statements being generated requires going through all layers (web, service, DAO) while having the application deployed on a web container. Even when using an in-memory database (e.g. HSQLDB) and a lightweight webserver (e.g. Jetty), these integration tests are going to be slower to execute than if layers were separated and the back-end integration tests used the database, while the front-end integration tests were mocking the service layer altogether.
The UI layer is limited to navigating associations which can, in turn, trigger N+1 query problems. Although Hibernate offers
@BatchSize for fetching associations in batches, and
FetchMode.SUBSELECT to cope with this scenario, the annotations are affecting the default fetch plan, so they get applied to every business use case. For this reason, a data access layer query is much more suitable because it can be tailored to the current use case data fetch requirements.
Last but not least, the database connection is held throughout the UI rendering phase which increases connection lease time and limits the overall transaction throughput due to congestion on the database connection pool. The more the connection is held, the more other concurrent requests are going to wait to get a connection from the pool.
Unfortunately, OSIV (Open Session in View) is enabled by default in Spring Boot, and OSIV is really a bad idea from a performance and scalability perspective.
So, make sure that in the
application.properties configuration file, you have the following entry:
This will disable OSIV so that you can handle the
LazyInitializationException the right way.
Starting with version 2.0, Spring Boot issues a warning when OSIV is enabled by default, so you can discover this problem long before it affects a production system.
~ Answered on 2018-01-12 09:14:31