Aston
Aston

Reputation: 3712

Grails GORM auto update issue

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:

What I would expect:

What happens instead:

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!

Upvotes: 1

Views: 1690

Answers (1)

Gregg
Gregg

Reputation: 35864

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.

Upvotes: 0

Related Questions