Conflicts between delayed loading and session Shutdown

Source: Internet
Author: User

Delayed loading (no session or session was closed) means that the data is not loaded when it is read, but is loaded when it is used.

So how does hibernate know when data is used by users? How does one load data?

In fact, it is very simple. It uses a proxy mechanism. What is returned to the user is not the entity itself, but the proxy of the object. The proxy object loads data from the database when the user calls the getter method.

However, the database connection is required to load data. When we close the session, the database connection is closed at the same time. This is called the uninitialized relationship.

The conflict between delayed loading and session shutdown is generally handled as follows:

1. Use urgent loading:

Fetchtype. Eager: when an object is loaded, the attributes that are urgently loaded are immediately loaded from the database.

2. Obtain the data to be queried before closing the session (hibernate. initialize () method ).

3. Use interceptor or filter to control the session.

For example:

  1. <! -- Implement opensessioninview of spring -->
  2. <Filter>
  3. <Filter-Name> opensessioninviewfilter </filter-Name>
  4. <Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter
  5. </Filter-class>
  6. <! -- The default value of singlesession is true. If it is set to false, opensessioninview is useless. Therefore, it can be left empty by default -->
  7. <Init-param>
  8. <Param-Name> singlesession </param-Name>
  9. <Param-value> true </param-value>
  10. </Init-param>
  11. <! --
  12. Specify the name of org. springframework. Orm. hibernate3.localsessionfactorybean in the spring configuration file. The default value is sessionfactory. If the name of localsessionfactorybean in spring is not sessionfactory, you must specify this parameter. Otherwise, the exception of sessionfactory cannot be found. Therefore, it can be left empty by default.
  13. -->
  14. <Init-param>
  15. <Param-Name> sessionfactorybean </param-Name>
  16. <Param-value> sessionfactory </param-value>
  17. </Init-param>
  18. </Filter>
  19. <Filter-mapping>
  20. <Filter-Name> opensessioninviewfilter </filter-Name>
  21. <URL-pattern>/* </url-pattern>
  22. </Filter-mapping>

 

Conflicts between delayed loading and session Shutdown

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.