在我的Grails项目中,我有一个石英(plugin:quartz2:2.1.6.2,但我测试过插件:quartz:1.0-RC7,但问题没有改变)(grails 2.2.1)。Quartz作业停止在StaleObjectStateException异常处执行
我有这样的执行方法本
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 occour工作。对于我的逻辑来说,这是可以的,我使用grails乐观锁,并且这种例外每周只发生一次。
问题是当这个异常发生时,Job停止再次发射。
我试着在try catch和flush hibernate session内部包装方法代码来捕获异常但没有财富。我的任何捕捉都没有捕捉到这个例外。
在网上查找我发现这是一个old grails quartz bug但它是固定的,无论如何使用try {} catch必须绕过该错误。
P.S. 的作业从bootstrab通过这种类型的呼叫计划
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)
在你的代码示例你不要再追“org.hibernate.StaleObjectStateException”,所以我想这是正常的工作失败... – moskiteau 2013-05-14 13:06:31
@moskiteau org.hibernate.StaleObjectStateException是org.hibernate.HibernateException – 2013-05-14 13:16:31