This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

OpenSessionInViewFilter and Threads

Apr 19th, 2007, 06:22 AM

We are using Spring along with Hibernate to enable the Data Access layer. The OpenSessionInViewFilter is used to tie the Hibernate Session to the lifetime of the request. This works fine when the DAO processing is complete within the request lifetime.

There is a case when we start a new background Thread from the view with a long running process that uses the DAOs and the response is returned back to the client completing the request. This is when the background Thread throws the 'LazyInitializationException'.

Is there a way to propagate the Session opened in the view down to the background Thread? I am not sure if opening a transaction before starting the Thread would help.

1. User browses for a file and clicks Upload.
2. The control now goes to a struts action.
3. In the action class a new thred will be started and the file will be passed
to the thread for further processing.
4. Once the thread starts the action will forward the request to a success jsp (irrespective of thread completion status).

[6/17/07 11:35:00:906 IST] 00000117 LazyInitializ E org.hibernate.LazyInitializationException <init> could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.alter.model.ReferenceCode$$EnhancerByCGLIB$$2b0c448c.attLOV(<generated>)
at com.alter.util.FileUploadCheckHelper.checkUploadMap(FileUploadCheckHelper.java:280)
at com.alter.util.FileThread.run(FileThread.java:75)

Any help will be greatly appreciated...
Thanks in advance.

- Rakesh

Comment

When using the "Open Session in View" filter, the Hibernate Session is attached to the current Thread (it's a ThreadLocal object).
So if you launch a new Thread, you won't be able to benefit from this filter, hence the "LazyInitializationException" problem.
Of course you could pass the Hibernate Session as a reference to this new Thread, but as karldmoore said it is a Bad Idea, as it is not a thread-safe object.

There are 2 solutions to this problem :
- Do not use lazy loading in your new Thread. You can force the instanciation of lazy-loaded collections using Hibernate.initialize()
- Use message-driven beans. It's the normal J2EE way to launch threads (in EJBs you cannot launch new threads, according to the spec), and my favorite solution. It will also prevent your application from running out of threads (which is one of the problems of your design : what happens if your system slows down and that a lot of users start to launch new Threads?)