Announcement Announcement Module
No announcement yet.
Hibernate, OpenSessionInView and Transactions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate, OpenSessionInView and Transactions

    Spring 2.0
    Hibernate 3.1

    The thread here discusses a problem very similar (or maybe the same) to mine.

    The problem is, is that I'm not quite that advanced yet so I think I'm having a more fundamental problem. I believe I understand the basic concept of OpenSessionInView (OSIV). I *think* the problem I'm having is related to setting up a transaction for getCurrentSession() to use. I'm working on a very heavily ajax-based web app. Basically, the user makes a request to edit a persistent object. The object is loaded from the database and stored in the HttpSession. Now the user is able to edit its properties, add & remove objects from its collections (can be nested pretty deep), etc. all done through AJAX. The approach I used before (the HibernateUtil/Threadlocal tool) would open and close the Hibernate session upon each request. The problem came when the app would load an object from another session (each AJAX request would create a new session) and put it in a collection of the object stored in the HttpSession. In other words... something like this...

    1) Request1 comes in to edit a persistent object
    2) SessionA created
    3) Object1 loaded from DB and stored in HttpSession
    4) SessionA closed
    5) Request2 comes in to add an existing persistent object to a collection in Object1 (from step 2).
    6) SessionB created
    7) Object2 is loaded using SessionB and added to a collection in Object1 (which was loaded using SessionA).
    8) SessionB is closed
    9) User makes a request to save-or-update Object1.
    10) SessionC is created
    11) Object is saved. Changes are actually persisted just fine. However, a follow-up call (in the same request) to view any properties in that added collection item results in a "the owning Session was closed" error!

    Everything I read suggests that the new getCurrentSession() and OSIV methods are the answer to my prayers. Basically, I think I need a transaction that spans multiple requests in conjunction with OSIV. But this is where I get totally lost in not knowing how to set up this sort of transaction. Would you suggest declarative or programmatic transaction demarcation for this problem? How? Sorry, I've read the docs on Spring/Hibernate transactions, but I'm just not getting it. If anybody would be willing to point me to a more specific example or lay it out in 'dummy' english, it would be greatly appreciated.

    thank you

  • #2
    never mind. I gave up and am sticking with detaching/re-attaching objects for now.


    • #3
      I think you want to have a session that spans several request, not a transaction. Hibernate session can be long lived and start/end transactions as needed, but I'm not sure Spring, at the moment, supports such stuff out of the box.