Grails GORM自动更新问题 [英] Grails GORM auto update issue

查看:145
本文介绍了Grails GORM自动更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新后的帖子:

在控制器中,如果我这样做:

In a Controller if I do this:

     def obj = new Test(name:"lol")
     obj.save(flush:true)
     obj.name = "lol2"

     //a singleton service with nothing to do with obj
     testService.dostuff() 
     /*
       "obj" gets persisted to the database right here
       even before the next println
     */
     println "done"

任何人都可以解释一下为什么在Grails 1.3.7而不是Grails 2上会发生这种情况吗?是什么原因?

Can anyone please explain me why is this happening with Grails 1.3.7 and not with Grails 2? What is the reason?

我知道我可以使用throw()并基本上重组代码,但是我对幕后发生的事情和原因很感兴趣.谢谢!

I know I could use discard() and basically restructure the code but I am interested in what and why is happening behind the scenes. Thanks!

旧帖子:

我有一个测试Grails应用程序.我有一个域类测试.测试:

I have a test Grails application. I have one domain class test.Test:

package test

class Test {
   String name
   static constraints = {}
}

我也有服务测试.TestService:

Also I have a service test.TestService:

package test

class TestService {

    static scope = "singleton"
    static transactional = true

    def dostuff() {
        println "test service was called"
    }
}

和一个控制器测试.TestController:

And one controller test.TestController:

package test

class TestController {

  def testService      

  def index = {
     def obj = new Test(name:"lol")
     obj.save(flush:true)
     obj.name = "lol2"
     testService.dostuff()
     println "done"
  }
}

那我该怎么做:

  • 创建域对象
  • 更改其属性之一
  • 调用单例服务方法

我期望的是

  • 除非我调用obj.save(),否则什么都不会持久化到数据库中

相反会发生什么:

  • 在服务调用之后,Grails将立即对数据库进行更新查询.

我已尝试从该URL进行以下配置:

I have tried the following configuration from this url: http://grails.1312388.n4.nabble.com/Turn-off-autosave-in-gorm-td1378113.html

hibernate.flush.mode="manual"

但这没有帮助.

我已经用Grails 1.3.7对其进行了测试,而Grails 2.0.3则没有此问题.

I have tested it with Grails 1.3.7, Grails 2.0.3 does not have this issue.

有人可以给我更多有关发生了什么的信息吗?似乎由于服务调用而必须终止当前会话,并且由于对象脏了,因此在服务调用之后该对象将自动持久保存到数据库中.我不了解的是,即使使用Hibernate中的手动刷新模式配置也无济于事.

Could anyone please give me a bit more information on what is exactly going on? It seems like the current session has to be terminated because of the service call and because the object is dirty it is getting automatically persisted to the database after the service call. What I don't understand that even with the manual flush mode configuration in Hibernate does not help.

提前谢谢!

推荐答案

我不确定链接到的线程会如何使您认为它可以工作.他们都说这行不通,创建的票已关闭,因为无法修复.这里的解决方案是使用discard()作为所述线程.

I'm not sure what about that thread you linked to made you think it would work. They all said it wouldn't work, the ticket created has been closed as won't fix. The solution here is to use discard() as the thread stated.

这篇关于Grails GORM自动更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆