Using a Hibernate Quartz Session

I have a web application that uses a framework like Struts and Hibernate. I am currently developing a scheduler for this application using Quartz. During coding, I realized that using a Hibernate session is not possible with Quartz streams.

Does anyone have a solution for using sleep mode sessions from the quartz job class?

+5
source share
4 answers

One approach is to use a class HibernateUtilthat builds SessionFactoryin a static initializer and makes it accessible through a public staticgetter. Your task is to create Quartz Sessionlike HibernateUtil.getSessionFactory().getCurrentSession()and use it.

+3
source

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

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

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

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

I apologize if this does not fix your specific problem, but I suspect that it will catch someone in the future.

+3
source

A search for "Quartz Hibernate" returned this. Having come to a different solution (and using Tapestry), I thought I would share it.

when planning a task:


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 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 someone can use this.

+1
source

All Articles