Grails w / GORM MongoDB插件:org.grails.datastore.mapping.core.OptimisticLockingException

时间:2014-12-11 15:03:17

标签: mongodb grails gorm gorm-mongodb

我有一个每隔几分钟就会运行的作业,一个新的域对象被实例化并填充来自api调用的数据,然后调用.save()来提交数据库。

此应用程序使用GORM Mongo插件。

在将新战争部署到测试框后,每次作业开始时我都会收到org.grails.datastore.mapping.core.OptimisticLockingException并尝试处理文件,进行api调用,然后保存结果。日志(& error)指向.save()调用。

在日志显示"解析文件内容"

后抛出错误
log.debug "Parsing file contents"
def map = [:]
def contents = new CsvParser().parse(fileContents, separator: '\t')
//Make a map of CSV contents
contents.each{ line ->
   map[line["name"]] = [:]
   line.columns.each{
      map[line["name"]][it.key[0].toLowerCase()+it.key.substring(1)]=line[it.key]
   }
   def thing = apiService.findThingDetailsByThingId(line["Thing_ID"].replaceAll( "[^\\d]", "" ))
   thing.save()
   map[line["name"]].putAll(thing.toMap())
}

下面的堆栈跟踪可能有点过分。

Heuristic completion: outcome state is rolled back; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Datastore transaction; nested exception is org.grails.datastore.mapping.core.OptimisticLockingException: The instance was updated by another user while you were editing
org.springframework.transaction.HeuristicCompletionException: Heuristic completion: outcome state is rolled back; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Datastore transaction; nested exception is org.grails.datastore.mapping.core.OptimisticLockingException: The instance was updated by another user while you were editing
   at org.grails.datastore.gorm.GormStaticApi.withNewSession(GormStaticApi.groovy:756)
   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)
Caused by: org.springframework.transaction.TransactionSystemException: Could not commit Datastore transaction; nested exception is org.grails.datastore.mapping.core.OptimisticLockingException: The instance was updated by another user while you were editing
   at org.grails.datastore.mapping.transactions.DatastoreTransactionManager.doCommit(DatastoreTransactionManager.java:156)
   ... 8 more
Caused by: org.grails.datastore.mapping.core.OptimisticLockingException: The instance was updated by another user while you were editing
   at org.grails.datastore.mapping.mongo.engine.MongoEntityPersister$5.doInDB(MongoEntityPersister.java:664)
   at org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.updateEntry(MongoEntityPersister.java:625)
   at org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.updateEntry(MongoEntityPersister.java:82)
   at org.grails.datastore.mapping.engine.NativeEntryEntityPersister$2.run(NativeEntryEntityPersister.java:862)
   at org.grails.datastore.mapping.core.impl.PendingOperationExecution.executePendingOperation(PendingOperationExecution.java:33)
   at org.grails.datastore.mapping.core.AbstractSession.flushPendingOperations(AbstractSession.java:364)
   at org.grails.datastore.mapping.core.AbstractSession.flushPendingUpdates(AbstractSession.java:343)
   at org.grails.datastore.mapping.core.AbstractSession.flush(AbstractSession.java:263)
   at org.grails.datastore.mapping.mongo.MongoSession.flush(MongoSession.java:126)
   at org.grails.datastore.mapping.transactions.DatastoreTransactionManager.doCommit(DatastoreTransactionManager.java:151)
   ... 8 more
org.springframework.transaction.HeuristicCompletionException: Heuristic completion: outcome state is mixed; nested exception is org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
   at org.grails.datastore.gorm.GormStaticApi.withNewSession(GormStaticApi.groovy:756)
   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)
Caused by: org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
   ... 6 more

尝试tomcat重启后,检查mongo连接等。我通过删除域对象mongo集合解决了这个问题(我最近对域对象进行了一系列更改)。是否可能由域对象的更改和mongo / GORM数据模型冲突导致此错误?如果是这样,我怎样才能避免将来放弃收藏?

我不相信其他人可能访问同一个域名集合,所以不要认为这是一个乐观的锁定异常,但如果是,我可以运行哪些命令mongo方面看看哪些连接正在访问该集合?

0 个答案:

没有答案