title: How to access database on application start in Play Framework?
body: <p>I want to create a scheduler and get cron expressions in the database during start up. But I get an error "Error injecting constructor, java.lang.RuntimeException: There is no started application". </p>
  
  <p>I think the problem is with using play.api.Play.current but I need it for DB.withConnection { implicit connection =></p>
  
  <p>Do I need to inject db connection? How do I do that? Or Am I using a wrong approach? Please advice.</p>
  
  <p>This is what I have so far..</p>
  
  <p><strong>Application.conf</strong></p>
  
  <pre><code>play.modules.enabled += "tasks.MyRecurrentTaskModule"
  </code></pre>
  
  <p><strong>MyRecurrentTaskModule.scala</strong></p>
  
  <pre><code>class MyRecurrentTaskModule extends AbstractModule {
    override def configure() = {
      bind(classOf[RecurrentTask]).asEagerSingleton()
    }
  }
  
  @Singleton
  class RecurrentTask @Inject() (actorSystem: ActorSystem, lifecycle: ApplicationLifecycle) {
  
      lifecycle.addStopHook{ () =&gt;
  
          Future.successful(actorSystem.shutdown())
      }
      // Error when I access db
      val schedule = models.ScheduleTable.getSchedule(1)
      ...
  
  }
  </code></pre>
  
  <p><strong>ScheduleTable.scala</strong></p>
  
  <pre><code>import play.api.db._
  import play.api.Play.current
  
  case class ScheduleTable (
    ...
  ) {
  }
  
  object ScheduleTable {
  val scheduleParser = {
    ...
    }
  }
  
  def getSchedule (ScheduleId: Int): Option[ScheduleTable] = {
      DB.withConnection { implicit connection =&gt;
        SQL("""
            select * from SCHEDULE_TABLE s
            where s.ScheduleId = {sid}
            """)
        .on('sid -&gt; ScheduleId)
        .as(ScheduleTable.scheduleParser.singleOpt)
      }
  }
  ...
  </code></pre>