简而言之,我要解决的是如何使用 Hibernate 从 Grails 应用程序中的某些数据库错误中恢复,并继续处理跳过作为一批更改一部分的失败行更新的事务。
该应用程序使用 Grails 2.3.11,但我也尝试使用 1.3.8 版本,但结果类似。
基本上有一个 Grails 服务类,它遍历导入记录的列表并尝试适本地更新关联的主记录。在某些情况下,domain.save(flush:true)
期间可能会发生异常。打电话,例如org.hibernate.exception.DataException
由于诸如(数据截断:列的数据太长......)等问题而引发。
在这一点上,我已经尝试过:
@Transactional
class UpdateService {
public updateBatch(Integer batchId) {
...
list.each { record ->
record.value = 123
try {
nestedService.saveDomain()
} catch (e) {
record.clearErrors()
record.discard()
}
}
batch.status = "POSTED"
batch.save()
}
}
@Transactional
class NestedService {
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class)
public void saveDomain(domainObj) throws RuntimeException {
if (domainObj.validate() && domainObj.save(flush:true) {
log.info "domain $domain was saved"
}
}
}
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction
@Transactional(propagation = Propagation.REQUIRE_NEW)
public void saveDomain(domainObj) {
def newObj = new Domain.get(domainObj.id)
newObj.properties = domainObj.properties
if (newObj.validate() && newObj.save(force:true) ) { ... }
最佳答案
一个简单的方法是循环然后使用 validate()。如果确实失败了,那么只需存储失败实体的 id 并继续。
if(!domainObject.validate()){
// store Id for trying it again later ?
}else{
// Save
}
https://stackoverflow.com/questions/29438818/
相关文章:
grails - Grails:如何从BuildConfig.groovy中知道正在执行哪个命令
mongodb - 在grails和MongoDB插件的条件查询中使用allowDiskUse?
grails - “grails -Dserver.port=9999 run-app”无效
grails - 使用插件时如何在Grails中添加Quartz监听器
php - 通过php我如何关闭由php ssh2_connect创建的事件ssh连接?
security - 在 Groovy 动态 SQL 中检测 SQL 注入(inject)
ruby-on-rails-3 - 使用 Heroku 访问和部署问题