Use principle and performance analysis of openSessionInView

Source: Internet
Author: User

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.