Quartz SchedulerFactoryBean异常--Spring自动装配惹的祸

Spring的配置文件,启用了自动装配模式:
<beans default-autowire="byName">


<bean id="dataSource" class="..."></bean>


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
  <property name="triggers">
   <list><ref bean="clearLogsTrigger" /></list>
  </property>
 </bean>


  ...


</beans>


则此时会产生如下错误:


Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in file [E:\workspace\eis_oracle2\web\WEB-INF\classes\conf\spring\eisSystemScheduleContext.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: ORA-00942: table or view does not exist


原因:其实这都是Spring的自动装配惹得祸,当存在dataSource的bean,在初始化SchedulerFactoryBean时就自动装配该dataSource属性,此时就启用了jobstore的LocalDataSourceJobStore模式使用数据库来维持状态,quartz的jobstore会从数据库中查询任务。quartz使用数据库进行job状态的维护,但是在数据库中没有找到相应的table,从而无法正常初始化scheduler 。






解决:屏蔽掉自动装配,如下:


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
  <property name="triggers">
   <list><ref bean="clearLogsTrigger" /></list>
  </property>

 </bean>


详细介绍请参见 SchedulerFactoryBean 方法 setDataSource(DataSource)的javadoc: 
Set the default DataSource to be used by the Scheduler. If set, this will override corresponding settings in Quartz properties. 


Note: If this is set, the Quartz settings should not define a job store "dataSource" to avoid meaningless double configuration. 


A Spring-specific subclass of Quartz' JobStoreCMT will be used. It is therefore strongly recommended to perform all operations on the Scheduler within Spring-managed (or plain JTA) transactions. Else, database locking will not properly work and might even break (e.g. if trying to obtain a lock on Oracle without a transaction). 


Supports both transactional and non-transactional DataSource access. With a non-XA DataSource and local Spring transactions, a single DataSource argument is sufficient. In case of an XA DataSource and global JTA transactions, SchedulerFactoryBean's "nonTransactionalDataSource" property should be set, passing in a non-XA DataSource that will not participate in global transactions.


动装配就是指由Spring来自动地注入依赖对象,无需人工参与。 

目前Spring3.0支持“no”、“byName ”、“byType”、“constructor”四种自动装配,默认是“no”指不支持自动装配的,其中Spring3.0已不推荐使用之前版本的“autodetect”自动装配,推荐使用@Autowired注解方式代替;如果想支持“autodetect”自动装配,请将schema改为“spring-beans-2.5.xsd”。 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值