Page 1 of 2 12 LastLast
Results 1 to 10 of 22

Thread: Hibernate Long Session Per Flow?

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Location
    Austin, Texas USA
    Posts
    46

    Default Hibernate Long Session Per Flow?

    A FormAction with a flow-scoped form backing object is a nice way to load a domain object using Hibernate and modify it over the course of a flow. It's natural that at the end of the flow, the object could then be submitted to Hibernate for persistence.

    The unfortunate truth of the matter is that keeping a detached object in the flow scope and attempting to reassociate it with another Hibernate Session can be a difficult proposition.

    The "Long Session" is often billed as the answer to this problem. Elsewhere in these forums Spring users have been asking about Hibernate Sessions that span for the length of an application transaction and the answer always seems to be: "but when will we know at what point to close the Hibernate Session?"

    Does SWF alleviate this issue by adequately demarcating the beginning and the end of an application transaction? With SWF as a first class member of the Spring family is it conceivable that we'll see a long session implementation that relies on flows?

  2. #2
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    Putting the Hibernate session in flow scope would easily give you 'flow scoped long Hibernate sessions'. You'd have to combine this with a FlowExecutionListener to connect/disconnect the session, e.g. using the requestSubmitted & requestProcessed hooks. With SWF it would probably be the flow itself that decides when to commit and/or discard the session, e.g. using an action that fires when you submit the work done in the flow.

    Erwin

  3. #3
    Join Date
    Nov 2004
    Location
    Austin, Texas USA
    Posts
    46

    Default

    Quote Originally Posted by klr8
    Putting the Hibernate session in flow scope would easily give you 'flow scoped long Hibernate sessions'. You'd have to combine this with a FlowExecutionListener to connect/disconnect the session, e.g. using the requestSubmitted & requestProcessed hooks. With SWF it would probably be the flow itself that decides when to commit and/or discard the session, e.g. using an action that fires when you submit the work done in the flow.

    Erwin
    I had the same thought regarding implementation. As it happened, I also needed to write a new continuation storage class as well. Hibernate Sessions don't work so well after they've been serialized.

    Thanks for the suggestion.
    -Alex

  4. #4
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    That's bizar, since the Session interface implements Serializable and the JavaDoc states 'A Session instance is serializable if its persistent classes are serializable'.

    Hibernate in Action also describes a long session scenario where the session is stored in the HttpSession.

    Erwin

  5. #5
    Join Date
    Nov 2004
    Location
    Austin, Texas USA
    Posts
    46

    Default

    Yes. You're quite right. Looks like I should have gone to bed sooner.

  6. #6
    Join Date
    Dec 2004
    Location
    CA
    Posts
    208

    Default

    Quote Originally Posted by klr8
    Putting the Hibernate session in flow scope would easily give you 'flow scoped long Hibernate sessions'. You'd have to combine this with a FlowExecutionListener to connect/disconnect the session, e.g. using the requestSubmitted & requestProcessed hooks. With SWF it would probably be the flow itself that decides when to commit and/or discard the session, e.g. using an action that fires when you submit the work done in the flow.

    Erwin
    Hi Erwin, can you provide an example of how this can be done. I would greatly appreciate it. I am pretty sure other people may be interested in this aproach.

    All of my form objects (domain objects) are persisted using hibernate. Currently, I pass my form objects to a service layer and persistence is done there, openning/closing hibernate session and transaction.

    It would be nice to open up a hibernate session at a start of flow, modify the form objects within the session and at the end of the flow the session is closed and the form object is persisted.

    Curtney
    Curtney Jacobs

  7. #7
    Join Date
    Nov 2004
    Location
    Austin, Texas USA
    Posts
    46

    Default

    Erwin,

    I've implemented long sessions as FlowExecutionListener which uses Spring's TransactionSynchronizationManager for thread local storage of a Hibernate Session in much the same way that the OpenSessionInViewFilter works.

    The primary difference, of course, is that the Hibernate session remains open throughout the duration of a flow session, is connected and disconnected from the db with each request, and is serialized with the flow continuation. Additionally, Hibernate sessions can be shared with subflows by including the proper annotation in the parent flow's declaration.

    I'd be happy to submit it for your consideration for inclusion to SWF if you think that other users might find it an adequate solution.

    -Alex

  8. #8
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    Start by posting the code here on the forum, that way other users can already have a look!

    One potential danger to look out for: what if the user doesn't correctly finish the flow: e.g. he closes his browser window in the middle of the flow. In that case the Hibernate session will only be cleaned up when the flow or HttpSession expires.

    Erwin

  9. #9
    Join Date
    Nov 2004
    Location
    Austin, Texas USA
    Posts
    46

    Default

    Quote Originally Posted by klr8
    Start by posting the code here on the forum, that way other users can already have a look!

    One potential danger to look out for: what if the user doesn't correctly finish the flow: e.g. he closes his browser window in the middle of the flow. In that case the Hibernate session will only be cleaned up when the flow or HttpSession expires.

    Erwin
    OK. I'll post it here.

    I don't think it's necessary to explicitly close a Session as long as it's disconnected. In that respect, it was my intention that this code leave the Session in a 'terminated' state after each request. As far as orphaned Sessions go, I agree with you. The use of a flow cleanup filter is highly recommended and would be the only thing preventing severe memory bloat on the server resulting from abandonded flows that contain Session references.

  10. #10
    Join Date
    Dec 2004
    Location
    CA
    Posts
    208

    Default

    Will the code be posted to this forum?

    Curtney
    Curtney Jacobs

Similar Threads

  1. OpenSessionInView and portlet support
    By garpinc2 in forum Web Flow
    Replies: 31
    Last Post: Apr 9th, 2010, 11:12 AM
  2. Is a 'singleton' flow a bad idea?
    By akw in forum Web Flow
    Replies: 4
    Last Post: Oct 6th, 2005, 01:16 AM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 9
    Last Post: Sep 25th, 2004, 12:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •