A detailed nosession problem in hibernate

Source: Internet
Author: User

1. Preface

Today when consolidating the SSH framework, when the performance layer calls the bean layer to find data, the error, as shown below



According to the literal meaning, should be the agent can not be initialized, the session has been closed, this blog to solve this problem.


2.NoSession Problem

1. Cause analysis

when executing the session's load () method, Hibernate does not immediately execute a SELECT statement that queries the Customers table, returning only instances of the proxy class of the customer class, which is characterized by the following characteristics:

(1) dynamically generated by hibernate at run time, it expands the customer class, becausethis inherits all the properties and methods of the customer class, but its implementation is transparent to the application.
(2) When Hibernate creates an instance of the customer proxy class, it only initializes its OID property, and all other properties are null, so this proxy class instance consumes little memory.
(3) Hibernate initializes the proxy class instance when the application accesses the customer proxy class instance for the first time (for example, by calling the Customer.getxxx () or Customer.setxxx () method). Executes the SELECT statement during initialization to actually load all the data from the customer object from the database. One exception is that when an application accesses the GetID () method of the Customer proxy class instance, Hibernate does not initialize the proxy class instance because the OID exists when the proxy class instance is created and does not have to be queried in the database.

Tip: Hibernate uses the Cglib tool to generate proxy classes for persisted classes. Cglib is a powerful Java bytecode generation tool that dynamically generates extensions while the program is runningJava class or a proxy class that implements the Java interface. For more information on Cglib, please refer to: 

Spring Learning Note Five (the difference between Jdkproxy and Cglibproxy)。


So literally we learned that the session was closed when the presentation layer was called. Because hibernate is loaded with load, the default is lazy loading, that is, the lazy=true operation, so when the load is finished, the session can be closed, if the output is worth again, it will report the above error, Because our session is just placed on the DAO layer. The presentation layer is not available at all.

public void Load () {//Call lazy load User User1 = Userservice.getbyid (1);//The following sentence will be an error System.out.println (User1.getname ()); System.out.println ("----------------------"); User user2 = Userservice.getbyid (1); System.out.println (User2.getname ()); System.out.println ("----------------------");}


2. Method One

Now that the analysis is the cause of the lazy load, we can close the deferred load property, that is, not delay loading the lazy=false, but only when it is determined that the business does not require a lazy load, consider this design. This is a solution, but a bit inflexible, so that the characteristics of such a good delay loading to the living removal.

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"                                   "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">



3. Method Two

We can put the life cycle of the session into the presentation layer, so that we can get the desired data under the application of the deferred loading feature. This is usually done when the operation is Opensessionview and configured in the Presentation layer Web. xml

<filter><filter-name>openSessionInView</filter-name><filter-class> org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class></filter>< filter-mapping><filter-name>opensessioninview</filter-name><url-pattern>/*</ Url-pattern></filter-mapping>

4. Method Three

The first way to do this is to close the global lazy load, a bit too inflexible. In hibernate we can dynamically turn off deferred load properties based on our business needs. The action taken is hibernate.initialize (user); But one thing to be aware of when using it is that the proxy is a persistent object's associated object property, such as a entity, and you want to check out the associated entity B for a, then write Hibernate.initialize (A.B).

Public user getById (int id) {User user = Usereao.getbyid (ID); Hibernate.initialize (user); return user;}




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

A detailed nosession problem in hibernate

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.