The solution and deep analysis of hibernate lazy loading anomaly

Source: Internet
Author: User

reason for occurrence :

Hibernateallow lazy loading on associated objects and properties,The premise is that the operation is in the sameHibernateSessionwithin the scope of.If such a situation occurs,whenServiceLayer Returns a feature that enables lazy loadingDomainobject toWebLayer,andWebwhen the layer accesses these objects or properties that require lazy loading,due to loadingDomainobject thatSessionhas been turned off causing no access,An access exception will occur.

Workaround :

Span style= "White-space:pre" > we can use a filter Span lang= "ZH-CN" style= "font-family: Chinese Italic" > in web Span lang= "ZH-CN" style= "font-family: Chinese italic" > When requested, bind it a Session This will ensure that the entire request is in the process of session All are open , can be deferred loading operation

Springthis filter is provided.----Opensessioninviewfilter,it putHibernateSessionbind to the request thread, Sessionwill automatically beSpringthe transaction manager Manager.even ifServicetransaction completion of the layer, Sessionwill also be turned on to support deferred load operations.

the specific approach is to Web. XML Configure this filter in the :


<filter>      <filter-name>OpenSessionInViewFilter</filter-name>      <filter-class> Org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class> </filter>< filter-mapping>    <filter-name>OpenSessionInViewFilter</filter-name>     <url-pattern>* .action</url-pattern></filter-mapping>


advantages and disadvantages of this workaround :

Span style= "White-space:pre" > example of a realistic project :

Span style= "White-space:pre" > if user department Span lang= "ZH-CN" style= "font-family: Chinese Italic" > is a many-to-one entity association relationship We want to display in the page User information ,user department use lazy loading

using opensessioninviewfilter, serviceLayer provides aQueryuser()way to get it done .,lazy Loading is automaticallyWebtriggered when a layer initiates a call.UseFilterfeel twice as cool as a dog,It's easy to use, you don't have to check the results .,if the project is not big,, serviceThe layer code is well developed and maintained..

Let's think about whether there's a performance problem. .if the project is large and highly concurrent,This is not going to be so cool..for what??each oneWebrequests are bound to aHibernateSession,all requests are boundSessionafter,means that all requests will consume a data connection.,until after the request processing is complete!Which meansWebthe request will take up a lot longer than the original data connection.,even someWebThe request does not need to use a data connection at all,This concurrency of access to data connection resources is more likely to cause performance bottlenecks.because most of our bottlenecks are on the database.~

is there any good solution? ?

There is . , make request bindings that require a connection to the database Session , you don't need a binding. , is a , just Technology for adding filters in front of filters I'm not quite sure.

say something that can be achieved. ,just don't opensessioninviewfilter..that lazy loading exception how to solve AH?Let's change the train of thought,put the lazy load thing inServicecompleted within,don't let it cross outServiceto theWeblayer of.It's going to beServicelayer design for multiple interfaces,and from the above example ,,I wasServiceLayer Write two interfaces,a callgetuserwithdepartment(),a callgetuserwithoutdepartment ().

Itoo Project Experience Summary :

itoo is not used inHibernateThe lazy load,Unnecessary Association queries at that time caused the system to display slowly by optimizingSQLstatement resolves the.Why don't we just lazy load such a good thing?because our lazy load operation is completely out of theHibernateSessionthe scope,one isEJBin the containerSession,one isSpringin theHibernate Session,out of range, of course, it doesn't work..


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The solution and deep analysis of hibernate lazy loading anomaly

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.