Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter and Threads Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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, 12:11 PM.

    Comment


    • #3
      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

      Comment


      • #4
        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?)

        Comment


        • #5
          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

          Comment


          • #6
            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, 12:11 PM.

            Comment

            Working...
            X