title: Grails: Testing Optimistic Locking
body: <p>Using Grails 2.3.9</p>
  
  <p>I am currently trying to test the optimistic locking in Grails. The <a href="http://grails.org/doc/2.3.9/guide/single.html#locking" rel="nofollow">documentation</a> says, </p>
  
  <blockquote>
    <p>When you perform updates Hibernate will automatically check the
    version property against the version column in the database and if
    they differ will throw a StaleObjectException</p>
  </blockquote>
  
  <p>In the integration test, I have something like:</p>
  
  <pre><code>def existingGroup = new Group("Group")
      .save(flush: true, failOnError: true)
  
  def groupA = Group.get(existingGroup.id)
  def groupB = Group.get(existingGroup.id)
  
  groupA.name = "Group A"
  groupA.save(failOnError: true, flush: true)
  
  groupB.name = "Group B"
  groupB.save(failOnError: true, flush: true)
  </code></pre>
  
  <p>The exception is, however, never thrown. So, I guess, I'm doing something wrong with the session/flushing. But all permutations (using flush and not) on that had no change. I would like to see that this exception is thrown.</p>
  
  <p>I also tried resetting the version to 0, without success (groupB saved as usually).</p>
  
  <p>Am I doing something wrong?</p>