Lazy loading of hibernate and its contradiction with session closure

Source: Internet
Author: User

Lazy loading is not the time to load the data when it is read, but to wait until it is loaded.

So how does hibernate know when users are using the data? And how does it load data?

In fact, it is very simple, it uses the proxy mechanism. It is not the entity itself that is returned to the user, but the proxy for the entity object. The proxy object will go to the database to load the data when the user invokes the Getter method.

However, loading the data requires a database connection. And when we close the session, the database connection is closed at the same time. This is called an uninitialized relationship.

The contradiction between delayed loading and session closing can generally be handled like this:

1), turn off the lazy load feature. It is easier to operate because Hibernate's lazy load feature is controlled within the HBM configuration.     The default lazy= "true", instead lazy= "false" is OK.     But the pitfalls of using this solution are very large. First of all, the presence of no session or session was closed proves that you are already using the Foreign Key association table, and if you remove the lazy load, then the cost of each query becomes very large, if the association table more, the consequences can be imagined. So it is not recommended to use this method to solve.

2), before the session closed before the data we want to query first obtained. The first thing you need to know is when the session closes, which is its life cycle. Normally hibernate will close the session when the query data is closed and use the Gethibernatetemplate (). The Get method query will delay the shutdown time.     Will not close until after the transaction has ended.     Use the Interceptor (interceptor) or filter to control the session. Spring provides a solution to the Hibernate feature to effectively control the session lifecycle.

Because we haven't talked about spring, we use filters to control the session in class.

First, a class Opensessioninviewfilter inherits the filter, overrides the Dofilter method, and writes the session's acquisition and closing in try,finally. The instance code is as follows:

public void DoFilter (        servletrequest arg0,         servletresponse arg1,        filterchain chain)                        throws IOException,   servletexception {        try {            hibutil.getsession ();            Chain.dofilter (arg0, arg1);        } finally {            hibutil.closesession ();        }}

Then add the relevant configuration in the Web. XML configuration file, for example:

<filter>    <filter-name>OpenSessionInViewFilter</filter-name>    <filter-class>        tarena.util.OpenSessionInViewFilter    </filter-class></filter><filter-mapping>    <filter-name>OpenSessionInViewFilter</filter-name>    <url-pattern>*.action</url-pattern > </filter-mapping>

Finally, remember to modify the service Layer transaction processing code, the closing session of the Code commented out.

Lazy loading of hibernate and its contradiction with session closure

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.