Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 4 and Quartz - "No Session found for current thread" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    application-context.xml
    Code:
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    	<property name="applicationContextSchedulerContextKey" value="applicationContext" />
    	<property name="configLocation" value="classpath:quartz.properties" />
    	<property name="dataSource" ref="dataSource" />
    	<property name="transactionManager" ref="transactionManager" />
    </bean>
    quartz.properties
    Code:
    org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
    org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
    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
    quartz_data.xml
    Code:
    <schedule>
    	<job>
    		<name>scheduledJob</name>
    		<group>DEFAULT</group>
    		<description>Process blah...description>
    		<job-class>com.blah.ScheduledJob</job-class>
    		<volatility>false</volatility>
    		<durability>false</durability>
    		<recover>false</recover>
    	</job>
    
    	<trigger>
    		<cron>
    			<name>scheduledTrigger</name>
    			<group>DEFAULT</group>
    			<job-name>scheduledJob</job-name>
    			<job-group>DEFAULT</job-group>
    			<!-- every minute -->
    			<cron-expression>0 * * ? * *</cron-expression>
    		</cron>
    	</trigger>
    </schedule>
    In my com.blah.ScheduledJob class, I am getting the bean from the like so:
    Code:
    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);
    		mbi.doDatabaseStuff();
    	}
    }
    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: https://jira.springsource.org/browse/SPR-9028

    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.

    Comment

    Working...
    X