Announcement Announcement Module
Collapse
No announcement yet.
Spring/Hibernate long session support or best practice? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring/Hibernate long session support or best practice?

    Hi,

    I've searched the Hibernate support forum for answers to my post on LazyInitializationException in a Swing application (see http://forum.springframework.org/showthread.php?t=16653) and found the following thread: http://forum.hibernate.org/viewtopic...ationexception

    As an essence, it was suggested to use long Hibernate sessions for Swing applications with well well-defined points at which to close the session to release memory.

    Is there support for long sessions in Spring or is it considered in any way?

    If not: what are the best practices to build a custom workaround for this?

    We are very happy with Spring so far but since we do not have a web application but a Swing like application (using ULC http://www.canoo.com/ulc), this is a serious problem for us.

    Thanks and regards,

    Michael
    Last edited by robyn; May 15th, 2006, 06:09 PM.

  • #2
    Having the same problem

    Hi,

    I also have a fat client swing application where I would like to use spring + hibernate with lazy load.
    I am able to use the "org.springframework.transaction.interceptor.Trans actionProxyFactoryBean" but without lazy loading.
    I am also able to use lazy loading, but I have to explicity declare transactions inside the class reference by "org.springframework.aop.framework.ProxyFactoryBea n".

    But I would like an approache where I define the transaction (Require, supports, ...) by the methodes (like the TransactionProxyFactoryBean) and also enable lazy loading.
    For the moment I am not able to do this because I often get exceptions on lazy loading the the sessions was closed.

    Do you have found any solution to your problem ??

    Comment


    • #3
      Hi,

      no, I haven't found a solution so far.

      I have searched a lot, but it seems that there is no solution around, at least none of which is described publically. The few hints I have found are very vague and often using the OpenViewInSessionPattern in web applications. It is said that this pattern should be used in a Swing application also, but a concrete solution (or a hint where to enhance Spring or wire something) is not given there.

      Maybe others are only using Spring in a web application context or don't want to share their experiences on this issue.

      Even the gurus don't seem to have a good answer.

      I'd appreciate any information on this issue and will report if I have a hint.

      Regards,

      Michael

      Comment


      • #4
        I don't think people are hiding anything really. I believe the problem is that not many people are writing swing apps these days and, additionally, swing apps are wild beasts compared to web apps. They spawn their own threads, have no firm structure, no common api one can use to demarcate transaction/session boundaries. Anyway, any pure swing solution would be useless to you as you are writing an ulc app.

        Comment


        • #5
          Hi dejanp,

          thanks for your suggestions.

          I hope that digging the problem will lead to a solution usable by ANY application which is not a web application. Mentioning Swing should only make the discussion easier because I think that ULC is not so common as Swing is. The problem is the same in both Swing and ULC, I think.

          However, I will also try to discuss this issue with the ULC developers. The client/server communication is done through http/https as in a web application, maybe there's a chance for a similar approach like OpenSessionInView for ULC.

          I think that this issue deserves attention and a standard (springy) solution even if there are more web applications developed these days. Spring is an excellent framework which should be usable outside a web application context also.

          Thanks and regards,

          Michael

          Comment


          • #6
            Hi,

            I'm currently working on a project using Spring and Hibernate with both a web based and a Swing based front end, providing different functionality, so yes I've come across the general problem.

            My basic solution (and I came to this conclusion before the swing interface was introduced) was to not use an OpenSessionInView pattern. It just caused me too many problems. I've seen this view mentioned by other people also.

            My default usage of Hibernate is to mark all collections lazy and all objects with proxies. Then, at my service layer, I have explicit methods of specifying the hydration level of the object graph based on my use case. I basically consider the hydration level part of the use case, rather than trying to ignore it with OpenSessionInView. In this way no more lazy load error, assuming I've got my join fetch commands written correctly in HQL. This also forces me to think about fetch strategies etc which I've found is needed for n+1 avoidance problems. A very nice side effect of this was when I added the swing interface I could use the same solution by just exposing my service interface remotely.

            More recently I've been using a more generic solution as I had the problem where some of my domain models required a large number of different hydration levels depending on where they were being accessed in the applications. So now some of my service layer methods have a hydration specification class as part of their method signiture. My dao's know how to interperate this hydration specification and create the required criteria in hibernate, and each domain object has a collection of valid hydration levels. The hydration specification passed to the service layer methods is simply two arrays of these hydration levels, one for prefetch and one for postfetch. I use OGNL as a way of specifying object graph levels that should be hydrated. Works pretty well.

            Jonny

            Comment


            • #7
              Hi Jonny,

              many thanks for your informative reply! It sounds very interesting.

              May I ask you to provide some code samples showing you hydration specification class and how it is used in your DAO's? This would be very helpful for me.

              I'll have a look at OGNL, I haven't used it before.

              Thanks again and kind regrads,

              Michael

              Comment


              • #8
                Michael,

                for sure, although I won't be able to get to the code till tomorrow (friday). Note that this only gives a method of being able to specify different hydration levels for the same object, not a way of ignoring hydration as with OpenSessionInView.

                I actually read through the other threads after replying to this one. I was interested to see if anyone was suggesting anything I wasn't aware of. I didn't see any. The solutions are either long sessions or explicit hydrations.

                Be aware, the one downside (if you want to call it that) of the explicit hydration approach is that you always need to thinking about what level you need your objects hydrated to at a particular instance in the application (I actually find that's useful). For example, I have a navigation tree that the user can drill down on. I currently will bring back the minimum needed for a closed leaf rendering (and any other information provided in linked views) and when the user opens the leaf I have to explictly refetch that one object with the new hydration level, replacing it in the tree. However, this gives me the ability to report I'm doing this to the user which would be harder with an implicit fetch from a long session.

                Jonny

                Originally posted by mbrohl
                Hi Jonny,

                many thanks for your informative reply! It sounds very interesting.

                May I ask you to provide some code samples showing you hydration specification class and how it is used in your DAO's? This would be very helpful for me.

                I'll have a look at OGNL, I haven't used it before.

                Thanks again and kind regrads,

                Michael

                Comment


                • #9
                  Hi Jonny,

                  many thanks for your efforts!

                  In my opinion, the need to think about what's going on is not necessarily a downside. It will help to build more elaborate applications.

                  As long as there's no intelligent mechanism in Hibernate/Spring to reattach objects to the Hibernate session automatically if needed, your approach seems to be a good workaround.

                  I'm looking forward to see the implementations.

                  Kind regards,

                  Michael

                  Comment


                  • #10
                    Michael,

                    can you send me an email with your return address and I can send you the code through attachments? There's too much to post here really.

                    jonny dot wray at fiveprime dot com

                    thanks
                    Jonny

                    Originally posted by mbrohl
                    Hi Jonny,

                    many thanks for your efforts!

                    In my opinion, the need to think about what's going on is not necessarily a downside. It will help to build more elaborate applications.

                    As long as there's no intelligent mechanism in Hibernate/Spring to reattach objects to the Hibernate session automatically if needed, your approach seems to be a good workaround.

                    I'm looking forward to see the implementations.

                    Kind regards,

                    Michael

                    Comment


                    • #11
                      Michael/Jonny
                      This is my first post to this forum.
                      Long time ...the last related post to this is almost a year back i.e 08/05/2005 , therfore would request you to update me on this...do you suggest something different or some better mechanism for the issue above or the solution is the best workaround for now too.

                      Would appreciate if you could share the code with me too .

                      I've already mailed Jonny on this.

                      Comment


                      • #13
                        Good job!
                        You solve the problem that bothers me a long time

                        Comment

                        Working...
                        X