Java programmers from stupid birds to cainiao () detailed description and usage of spring (12) opensessioninview

Source: Internet
Author: User


First, let's take a look at what is opensessioninview?


When the load method is used in hibernate, the session is closed when the data is not actually obtained. When we really want to obtain the data, the load will be forced to load the data, and the session has been closed at this time, therefore, an exception occurs. Typically, in MVC mode, when the persistent layer is called on the m layer to obtain data (the persistent layer uses the load method to load data), when the call ends, the session is closed, and we want to use the data in layer V to force the load to load the data. We want the session to be open at this time, this is the so-called open session in view. We can use filters to achieve this goal. This article introduces Baidu encyclopedia, but it does explain the role of opensessioninview.

Opensessioninviewfilter is a support class provided by spring for hibernate. It mainly means to open the hibernate session when initiating a page request and keep the session until the request ends, it is implemented through a filter. Because hibernate introduces the lazy load feature, Hibernate will throw a lazyload exception if you want to get the value of the associated object from the object in the hibernate session cycle through the getter method. To solve this problem, spring introduces this filter, which leads to a longer life cycle of the hibernate session.


First, we can analyze its source code and find that its functions are actually relatively simple:

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);}}}

In the above Code, first obtain sessionfactory and then get a session through sessionfactory. Then execute the real action code and close the hibernate session as needed. The entire idea is clear.


Next let's take a look at his specific configuration, which is actually very simple. It's okay to directly configure the filter in Web. xml:

<filter><filter-name>openSessionInView</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><param-name>sessionFactoryBeanName</param-name><param-value>sf</param-value></init-param></filter><filter-mapping><filter-name>openSessionInView</filter-name><url-pattern>/*</url-pattern></filter-mapping>

Pay attention to the following points in the above Configuration:

1. This filter must be configured in front of the struts filter, because the filter is "advanced and backward". If you are configured behind struts, your opensessioninview filter has been executed, how can I manage the redirection page of an action.

2. opensessioninview also requires bean injection from sessionfactory. By default, it finds the bean whose ID is sessionfactory. If the bean ID of sessionfactory is not the name, remember to configure a parameter named sessionfactorybeanname for this filter and set its value to the ID value of your sessionfactory bean.

3. When using opensessioninview, you must note that if you do not configure transaction, when using opensessioninview, the transaction is configured as only-read by default. In this case, if you add, delete, modify, or delete a read-only transaction. If opensessioninview is removed, the start boundary of the default transaction is locked on the DaO layer operation. The Dao layer hibernatetempt provides the start and commit of the transaction.

Side effects of opensessioninview

After learning about the above problems, you can probably know the side effects of opensessioninview-serious resource occupation, improper configuration, and impact on system performance. After opensessioninview is used, if any step is blocked in the process returned by the request and response, the connection will be occupied and not released during this period. For example, if the page is large, the display takes time or the network speed is too slow, and the transmission time between the server and the user is too long, this will lead to resource occupation, the most direct manifestation is that the connection pool connection is insufficient, as a result, the server cannot provide services.

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.