Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hibernate session with quartz

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?

like image 370
Joe Avatar asked Dec 15 '10 02:12

Joe


2 Answers

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.

like image 178
Binil Thomas Avatar answered Oct 04 '22 05:10

Binil Thomas


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.

like image 21
user2529170 Avatar answered Oct 04 '22 06:10

user2529170