Announcement Announcement Module
No announcement yet.
Hibernate 4 and Quartz - "No Session found for current thread" Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 4 and Quartz - "No Session found for current thread"

    I'm trying to set up Quartz using a JobStoreTX to keep my jobs and triggers. Right now I'm using the SchedulerFactoryBean to set up quartz and loading the initial job data with the XMLSchedulingDataProcessorPlugin.

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    	<property name="applicationContextSchedulerContextKey" value="applicationContext" />
    	<property name="configLocation" value="" />
    	<property name="dataSource" ref="dataSource" />
    	<property name="transactionManager" ref="transactionManager" />
    org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
    org.quartz.jobStore.driverDelegateClass =
    org.quartz.jobStore.tablePrefix = QRTZ_
    org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
    org.quartz.plugin.jobInitializer.fileNames = quartz_data.xml
    org.quartz.plugin.jobInitializer.failOnFileNotFound = true
    org.quartz.plugin.jobInitializer.scanInterval = 0
    org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
    		<description>Process blah...description>
    			<!-- every minute -->
    			<cron-expression>0 * * ? * *</cron-expression>
    In my com.blah.ScheduledJob class, I am getting the bean from the like so:
    public class ScheduledJob implements StatefulJob {
    	public void execute(JobExecutionContext context) throws JobExecutionException {
    		ApplicationContext ctx = context.getScheduler().getContext().get("applicationContext");
    		MyServiceInterface mbi = (MyServiceInterface) ctx.getBean(MyServiceInterface.class);
    Outside of Quartz and inside my spring application, my service implementation works fine.

    I know that Quartz is creating a new instance of my ScheduledJob class in a new thread. That's why I was using the getBean() method to get my service implementation which I thought was already set up with the datasource and transaction manager, but when my service tries to do any database access from Scheduledjob, it throws a the No Session found for current thread exception.

    Do I need to set up the trigger and job as beans instead of using the xml file? Wouldn't I still have the issue with the thread? I saw some posts about a using a HibernateInterceptor somehow, but I also saw that it doesn't exist for Hibernate 4.

    I'm very frustrated - any suggestions?

  • #2
    For anyone interested, I found this:

    Someone attached a HibernateInterceptor for Hibernate 4. I know it's not an official version, and it doesn't support Hibernate Filters, but I've given it a try and it seems to be working so far. I'll use this for now and look for it in an official release.