title: Grails Quartz Job has no Hibernate session after upgrade, causing LazyInitializationException
body: <p>I've upgraded a Grails 1.0.4 application to 1.1.1. After upgrading, I'm repeatedly getting Exceptions when executing my Quartz jobs (using Quartz plugin 0.4.1). The plugin is used to manually schedule jobs using Simple and Cron Triggers via a service (paraphrased code below):</p>
  
  <pre><code>class SchedulerService implements InitializingBean
  {
      static scope = 'singleton'
      ...
      def schedule(def batch) {
          JobDetail job = new JobDetail(uniqueId, groupName, BatchJob.class, false, false, true)
          job.jobDataMap.put("batchId", batch.id)
  
          SimpleTrigger trigger = new SimpleTrigger(triggerId, triggerGroup, 0)
  
          SchedulerFactory factory = new SchedulerFactory()
          factory.initialize(properties)
          Scheduler scheduler = factory.getScheduler()
  
          scheduler.scheduleJob(job, trigger)
      }
      ...
  }
  </code></pre>
  
  <p>My BatchJob job is set up as follows:</p>
  
  <pre><code>class BatchJob implements Job, InterruptableJob
  {
      static triggers = {}
      void execute(JobExecutionContext context) {
          def batch = Batch.get(context.jobDetail.jobDataMap.getLongValue("batchId"))
          // the next line is "line 49" from the stack trace below
          def foo = batch.batchStatus.description
      }
  }
  </code></pre>
  
  <p>Here's an abbreviated definition of Batch.groovy (domain):</p>
  
  <pre><code>class Batch
  {
      BatchStatus batchStatus // relationship
  }
  </code></pre>
  
  <p>However, when <code>schedulerService.schedule()</code> is invoked with an existing, saved Batch, I receive the following Exception:</p>
  
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
          at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
          at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.unwrapProxy(GrailsHibernateUtil.java:311)
          at org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil$unwrapProxy.call(Unknown Source)
          at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
          ...
          &lt;b&gt;at BatchJob.execute(BatchJob.groovy:49)&lt;/b&gt;
          ...
  </code></pre>
  
  <p>I've tried the following actions to fix this, but none have worked:</p>
  
  <ul>
  <li>I've specified <code>static fetchMode = [batchStatus: 'eager']</code> on my Batch domain class</li>
  <li>I've used <code>static mapping = { columns { batchStatus lazy:false }}</code> on my Batch domain class</li>
  <li>I've tried using <code>batch.attach()</code> after calling <code>Batch.get()</code> in the Job</li>
  </ul>
  
  <p>I can't use <code>BatchJob.triggerNow()</code> in this instance, because this is only one of a couple examples - the others are still scheduled by the service, but might be scheduled as a cron job or otherwise. I should mention that I did upgrade the Quartz plugin as well when upgrading Grails; the previous Quartz version was 0.4.1-SNAPSHOT (as opposed to the upgraded version, just 0.4.1). </p>
  
  <p><strong>How do I get Hibernate sessions to work correctly in these manually-triggered Quartz Jobs?</strong></p>
  
  <p><em>I've also sent this question to the grails-user mailing list, as for a more niche issue like this, the list seems to elicit a bit more response. I'll update this question with an answer if one comes out of there. <a href="http://old.nabble.com/Quartz-Job-has-no-Hibernate-session-after-upgrade-from-1.0.3-to-1.1.1,-causing-LazyInitializationException-td26678488.html" rel="nofollow noreferrer">Here's a link</a>.</em></p>