title: Grails - not-null property references a null or transient value error only with concurrent users
body: <p>a web application that we have been developing in Grails has recently moved into the stress testing phase, and now we are encountering this issue that seems to only be happening when multiple users (different user accounts) perform the same actions at the same time.  These actions are transactional but none of them are executed on the same object (new objects are being created). The exception that we have been getting is:</p>
  
  <blockquote>
    <p>Class: org.hibernate.PropertyValueException
    Message: not-null property references a null or transient value: xxx.xxx.xxx.domains.PathSections._PathViews_sectionsBackref </p>
  </blockquote>
  
  <p>This is generally what the exception looks like, with some variations to the involved domain classes. To make it even worse, when this error occurs, every other transactional action that involves a list will result in the same error (different domain classes), whether concurrently done or not.  The server then needs to be restarted in order to temporarily fix it.  Here are some snippets of the relevant domain classes:</p>
  
  <pre><code>class PathViews extends View{
  
  static constraints = {
      molreports(nullable:true)
      molorders(nullable:true)
      otherreports(nullable: true)
      sections(nullable:true)
      addendum(nullable:true)
      images(nullable:true)
      edits(nullable:true)
  }
  
  List sections
  List otherreports
  List molreports
  List molorders
  List images
  PathEdits edits
  PathSections addendum
  int headings
  String accid
  
  static auditable = true
  
  static hasMany = [sections: PathSections, otherreports: PathSections, molorders:MolecularOrder, molreports: MolecularReport, images: PathLocalImage]
  
  static mappedBy = [sections: 'pathviews', otherreports: 'reports']
  
  
  class PathSections extends Sections {
  
  static constraints = {
      pathviews(nullable:true)
      reports(nullable:true)
  }
  
  
  static auditable = true
  
  static belongsTo =  [pathviews: PathViews, reports: PathViews]
  </code></pre>
  
  <p>}</p>
  
  <p>A temporary fix that I thought was working at first was to make most of the transactional service methods synchronized.  This seems to have just made the error more random and less reproducible. I have also double checked other things such as making sure the child objects of a hasMany relationship are not explicitly saved before being added to a parent and using domainClass.lock(id) instead of domainClass.get(id).  This issue does not occur at all when it's just one user using the application.</p>
  
  <p>Versions used:</p>
  
  <blockquote>
    <p>Grails: 2.3.7
    Hibernate: 3.6.10.10   </p>
  </blockquote>
  
  <p>Any help would be greatly appreciated as I'm close to pulling out whatever's left of my hair right now, and please let me know if anymore information is needed.</p>
  
  <p>Thanks!
  Shawn.  </p>