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

  • OpenSessionInViewFilter alternative

    Hi, I created a project with Spring ROO that works perfectly fine.

    I created a Thread to do heavy background work in that application, but I keeo getting Lazy Loading Exceptions. I know web requests use the OpenSessionInViewFilter to manage that. The only way I hace found to make my thread work is to declare de working method Transactional and given that it has to do a time comsuming job, I don't like the idea of having the database blocked.
    ¿Which is supposed to be the best way to deal with this?
    I suppose that I has to be something like binding the session to the running Thread but I couldn't find any example of it.
    Any help would be appreciated.

    Regards.

  • #2
    Just because something is transactional doesn't mean that it has to block the database, it very much depends on the database, isolation levels and locking. The source for OSIVF might help you understand more about what you're trying to do. Essentially you just want a thread bound session. If you have a look at the Hibernate docs there is already the concept of [[http://www.dil.univ-mrs.fr/~massat/d...nContext.html][contextual sessions]].

    Comment


    • #3
      Originally posted by erzapito View Post
      I created a Thread to do heavy background work in that application, but I keeo getting Lazy Loading Exceptions. I know web requests use the OpenSessionInViewFilter to manage that. The only way I hace found to make my thread work is to declare de working method Transactional and given that it has to do a time comsuming job, I don't like the idea of having the database blocked.
      ¿Which is supposed to be the best way to deal with this?
      I suppose that I has to be something like binding the session to the running Thread but I couldn't find any example of it.
      Any help would be appreciated.
      Divide up your large job into smaller units-of-work and each one is a separate transaction.

      It sounds like you are defining your transaction boundary on the outer method of the entire run of the heavy background work. Where as what you should be doing it a lot of small bite-size transactions. This is how you stop your database being blocked.

      One of the side effects of declaring a transaction boundary is that it will keep the ORM session open while the transaction is still open, this is why the Lazy Loading Exception appears to go away.

      Lazy Loading Exceptions are the result of you trying to use some data originally returned from the ORM API but after the ORM session that origined it has been closed. What is probably happening when you see this exception is your ORM data lookup/access is being done in an implicit transaction (one that only exists to service the immediate request).

      By default there is no need to bind a session to a thread, this is actually automatically done by spring. You just need to move the boundary where the transaction start and ends, maybe just create another service, write a method that implements the outer method of your background job, inject the existing service you have with transational support enabled. Find a way to break down the units-of-work so methods on the inner service are invoked from the outer service multiple times (once per transaction). Do not instruct spring to do anything transactional with the outer service, this causes the outer loop of your workload to be outside of any transaction and call into the inner service to do one bite sized transaction. Hope I explained that well enough.

      Comment

      Working...
      X