Spring Quartz *.QRTZ_LOCKS' doesn't exist

来源:互联网 发布:淘宝的ipad为什么便宜 编辑:程序博客网 时间:2024/06/05 08:27

在使用 Spring OpenSymphony Quartz 做计划任务时,遇到错误:

 

ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: Table 'zp60v1_db.QRTZ_LOCKS' doesn't exist [See nested exception: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'zp60v1_db.QRTZ_LOCKS' doesn't exist]]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean

....

 

开发环境:
JDK 1.6
GWT 2.0
SmartGwt pro 2.0
Hibernate 3.2
Spring 2.5

 

applicationContext.xml:

 

为了定时执行一项任务,设置了Quartz , 但是运行后异常" Failure obtaining db row lock: Table ‘hibernate.qrtz_locks’ doesn’t exist“,相信你肯定会感到困惑,甚至也许会奇怪为什么 SchedulerFactoryBean 和数据持久层有关。

 

原因:

事实上,这个异常是由于 Spring 的 autowire 属性引起的,SchedulerFactoryBean  类含有方法 : setDataSource. 因为autowire 属性使用了 autodetect , 并且也设置了 datasource 在项目中, spring 容器就自动将 dataSource 注入到SchedulerFactoryBean, 而SchedulerFactoryBean将从 dataSource 中查找计划任务。 但 dataSource 中并没有任务,因此抛出了异常。

幸运的是,当你知道了根本原因,就知道如何避免了。

 

解决方法:

不论 spring 的 default-autowire 设置为"autodetect " 还是 "byName" ,都会出现 *.QRTZ_LOCKS' doesn't exist

方法一: 不使用 default-autowire 属性;

方法二: 在不改变 spring default-autowire  属性的前提下, 给 SchedulerFactoryBean  设置 autowire="no"。