Grails:从后台线程访问GORM
我已经为这个错误挣扎了一个星期了,现在我已经严重地失去了理智!我已经尝试过多ble实现、变通方法和黑客等等,但我只是不断地陷入另一个例外 我正在使用Executor插件异步运行一个方法:Grails:从后台线程访问GORM,grails,gorm,executor,Grails,Gorm,Executor,我已经为这个错误挣扎了一个星期了,现在我已经严重地失去了理智!我已经尝试过多ble实现、变通方法和黑客等等,但我只是不断地陷入另一个例外 我正在使用Executor插件异步运行一个方法: runAsync{ run(...) } 该方法最初删除一些对象: page.delete(flush:true) def page = new Page(type : Page.TYPE_TABLE, domain : domainVersion.domain, identifier : table
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)
之后可能会重新创建这些对象:
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]
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)
页面
和域
之间的关系简单地通过具有域
属性的页面
实现。没有有很多og属于——因为在之前的一篇文章中,由于性能问题,我不愿意这样做
我想我已经尝试了save
、merge
、with transachtion
和PersistenceContextInterceptor
的所有可想到的组合
这是怎么回事?请举例说明
提前谢谢 在新线程中工作似乎不是问题,它看起来像是一个标准的验证问题。这意味着页面
为空,这表示一个验证错误,因为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
引发异常:
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]
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)
它是如何失败的?请显示一些不只是伪代码的代码。是否确实保存了第一行?检查它是否返回null,表示违反了约束。还考虑<代码>保存(刷新:TRUE,ValnOrrr:true)< /C> >,如果无效,则引发异常。您的建议使我意识到一些新手错误,现在我用更合适的描述更新了这个问题。