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:

  1. <beans default-autowire=”autodetect”> <!–autowire is the cause of exception–>
  2. <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
  3. ……
  4. </bean>
  5. <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
  6. ……
  7. </bean>
  8. …….
  9. <bean id=”cronReportTrigger” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
  10. ……
  11. </bean>
  12. <bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
  13. <property name=”triggers”>
  14. <list>
  15. <ref bean=”cronReportTrigger”/>
  16. </list>
  17. </property>
  18. </bean>
  19. </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"。

  1. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
  2. <property name="triggers">
  3. <list>
  4. <ref bean="simpleTriggerBean" />
  5. </list>
  6. </property>
  7. </bean>
 
 
  • 原理:

<bean id="scheduler" lazy-init="false"

      class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
此bean会试图访问数据库获取quartz的一些管理表信息,自然访问数据库时需要注入dataSource bean,当缺省autowire为no,则没有dataSource bean被注入,quartz会认为项目没连数据库,会BYPASS这个访问管理表的功能.
当你配置了default-autowire=byName时,dataSource bean被自动注入,这时quartz认为项目既然能连到数据库,就想当然的认为对应的那些表一定存在,没找到时就出异常.

  • 解决办法:

1.去掉default-autowire=byName即可
此法简单,但往往很难决定,因为缺省,谁也不会傻乎乎的显示配这么一条,配了它一定是有用到它的地方.你愿不愿意牺牲这部分byName注入的功能?

2.在库中建对应的表
此法不可取,因为非常麻烦,要建很多表
CREATE TABLE QRTZ_LOCKS
CREATE TABLE QRTZ_JOB_DETAILS
CREATE TABLE QRTZ_TRIGGERS
CREATE TABLE QRTZ_FIRED_TRIGGERS
CREATE TABLE QRTZ_JOB_LISTENERS
少一张,spring都报异常, 这是为大型调度功能准备的.你要有上百个任务,可能需要它.

3.bean里直接关掉autowired
推荐此法

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
          default-autowire="byName"
           > 
<bean id="scheduler" lazy-init="false" autowire="no"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
 

Spring Quartz *.QRTZ_LOCKS' doesn't exist的更多相关文章

  1. spring quartz分布式任务计划

    spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...

  2. Spring+quartz 实现定时任务job集群配置

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  3. Spring+quartz 实现定时任务job集群配置【原】

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  4. 【示例】Spring Quartz入门

    JAVA 针对定时任务,有 Timer,Scheduler, Quartz 等几种实现方式,其中最常用的应该就是 Quartz 了. 一. Quartz的基本概念 在开始之前,我们必须了解以下的几个基 ...

  5. 基于spring+quartz的分布式定时任务框架

    问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...

  6. Spring Quartz

    Spring  Quartz Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先我们来写一个被调度的类: pac ...

  7. [Spring] - Quartz定时任务 - Annotation

    Spring + Quartz可以使用annoation方式: 1.AppJob类: package com.my.quartz.testquartz1; import org.springframe ...

  8. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  9. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

随机推荐

  1. vue常见依赖安装

    1):$ npm install less less-loader --save 2)style里 <style lang='less'> 2): $ npm i vue-resource ...

  2. 信息领域热词分析系统--java爬取CSDN中文章标题即链接

    package zuoye1; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLExce ...

  3. springboot 启动的java进程默默终止

    首先说明这是一个灵异事件......... 场景1 :把之前用map实现的缓存用Redis重构,高高兴兴上线更新,10 分钟后,老板告诉我,项目停了,what ??? 像我这么帅气,英俊,聪明的人,更 ...

  4. ThreadLocal 开启事务

    1.ThreadLocal该类提供了线程局部变量 2.分析原理: ThreadLocal内部有一个Map.Map的key是当前线程对象,value是一个Object对象. 模拟该类: public c ...

  5. pat1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  6. Windows Server 2012 R2

    Windows Server 2012 R2 历史上的Server有2003 server, 2008 server, 2012 server windows server 2012 r2对计算机的消 ...

  7. 关于EasyUI datagrid 表头居中 数据列内容居右 或者居左

    cell.css("text-align",(col.halign||col.align||"")); 这里有个属性挺眼熟 : col.align 前面还有一个 ...

  8. Devexpress Xtrareport 并排报表

    什么是并排报表呢? 按照我个人理解:并排报表是把两张或者两张以上的报表,放在一个报表页面. 注:为了方便,本示例使用同一个数据源,但是您可以使用相同的方法,而在一个报表文档中显示两个完全不同的 (使用 ...

  9. hql基础入门

    [转]进入HQL世界 一个ORM框架是建立在面向对象的基础上的.最好的例子是Hibernate如何提供类SQL查询.虽然HQL的语法类似于SQL,但实际上它的查询目标是对象.HQL拥有面向对象语言的所 ...

  10. 在UITableView中识别左右滑动,实现上下翻页的功能

    目前有三种方案: 1. UIScrollView + UITableView. 实现方法,在UIScrollView中,加入UITableView即可 设置UIScrollView的代理和方法 - ( ...