【问题标题】:Grails: Accessing GORM from a background threadGrails:从后台线程访问 GORM
【发布时间】:2011-08-12 16:39:27
【问题描述】:

我已经为这个错误苦苦挣扎了一个星期,我为此严重失去理智!我已经尝试过多种实现、变通方法和 hack 等等,但我只是不断陷入另一个例外。

我正在使用 Executor 插件异步运行方法:

runAsync{
   run(...)
}

该方法最初删除了一些对象:

page.delete(flush:true)

然后可能会重新创建这些对象:

def page = new Page(type : Page.TYPE_TABLE, domain : domainVersion.domain, identifier : tableName)
page.save(flush: true, failOnError: true)

但这会失败,并出现以下异常:

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.ramboll.egs.ohs.domain.Domain#1]

PageDomain 之间的关系只是由具有Domain 属性的Page 实现的。否 hasMany og belongsTo - 由于性能问题,我在之前的帖子中对此感到沮丧。

我想我已经尝试了savemergewithTransachtionPersistenceContextInterceptor 的所有可以想到的组合...

这应该如何工作?请举例。

提前致谢!

【问题讨论】:

  • 它是如何失败的?请展示一些不仅仅是伪代码的代码。
  • 你确定第一行保存了吗?检查它是否返回 null,表示违反约束。还要考虑save(flush: true, failOnError: true),如果无效则抛出异常

标签: grails grails-orm executor


【解决方案1】:

在新线程中工作似乎不是问题,它看起来像是标准验证问题。这表示Page 为空,这表示验证错误,因为如果成功,save() 将返回实例,如果存在一个或多个验证错误,则返回null。有几个选项:

def page = new Page(type : Page.TYPE_TABLE,
     domain: dbUpdate.domainVersion.domain, identifier: tableName)
page.save(flush:true)
if (page.hasErrors()) {
   // handle errors
}
else {
   def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
       con, tableName, dbUpdate.author).save(flush:true)
}

或者使用failOnError抛出异常:

def page = new Page(type : Page.TYPE_TABLE, identifier: tableName,
     domain: dbUpdate.domainVersion.domain).save(flush:true, failOnError: true)
def pageVersion = createPageVersion(page, dbUpdate.domainVersion,
    con, tableName, dbUpdate.author).save(flush:true)

【讨论】:

  • 你的建议让我意识到了一些新手错误,我现在已经用更合适的描述更新了这个问题。
猜你喜欢
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多