Results 1 to 6 of 6

Thread: OpenSessionInViewFilter and Threads

  1. #1

    Default OpenSessionInViewFilter and Threads

    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.

    Any help would be great appreciated. Many Thanks in advance!

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    The Session created by OSIV is going to be in a different thread to the dao call? Session isn't thread-safe so passing it between threads might not be the best of ideas.
    Last edited by karldmoore; Aug 29th, 2007 at 12:11 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

    Hi,

    I am also encountering the same problem in my application.

    My env:
    Websphere Application Server 6
    Spring 1.2.8

    The flow is like this.

    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).

    I am using OSIV filter and the configuration in web.xml is

    Code:
    <filter>
            <filter-name>hibernateFilter</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>hibernateFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    Following is the exception stack :
    Code:
    [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

  4. #4

    Default

    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?)

  5. #5
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by julien.dubois View Post
    When using the "Open Session in View" filter, the Hibernate Session is attached to the current Thread (it's a ThreadLocal object).
    Not only OSIV, but the whole Spring transaction management works thread-bound.

    Jörg

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    The other option is to begin a transaction within the threads execution and then reattach the entity to that Session.
    Last edited by karldmoore; Aug 29th, 2007 at 12:11 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

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