title: Multithreading in Grails - Passing domain objects into each thread causes some fields to randomly be null
body: <p>I am trying to speed up a process in a Grails application by introducing parallel programming. This particular process requires sifting through thousands of documents, gathering the necessary data from them and exporting it to an excel file.</p>
  
  <p>After many hours of trying to track down why this process was going so slowly, I've determined that the process has to do a lot of work gathering specific parts of data from each domain object. (Example: The domain object has lists of data inside it, and this process takes each index in these lists and appends it to a string with commas to make a nice looking, sorted list in a cell of the excel sheet. There are more examples but those shouldn't be important.)</p>
  
  <p>So anything that wasn't a simple data access (document.id, document.name, etc...) was causing this process to take a long time.</p>
  
  <p>My idea was to use threads for each document to asynchronously acquire all this data, when each thread finished gathering the data, it can come back to the main thread and be placed into the excel sheet, now all with simple data access, because the thread already gathered all the data. </p>
  
  <p>This seems to be working, however I have a bug with the domain objects and the threads. Each thread is passed in its corresponding document domain object, but for whatever reason, the document domain objects will randomly have parts of its data changed to null. </p>
  
  <p>For example: Before the document is passed into the thread, one part of the domain object will have a list that looks like this: [US, England, Wales], randomly at any point, the list will look like this in the thread: [US, null, Wales]. And this happens for any random part of the domain object, at any random time. </p>
  
  <p>Generating the threads:</p>
  
  <pre><code>def docThreadPool = Executors.newFixedThreadPool(1)
  def docThreadsResults = new Future&lt;Map&gt;[filteredDocs.size()]
  filteredDocs.each {
      def final document = it
      def future = docThreadPool.submit(new DocumentExportCallable(document))
      docThreadsResults[docCount] = future
      docCount++
  }
  </code></pre>
  
  <p>Getting the data back from the threads:</p>
  
  <pre><code>filteredDocs.each {
          def data = docThreadsResults[count].get()
  
          build excel spreadsheet...
  }
  </code></pre>
  
  <p>DocumentExportCallable class:</p>
  
  <pre><code>class DocumentExportCallable implements Callable {
      def final document
  
      DocumentExportCallable(document) {
          this.document = document
      }
  
      Map call() {
              def data = [:]
  
              code to get all the data...
  
              return data
      }
  }
  </code></pre>
  
  <p>EDIT:
  As seen below, it would be useful if I could show you the domain object. However I am not able to do this. BUT, the fact that you guys asked me about the domain object had me thinking that it just might be where the problem lies. Turns out, every part of the domain object that randomly messes up in the threads is a variable in the domain object inside "mapping" which uses SQL joins to get the data for those variables. I've just been made aware of lazy vs eager fetching in Grails. I'm wondering if this might be where the problem lies...by default it is set to lazy fetching so this constant access to the db by each thread might be where things are going wrong. I believe finding a way to change this to eager fetching might solve the problem. </p>