Announcement Announcement Module
Collapse
No announcement yet.
Quartz and Hibernate - Handling Session Management? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    You can pass references to your transactional Hibernate DAO proxies into the Quartz jobs via the job detail map.

    Rob

    Comment


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

      Comment


      • #4
        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.

        Comment


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

          Comment


          • #6
            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.

            Comment


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

              Comment


              • #8
                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.

                Comment


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

                  Comment

                  Working...
                  X