Apr 3rd, 2008, 03:57 AM
Spring usage in a desktop application
I am using Spring-Hibernate in a desktop application. No one in my team (including me) is not a good expert on either technology.
I would like to have your opinion on a possible usage of Spring, which I am contemplating.
I would like to use Spring's transaction management on my services layer (i.e. I want to put a transation boundary on my services layer methods) but I dont want to close the Hibernate session when the boundary or method is exited. My idea is to keep the session open for the currently logged in user (only one at a time since it is a desktop application) for the entire length he uses the application. This way I save myself and my team from botherations of dirty objects, session attachment etc. It will be like a OpenSessionInView pattren of Hibernate.
Please suggest me how I can acheive this and if there are any pitfalls with this approach.
Apr 3rd, 2008, 04:18 AM
If you are really intending to keep the session open remember
1) Your session can become large, the 1st level cache grows and grows
2) You might end up with dirty objects, objects that are changed in the database but which aren't getting synchronized with the 1st level cache (i.e. you see an old state whilst the database is already updated).
You can simply open a Session at the start of your application and bind it to the current thread (swing thread) that way it will remain open. However you have to be aware of the pitfalls I mentioned.
Apr 3rd, 2008, 04:31 AM
If I am not able to express my query earlier, then it is here that I want.
Using HibernateTransactionManager, sequence is like:
The sequence is like this:
Hibernate session opened and bound to the current thread
Actual method execution
Hibernate session bound to the current thread closed
I dont want to do step 4 i.e. I dont want to unbound the session from the thread so that I can use the same session again.
Is there any straight forward way to do this or is there any way I can re-associate the same session with the current executing thread, after it has been unbound.
Apr 3rd, 2008, 04:40 AM
Have you read my post?!
I never say that it cannot be done? I just point out that you need to handle a couple of issues...
I also gave you the solution. Open a session and register it with the thread (check the OpenSessionInViewFilter/Interceptor) how it is done there. You want to do that at the start of your application and close/cleanup the session at the end. Exactly what the OSIVF does.
Apr 3rd, 2008, 07:16 AM
I read your post after I replied to my query. I understand your solution completely. However, i also got to know of a solution with spring-aop and HibernateInterceptor.
I know it is a different question. But it will be great if I can have your comments on this one.