Grails GORM auto update issue

我是研究僧i 提交于 2019-12-11 06:14:47

问题


Updated post:

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"

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

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!

Old post:

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

package test

class Test {
   String name
   static constraints = {}
}

Also I have a service test.TestService:

package test

class TestService {

    static scope = "singleton"
    static transactional = true

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

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"
  }
}

So what I do:

  • Create a domain object
  • Change one of it's properties
  • Call a singleton service method

What I would expect:

  • Nothing gets persisted to the db unless I call obj.save()

What happens instead:

  • Right after the service call Grails will do an update query to the database.

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"

But it didn't help.

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

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.

Thanks in advance!


回答1:


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.



来源:https://stackoverflow.com/questions/11546269/grails-gorm-auto-update-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!