hibernate - 我可以在 withTransaction 闭包内提交吗

标签 hibernate grails grails-orm

我可以在 withTransaction 内提交吗?关闭?我想在我的 Quartz 里面做这样的事情工作:

Author.withTransaction {
    def author = Author.get(1)
    author.staus = 'processing'
    author.save()
    commit // how can i do this?
    // so some work here which takes 1-2 minutes
    author.status = 'completed'
    author.save()
}

这个想法是我想要一个状态屏幕来显示所有 Author目前正在处理的 s,所以我想将状态设置为 processing并且能够从 Controller 看到这个状态。

编辑:
这将在没有 withTransaction 的情况下工作,但我必须有 withTransaction那里...见this question .

最佳答案

为了从不同的未提交事务读取值,您的数据库隔离级别必须设置为“读取未提交”,这通常表明您可能做错了什么。

你有什么理由不能把它分成单独的交易吗?一种将记录标记为“进行中”,另一种用于执行实际工作?

Author.withTransaction {
    def author = Author.get(id)
    author.status = 'processing'
    author.save()
}

Author.withTransaction() {
    def author = Author.get(id)
    // do some work
    author.status = 'completed'
    author.save()
}

关于hibernate - 我可以在 withTransaction 闭包内提交吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17713231/

相关文章:

grails - 域构造函数上缺少 transient 属性(带有映射参数)

java - 如何将 native 查询打印到控制台?

MySQL utf8mb4 unicode(笑)

java - Hibernate 条件查询以匹配所有子集合元素

grails - 使用 Groovy 将文件从一个文件夹复制到另一个文件夹

grails - grail的<g:layoutBody/>标签如何工作?

Grails 2.3.7 Rest API - 搜索/查询

hibernate - 在grails的单个应用程序中连接不同的数据库(动态)

更新 : Validation error whilst flushing entity on AbstractPersistenceEventListener 上的 ValidationException

java - 需要解决一个查询缓存问题