[英]Grails runtime exception: Could not open Hibernate session for transaction
[英]Grails and hibernate session, save to database on exception
我正在使用Grails框架。
想要在失败时将某些内容保存到数据库中(引发RuntimeException
之后)。 假设我在一种事务服务中有这样的东西:
try {
throw new RuntimeException()
} catch(Exception ex) {
new FatalErrorDomainObject().save()
}
当前版本将失败,因为会话设置为回滚。
我试图以多种方式解决此问题,到目前为止,我发现的是:
.withTransaction
无效,因为它将事务绑定到当前会话
.withSession
将不起作用,因为它将仅重用现有会话
.withNewSession
单独将不起作用,因为它将在同一线程中创建新会话(同一线程=相同的数据库连接,因此它将因SQL异常而失败)
到目前为止,我发现的唯一可行的解决方案是创建新线程和新的休眠会话。 这真的是唯一(正确的)方法吗?
public static void syncSession(Closure job) {
// Checking environment in production code it's not very elegant but thanks to this it's transparent
// for all tests and makes them DRY.
if(Environment.current == Environment.TEST) {
job.call()
}
Thread t = new Thread({
DomainObject.withNewSession {
job.call()
}
})
t.start()
t.join()
}
用法:
try {
throw new RuntimeException()
} catch(Exception ex) {
syncSession {
new FatalErrorDomainObject().save()
}
}
我认为您的解决方案更好,但是我仍然想给您这个建议:在其余逻辑之前在单独的事务中调用new FatalErrorDomainObject().save()
,并在提交结束之前在逻辑末尾删除FatalErrorDomainObject。 像这样:
def fatalError = new FatalErrorDomainObject()
FatalErrorDomainObject.withTransaction { status ->
fatalError.save()
}
doSomethingThatMightThrowRuntimeException()
FatalErrorDomainObject.get(fatalError.id).delete()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.