views:

1537

answers:

2

I'm writing a small Grails app, and I keep on getting StaleObjectStateException:s for about 1/10:th of the calls to "createfoo" when running the following rather simple code. Most probably I'm missing out on the best way to use GORM.

This is the code:

def viewfoo = {
  session.user.refresh()
  // ...
}

def createfoo = {
  session.user.refresh()
  var user = session.user
  if (param["name"]) {
    var newFoo = new Foo()
    newFoo.name = param["name"]
    if (newFoo.validate()) {
      newFoo.save()
      if (user.validate()) {
        user.addToFoos(newFoo)
      } else {
        user.discard()
      }
    } else {
      newFoo.discard()
    }
  }
}

My questions regarding GORM best practices:

  1. Is the "if-validate()-then-save()-else-discard()" the correct way do to persist a new object in GORM?

  2. Should I validate all objects that I'm about to save()? I.e. should I validate both the Foo-object and the User-object in the above code? Will validating the User-object implicitly check the status of the Foo-object?

  3. What did I do to deserve the StaleObjectStateException? :-)

The GORM/Hibernate exception:

Caused by: Object of class [Foo] with identifier [15]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Foo#15]
+2  A: 

One thing I notice is that you're not saving user, even though you've just added some foo to it. In fact, saving user should obviate the need to save foo.

You don't have to validate user: it's not having its properties changed by an untrusted source, and the database-level constraints are checked anyway whenever you save.

Lastly, things like user.refresh() are better moved outside your actions and into an interceptor or a filter.

Bart Schuller
+2  A: 

I'm not exactly sure why you're hitting issues, but there is a merge method on domain objects. It lets you reattach the current object to the current persistent context.

I don't know enough about what your Foo, or what customizations you've done to the User object, or the version of grails/java you're using to be able to reproduce this.

I'm thinking it has something to do with the refreshing you're doing on the user object which is causing the database version to get updated (and thus out of sync), but I can't be sure.

Also, I believe that the validate then discard behavior is changing and less necessary in grails 1.1 based on the comments on this post

Ted Naleid