Use principle and performance analysis of openSessionInView
OpenSessionInView is used in many projects. This is nothing more than convenient development. You can operate database-layer services on the JSP page. The usage and performance of openSessionInView are described below.
Usage:
1. Add a Filter to control the connection management of transactions and databases. The Code is as follows:
SessionFactory sessionFactory = lookupSessionFactory(request);Session session = null;boolean participate = false;if (isSingleSession()) {// single session modeif (TransactionSynchronizationManager.hasResource(sessionFactory)) {// Do not modify the Session: just set the participate flag.participate = true; }else {logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");session = getSession(sessionFactory);TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));}} else {// deferred close modeif (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {// Do not modify deferred close: just set the participate flag.participate = true; } else {SessionFactoryUtils.initDeferredClose(sessionFactory); }}try {filterChain.doFilter(request, response);} finally {if (!participate) { if (isSingleSession()) { // single session modeTransactionSynchronizationManager.unbindResource(sessionFactory);logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");closeSession(session, sessionFactory);}else {// deferred close modeSessionFactoryUtils.processDeferredClose(sessionFactory);}}}The current Code is just an example. For details, refer to the actual situation in your project (spring configuration, hibernate configuration, etc)
2. Add the Filter configuration information in web. xml.
OpenSessionInView
com....OpenSessionInView
OpenSessionInView
/*
3. Retrieve SESSION operation data on a specific Jsp page
The usage ends here. Let's briefly discuss the performance issues.
In fact, according to the above configuration, we can think of: for the processing of the configured Filter, each time a request is intercepted, before processing in the background, a transaction is enabled and a database connection is opened (thread-safe). After processing in the background, the current connection is closed. When we intercept all requests and do this, what do you think will be the performance of opensessioninview? (If it matches some requests, the effect will be better)
I drew a simple schematic of opensessioninview, which is a little simple. Don't like it: