How to get around LazyInitializationException in scheduled jobs?

Posted by Shreerang on Stack Overflow See other posts from Stack Overflow or by Shreerang
Published on 2010-06-07T19:59:19Z Indexed on 2010/06/07 20:02 UTC
Read the original article Hit count: 198

Filed under:
|
|

I am working on a J2EE server application which is deployed on Tomcat. I use Spring source as MVC framework and Hibernate as ORM provider. My object model has lot of Lazy relationships (dependent objects are fetched on request). The high level design is like Service level methods call a few DAO methods to perform database operation. The service method is called either from the Flex UI or as a scheduled job. When it is called from Flex UI, the service method works fine i.e. it fetches some objects using DAO methods and even Lazy loading works. This is possible by the use of OpenSessionInViewFilter configured with the UI servlet.

But when the same service method is called as scheduled Job, it gives LazyInitializationException. I can not configure OpenSessionInViewFilter because there is no servlet or UI request associated with that.

I tried configuring Transaction around the scheduled job method so that service method starts a transaction and all the DAO methods participate in that same transaction, hoping that the transaction will remain active and hibernate session will be available. But it does not work.

Please suggest if anyone has ever been able to get such a configuration working. If needed, I can post the Hibernate configuration and log messages.

Thanks a lot for help!

Shreerang

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about spring