Using Hibernate session with quartz
Asked Answered
O

4

8

I've a web application which uses framework like Struts and Hibernate. Currently I'm developing a scheduler for this application using Quartz. While coding I realized that the use of Hibernate session is not possible with the threads of Quartz.

Anybody have a solution for using hibernate sessions from quartz job class?

Othilia answered 15/12, 2010 at 2:4 Comment(0)
M
4

One approach is to use a HibernateUtil class which builds the SessionFactory in a static initializer and makes it available via a public static getter. Your Quartz job can create a Session as HibernateUtil.getSessionFactory().getCurrentSession() and use it.

Mump answered 15/12, 2010 at 4:18 Comment(2)
My quartz job class is supposed to FTP to some remote servers and upload a large collection(millions) XML files to them. Is this the right method to follow for this scenario?Othilia
My code is producing lots of errors while running the hibernate from a quartz job. If I run it from outside the quartz, it works correctly and do all DB operations. What I need to change to make it working with quartz.Othilia
A
4

I know this is an old question, but I did a quick Google search, and this was the first hit.

In the quartz job, add this line at the start of the method:

public void execute(JobExecutionContext context) throws JobExecutionException
{
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //<-- this line

     //...your code here...
}

I apologize if this doesn't fix your specific issue, but I suspect it will catch someone in the future.

Aronson answered 21/11, 2014 at 13:34 Comment(1)
Thanks a million for your answer. I was facing this problem from last few days and your answer solve my issues.... Thanks a million againMonsour
F
2

Searching for "Quartz Hibernate" returned this. Coming to a different solution (and using Tapestry), I thought I'd share it.

when scheduling the Job:

…
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDataMap myJobDataMap = new JobDataMap();
myJobDataMap.put("HibernateSessionManager", hibernateSessionManager);
        myJobDataMap.put("PerthreadManager", perThreadManager);
JobDetail job = JobBuilder.newJob(SomeJob.class).withIdentity(
            "SomeJob", "someGroup").setJobData(
            myJobDataMap).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
            "Some Trigger", "someGroup").startNow().withSchedule(
            SimpleScheduleBuilder.repeatSecondlyForever(30)).build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
…

and in the Job

public void execute(JobExecutionContext context)
                throws JobExecutionException
{
    JobDataMap jdm = context.getMergedJobDataMap();
    HibernateSessionManager hibernateSessionManager = (HibernateSessionManager) jdm.get("HibernateSessionManager");
    PerthreadManager perThreadManager = (PerthreadManager) jdm.get("PerthreadManager");

    Session session = hibernateSessionManager.getSession();
    //do stuff with session …
    //now clean up, otherwise I ended up with <IDLE> in transactions
    perThreadManager.cleanUp();
}

Hope somebody can use this.

Faint answered 14/6, 2016 at 10:13 Comment(0)
S
1

You can look at the below link to see if it gives you a direction to follow. Since you are not using Spring, it might be hard to apply this directly

http://forum.springsource.org/showthread.php?t=12117

Stoop answered 15/12, 2010 at 2:15 Comment(1)
I can use spring in this case, as I've very small amount of code in Struts.Othilia

© 2022 - 2024 — McMap. All rights reserved.