grails/hibernate:在作业中获取org.hibernate.StaleStateException
我有一个非系统性的例外情况。grails/hibernate:在作业中获取org.hibernate.StaleStateException,hibernate,grails,Hibernate,Grails,我有一个非系统性的例外情况。 我试图通过在每次迭代中刷新和清理会话来解决这个问题,但没有成功 [quartzScheduler_Worker-7] ERROR jdbc.AbstractBatcher - Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; exp
我试图通过在每次迭代中刷新和清理会话来解决这个问题,但没有成功
[quartzScheduler_Worker-7] ERROR jdbc.AbstractBatcher - Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at bosmonitor.MyJob$_execute_closure1.doCall(MyJob.groovy:27)
at bosmonitor.MyJob.execute(MyJob.groovy:25)
at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:102)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
课程
class Product {
int duration = 0
int timer = 0
// ...
static mapping = {
version false
}
}
通过添加调用作业的服务来更新Poste
MyService
def ProductInstance = Product.get(1)
MyJob.schedule(1000L,0)
MyJob.triggerNow([ProductInstance:ProductInstance])
工作
class MyJob {
def sessionFactory
static triggers = {}
def execute(context) {
def ProductInstance = context.mergedJobDataMap.get('ProductInstance')
if (ProductInstance) {
// loop every second
while (ProductInstance.timer < ProductInstance.duration) {
def millis = System.currentTimeMillis()
Product.withTransaction {
ProductInstance.timer = ++ProductInstance.timer
ProductInstance.merge(flush: true)
def hibSession = sessionFactory.getCurrentSession()
hibSession.flush()
hibSession.clear()
}
Thread.sleep(1000 - millis % 1000)
}
}
}
}
classmyjob{
def会话工厂
静态触发器={}
def执行(上下文){
def ProductInstance=context.mergedJobDataMap.get('ProductInstance')
if(ProductInstance){
//每秒循环一次
而(ProductInstance.timer
省去withSession的东西,把事务代码放在服务中
此外,在保存时启用FailOneError有时有助于找到根本错误…省去withSession内容,将事务代码放入服务中
此外,在保存时启用FailOneError有时有助于找到根本错误…将服务注入到作业中,然后使用@Transactional服务为您完成作业如何?还有什么是context.mergedJobDataMap.get('ProductInstance')你能在def ProductInstance之后做一个println--ProductInstance是${ProductInstance.getClass()}并告诉每个人我用session替换了什么吗,但是异常仍然发生。您没有回答我上面提到的关于getClass的问题。如果您将代码放入服务中,然后通过控制器调用它进行测试,会发生什么情况?它是否在quartz计划之外工作正常?将服务注入到您的工作中,然后让@Transactional服务为您完成工作如何?还有什么是context.mergedJobDataMap.get('ProductInstance')你能在def ProductInstance之后做一个println--ProductInstance是${ProductInstance.getClass()}并告诉每个人我用session替换了什么吗,但是异常仍然发生。您没有回答我上面提到的关于getClass的问题。如果您将代码放入服务中,然后通过控制器调用它进行测试,会发生什么情况?它是否在quartz计划之外正常工作?