title: How to start JPA in a Guice + Quartz web application?
body: <p>I'm having some kind of trouble with an application, I use Guice for injections with some background tasks handled by Quartz.</p>
  
  <p>As stated by the <a href="https://code.google.com/p/google-guice/wiki/JPA" rel="nofollow">documentation</a>, I'm using a PersistFilter to handle my transactions and starting my JPAService. The problem is that the first time my jobs are executed, the JPAService is not started yet and my <code>unitOfWork.begin()</code> throws an exception :</p>
  
  <pre><code>2013-07-01 11:45:05,527 [DefaultQuartzScheduler_Worker-1] ERROR com.foo.core.synchronization.impl.Result.notifyListener(Result.java:65) - Error while notifying synchronization listener
  java.lang.NullPointerException
          at com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)
          at com.foo.convert.DiscoveryService.parsedElement(DiscoveryService.java:148)
          at com.foo.convert.DiscoveryService.parsedElement(DiscoveryService.java:67)
          at com.foo.core.synchronization.impl.Result.notifyListener(Result.java:62)
          at com.foo.core.synchronization.impl.Synchronizer.synchronize(Synchronizer.java:68)
          at com.foo.convert.DiscoveryService.execute(DiscoveryService.java:128)
          at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
          at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
  </code></pre>
  
  <p>All the next calls to the task are successfull. My bet is that the persist service is not started yet, so I tried to inject an initializer :</p>
  
  <pre><code>public class MyInitializer { 
          @Inject MyInitializer(PersistService service) {
                  service.start(); 
          } 
  }
  </code></pre>
  
  <p>When injecting this initializer, Quartz jobs are working right away, but the <code>PersistFilter</code> calls <code>service.start()</code> a second time and my web application breaks :</p>
  
  <pre><code>SEVERE: Exception starting filter Guice Filter
  java.lang.IllegalStateException: Persistence service was already initialized.
      at com.google.inject.internal.util.$Preconditions.checkState(Preconditions.java:142)
      at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:88)
      at com.google.inject.persist.PersistFilter.init(PersistFilter.java:77)
      at com.google.inject.servlet.FilterDefinition.init(FilterDefinition.java:114)
      at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:98)
      at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
  </code></pre>
  
  <p>Injecting the <code>PersistFilter</code> in my <code>QuartzInitializer</code> does not help either.</p>
  
  <p>The workaround I implemented so far is to wait one minute before starting the Quartz jobs but this is not a long-term solution. </p>
  
  <p>Anybody had this problem before ?</p>
  
  <p>Edit : This seems to be related to this issue <a href="https://code.google.com/p/google-guice/issues/detail?id=598" rel="nofollow">Issue 598: Persist Extension: PersistService.start() cannot be called multiple times</a></p>