2016-07-07 28 views
0

我在Grails應用程序下面的石英工作:石英工作Grails的不能更新記錄

class DataWarehouseJob { 

    def execute(context) { 

     LeadList list = context.mergedJobDataMap.get('list') 

     if (list.processing) { 
      return 
     } 

     list.processing = true 
     list.save(failOnError: true) 

     ... 

    } 
} 

list作爲工作triggerNow調用的參數傳遞和它填充了正確的值。該列表已經存在於數據庫中。然而,當它試圖保存列表中,會出現以下錯誤:

org.quartz.JobExecutionException: org.springframework.dao.DuplicateKeyException: A different object with the same identifier value was already associated with the session : [haystack.LeadList#169]; nested exception is org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [haystack.LeadList#169] 
    at grails.plugins.quartz.GrailsJobFactory$GrailsJob.execute(GrailsJobFactory.java:111) 
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) 
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) 
Caused by: org.springframework.dao.DuplicateKeyException: A different object with the same identifier value was already associated with the session : [haystack.LeadList#169]; nested exception is org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [haystack.LeadList#169] 

... 

Caused by: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [haystack.LeadList#169] 
at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:618) 
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:301) 
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:244) 
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109) 
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90 
... 

我曾嘗試加入flush: true作爲參數保存功能,調用列表merge保存之前,註釋類@ExecuteInJTATransaction,節約LeadList withNewSession,但沒有幫助 - 相同的錯誤總是發生在同一行(list.save

最奇怪的是,此代碼之前工作,我不知道哪些更改可能會打破它 - 有沒有改變觸及這個類在相當一段時間。

代碼編寫Grails的3.1.5和Tomcat上8

回答

0

我不想刪除的問題上運行,因爲解決方案可能受益的人。

問題是因爲工作接受了LeadList對象,我想它創建了它的副本而不是通過引用傳遞對象,這就是導致問題的原因。

解決方案是發送對象的ID作爲參數來工作,而不是整個對象,並在作業首先通過ID在數據庫中進行更改之前獲取對象:

// LeadList list = context.mergedJobDataMap.get('list') 
long listId = context.mergedJobDataMap.get('listId') 
LeadList list = LeadList.findById(listId) 

不過,我不知道如何傳遞整個對象,然後在某個時刻突然出現問題。