Results 1 to 9 of 9

Thread: Quartz and Hibernate - Handling Session Management?

  1. #1
    Join Date
    Nov 2004
    Posts
    21

    Default Quartz and Hibernate - Handling Session Management?

    Greetings,

    Since there isn't a scheduler forum, I thought I would drop a question in here. I have a spring-based application using hibernate for data storage. For normal requests, I run through the OpenSessionInViewFilter for handling the Hibernate session, and I use the AOP interceptor for handling the transaction control for normal DAO access.

    However, I want my Quartz Jobs to also have this access. What is recommended for getting the same sort of Session management and transaction control for jobs being executed?

    I've looked around for some examples in this area, but haven't really seen it covered too much. So, any pointers or feedback?

    thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    You can pass references to your transactional Hibernate DAO proxies into the Quartz jobs via the job detail map.

    Rob

  3. #3
    Join Date
    Nov 2004
    Posts
    21

    Default

    Ok.. I'm back on this problem again, and I would like to ask a followup on this. I have some DAO's and some service's defined in my spring application. I create a Quartz scheduler, and I supply one of the services to my Job object in the map. So far, so good.

    My service is supplied with DAO's, and those DAO's are defined through the transactional proxy, by using it as their parent. Generally, this works perfectly fine.

    What happens is.. in my Quartz job, if I pull in some data, it works immediately. But if I then access a lazy-loaded attribute, it blows up on a "no session or session was closed" error. Similar to if I didn't have a OpenSessionInView filter and tried to access that in JSP..

    So, I have that transaction proxy ok.. but how do I manage the Hibernate session for the duration of the quartz job execution?

    thanks!

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    but how do I manage the Hibernate session for the duration of the quartz job execution?
    Quartz/cron jobs by default have their own start-up times which doesn't always match the application workflow. If you are using lazy-init entities inside your Quartz Job then I suggest you initialize them eagerly either through a FETCH or using Hibernate.initialize().

    Another approach (which I use) it to put all the logic inside some DAO or BusinessObject and the job simply fires them up (calls them). However if you are using OpenSessionInViewFilter you can't simulate very well a web request so it's best to use OpenSessionInViewInterceptor around your methods.
    I haven't used the interceptor and filter around the same method so I can't say if they are going to use the same session or they will both use their own threadlocal.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Nov 2004
    Posts
    21

    Default

    I could push more logic down in there, though it wouldn't be my first choice. I'm curious though.. couldn't you build an "OpenSessionInView" type of functionality for a Job task? So, with the Job lifecycle open the session, kick off the job, and then flush it off at the end, based on the behavior? Would there be a reason not to do this?

    I really wouldn't want to fetch my lazy associations eagerly, since it could pull in a fairly large tree of data, and that wouldn't be good.

    Thanks for the information!

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Repeating myself, why don't you put the logic inside a bean - define the OpenSessionInView interceptor around it and call the bean from within the job. If you have several methods use a facade and put the interceptor on that one.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default Same problem

    Hi, i have the same problem in our application we want to run a repetitive task and inside this task accessing via facade interface a method that load (using DAO) an object with a lazy list, we are usin the HibernateTransactionManager and OpenSessionInView interceptor but in this case this interceptor donīt work because the job don't run in a view threat and the list isn't populated, the question is, what can i use to intercept the repetitive job method and pass and manipulate the hibernate session correctly, we are seeking in this forum and spring-reference.

    Any help will be preciated
    Rodney
    Rodney Gallart
    Radikal Systems
    http://www.radikalsystems.com

  8. #8
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Just use AOP - you decouple the method inside a different class/bean and wrap the OpenSessionInViewinterceptor around it - no matter when the job is fired, the interceptor will take care of the session handling.
    If you have the case where your object is too big to be lazy initialzed all at once then you can use different approaches:
    1. hold a reference (id) of the object and make a query for it in each method - that is keep a weak reference.
    2. decouple the object and at the beginning at each method attached it to a newly opened session.

    You can take care of the session this way either through the interceptor or, if you don't need the a thread-local session, use directly hibernateTemplate.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  9. #9
    Join Date
    Oct 2004
    Location
    Havana, Cuba
    Posts
    25

    Default

    Sorry, but i don't understand your post, i'm relatively new with spring, if you can post a small example to illustrate that will be very useful.

    Regards. Rodney
    Rodney Gallart
    Radikal Systems
    http://www.radikalsystems.com

Similar Threads

  1. ISOLATION_SERIALIZABLE and Hibernate Lists
    By maxjar10 in forum Data
    Replies: 3
    Last Post: Jan 12th, 2006, 10:12 AM
  2. Replies: 5
    Last Post: Jun 10th, 2005, 03:21 AM
  3. Quartz - Couldn't retrieve job
    By newreaders in forum Container
    Replies: 4
    Last Post: Jun 1st, 2005, 08:12 PM
  4. Replies: 1
    Last Post: Apr 6th, 2005, 04:08 PM
  5. Replies: 2
    Last Post: Mar 30th, 2005, 12:13 AM

Posting Permissions

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