Announcement Announcement Module
No announcement yet.
LazyInitialzationException on retrieving large collection map (OSIVFilter turned on) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitialzationException on retrieving large collection map (OSIVFilter turned on)

    Guys this is a serious problem. please try to help me on this. I am stuck on some fundamental problem on my project using Spring and Hibernate. The problem is so stark and serious it makes me feel why I am using the combination of Spring and Hibernate at the first place.
    I have a web application using the following architecture - Struts with JSP 2.0 and JSTL for front end + Spring as the wiring intermediary + Hibernate as the ORM.

    Now I am having a search page where I have a number of text fields where user can enter criteria. I have a Criteria based object map retrieval functionality in my corresponding DAO which fetches the object map (of the object along with its associated collection mappings). What i mean is - Their is an EMPLOYEE class with many LICENCES. (EMP contains a one-to-many java.util.Set based mapping of LICENCE collections). On search page the user can choose the page to show all users existing in the system and I will simply load all EMPLOYEES in the system through Criteria.

    Now I get the EMPLOYEE list from criteria.list() and send it to Struts action where I put the list in HTTPSession and show on JSP. I am using a Pagination Tag Library where I will show only a small list of users at a time (like Google which says 1-5 6-11 12-16 etc...)

    So far so good, but when I click on next link i.e. 6-11 I get a lazyLoadexception saying the session is already closed.

    I have put a mapping of OSIVFilter in my web.xml which looks something like this -
    <filter-class> penSessionInViewFilter</filter-class>

    I have put this Filter mapping in web.xml to close any duplicate sessions since I get an exception - A duplicate session is open for collection (or something like this). I guess since I have a collection of LICENCE in my EMP class and when I am updating the EMP with the LICENCE it is throwing such exception. I put OSIVFilter to kill such open sessions, but i guess now it is giving such problem (of lazyloading) since OSIVFilter kills all outstanding sessions too...

    Please help folks. all suggestions are very much welcome. Please suggest me what to do. This is really a serious bug.
    Thanks a lot,

  • #2
    anyone please do reply...


    • #3
      Your config looks ok. What code gets executed when you click next?

      Here is my guess what happens.

      The first time you display the page, you retrieve the list of employees and store that list in your session. The page gets rendered and displayed to the user. After rendering the hibernate session gets closed.

      Now you click next, your controller (or taglibrary) uses the list stored in the session to display the selected page. However the hibernate session which was used to retrieve those objects has already been closed. Hence your error.

      To use those objects again you either have to reattach them to a new session or retrieve them again from the database.

      Also there is a lot of information in this forum (use the search) and also try the hibernate forums.


      • #4
        How do i reattach the list/make a new DB hit?

        hi, as u said there are 2 ways to solve this problem -
        >> To use those objects again you either have to reattach them to a new session or retrieve them again from the database.

        Quick questions -
        1) How do i reattach the employee objects back to a hibernate session?
        2) does it make any sense to eagerly load the objects on first time database hit instead of relying on lazyloading strategy???

        I kind-of tried to solve this problem by putting - outer-join='true' in the association mapping in EMPLOYEE class.

        I looked at the SQL generated in the console and found that it indeed was fetching all LICENSES at first time database hit...

        But is this approach good? i am not sure about this since i think sooner or later i will again bump into lazyloading exception in some other JSP and will again have to make outer-join='true' in the association mapping which i dont want to do...

        please suggest something. please forward any links/forums u may have..

        any help is EAGERLY awaited and un-LAZILY appreciated
        Thanks, Vaibhav


        • #5
          Those are really Hibernate questions, not Spring specific. You can reattach an object using Session.lock(obj, LockMode.NONE).