grails / hibernate:在Job中获取org.hibernate.StaleStateException

时间:2016-08-30 08:46:14

标签: 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; 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)
            }
        }       
    }
}

1 个答案:

答案 0 :(得分:0)

省略withSession的东西并将交易代码放入服务中?!

另外,在保存时启用failOnError有时会导致出现根错误...