Delay loading anomaly in Java program and its solution _java

Source: Internet
Author: User

The so-called delay loading is lazy loading (lazy), delay loading.

When to use lazy load, I can only answer to use lazy load when loaded with lazy.

As for why lazy to load, that is, when we want to access the amount of data is too large, obviously with the cache is not appropriate, because the memory capacity is limited, in order to reduce concurrent volume, reduce the consumption of system resources,

We let the data load when it was needed, and we used lazy loading. Lazy loading of the exception there are many kinds of, you are referring to the session has been closed the exception? Because that beginner is most likely to encounter the situation is when you load a one-to-many relationship on the end of the object, and then after the transaction place, such as the page, but also want to get multiple objects inside the other than the ID of the time, will run out of this exception.

For example, you have a Grade object that associates a lot of Student objects with lazy loading.

You get a Grade object in your service with a database query, which has a set<student> students collection, but you don't do any further work on the object (such as Intialize things like that let lazy loaded proxy objects really instantiate it.
And then when you get to the presentation of the page, you go through the students collection, and you try to display the Name property of each student on the page (this property is not StudentID) this time it throws the session closed exception

In the SSH framework:

Opensessioninviewfilter can be used directly
Configuring in Configuration Files

Copy Code code as follows:

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

The above is the entire content of this article, I hope that the small partners can enjoy.

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.