Spring Quartz *.QRTZ_LOCKS' doesn't exist
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:
- <beans default-autowire=”autodetect”> <!–autowire is the cause of exception–>
- <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
- ……
- </bean>
- <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
- ……
- </bean>
- …….
- <bean id=”cronReportTrigger” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
- ……
- </bean>
- <bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
- <property name=”triggers”>
- <list>
- <ref bean=”cronReportTrigger”/>
- </list>
- </property>
- </bean>
- </beans>
为了定时执行一项任务,设置了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"。
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
- <property name="triggers">
- <list>
- <ref bean="simpleTriggerBean" />
- </list>
- </property>
- </bean>