Application principle and performance analysis of Opensessioninview

Source: Internet
Author: User

See a lot of projects used in the Opensessioninview, this approach is nothing more than the development of convenient, you can operate in the JSP page database layer business. Below say Opensessioninview's usage and performance problem.

Use:

1, add a filter, which is used to control the transaction and database connection management, the code is as follows:

Sessionfactory sessionfactory = lookupsessionfactory (request);  
Session session = NULL;  
      
Boolean participate = false; if (Issinglesession ()) {//single session mode if (Transactionsynchronizationmanager.hasresource sessionfacto  
    ry)) {//does not modify the Session:just set the participate flag.  
       participate = true;  
    else {logger.debug ("Opening single Hibernate Sessions in Opensessioninviewfilter");  
    Session = GetSession (sessionfactory);  
    Transactionsynchronizationmanager.bindresource (Sessionfactory, new Sessionholder (session)); } else {//deferred close mode if (Sessionfactoryutils.isdeferredcloseactive (sessionfactory)) {//Do n  
    OT 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 mode Transactionsynchronizationmanager  
        . Unbindresource (Sessionfactory);  
        Logger.debug ("Closing single Hibernate Sessions in Opensessioninviewfilter");  
    CloseSession (session, Sessionfactory);  
    }else {//deferred close mode Sessionfactoryutils.processdeferredclose (sessionfactory); }  
}  
}

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/JSP/

The current code is just an example, the specific need to refer to their own project in the actual situation (Spring configuration, hibernate configuration, etc.)
2, add the configuration information of the filter in Web.xml

<filter>  
    <filter-name>OpenSessionInView</filter-name>  
    <filter-class>com .... opensessioninview</filter-class>  
</filter>  
<filter-mapping>  
    <filter-name> opensessioninview</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping >

3, in the specific JSP page to take the session operation data

This is the end of the usage here, let's discuss the performance issues briefly below

In fact, everyone according to the configuration of the above can be thought: for the configuration of filter processing, each will intercept to a request, in the background processing will open a thing and open a database connection (thread-safe), after the completion of the background processing will close the current connection. Interception to all requests to do so, people think Opensessioninview way performance will be good where to go? (If you are matching certain requests, the effect is a little bit better)

drew a simple schematic diagram of Opensessioninview, a bit shabby, do not like to spray:

Author: csdn Blog Maple fluttering moment

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.