我在我的 Grails 项目( grails 2.2.1 )上遇到了石英问题(插件 :quartz2:2.1.6.2 但我甚至使用插件 :quartz:1.0-RC7 进行了测试,但问题没有改变)。
我有这样的工作
class MyJob {
def concurrent = false
def execute(context){
try {
//....
// works with domains .....
myDomain.save(flush: true)
// works with domains .....
//....
sessionFactory.currentSession.flush()
} catch (org.springframework.dao.OptimisticLockingFailureException olfe) {
println "Job failed by database exception "
} catch ( org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException ole){
println "Job failed by database exception "
} catch ( org.hibernate.HibernateException hibe ){
println "Job failed by database exception "
}
}
}
}
有时在执行方法中会出现 StaleObjectStateException。这对我的逻辑来说没问题,我正在使用 grails 乐观锁定,并且这个异常每周只发生一次。
问题是当此异常发生时,作业停止再次触发。
我已经尝试将方法代码共同包装在 try catch 中并在内部刷新休眠会话以捕获异常但没有运气。我的任何捕获都没有捕获异常。
在网上查找,我发现这是一个旧的 grails 石英错误,但已修复,无论如何使用 try{}catch 必须绕过该错误。
PS 该作业是通过这种类型的调用从引导程序调度的
MyJob.schedule( 10000L )
停止调度的异常是
[194949896] core.ErrorLogger Unable to notify JobListener(s) of Job that was executed: (error will be ignored). trigger= DEFAULT.MT_3tbn6lewgiqa3 job= DEFAULT.MyJob
org.quartz.SchedulerException: JobListener 'persistenceContextJobListener' threw exception: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [MyDomain#42] [See nested exception: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [MyDomain#42]]
at org.quartz.core.QuartzScheduler.notifyJobListenersWasExecuted(QuartzScheduler.java:1939)
at org.quartz.core.JobRunShell.notifyJobListenersComplete(JobRunShell.java:361)
at org.quartz.core.JobRunShell.run(JobRunShell.java:235)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [MyDomain#42]
at grails.plugin.quartz2.PersistenceContextJobListener.jobWasExecuted(PersistenceContextJobListener.groovy:46)
at org.quartz.core.QuartzScheduler.notifyJobListenersWasExecuted(QuartzScheduler.java:1937)
... 3 more
.....
events.PatchedDefaultFlushEventListener Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [MyJob#42]
at MyJob.execute(MyJob.groovy:354)
at grails.plugin.quartz2.GrailsArtefactJob.execute(GrailsArtefactJob.java:57)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)