title: How to handle a 'Configuration error[Cannot connect to database [...]]'
body: <p>I am implementing a web service with the Play Framework, that uses multiple databases. All databases are configured in the conf/application.conf by specifying the db.database1..., db.database2... properties. </p>
  
  <p>At startup, play will try to establish connections to all databases configured in the database and if one connection fails, the service will not start.</p>
  
  <p>In my case, not all databases are necessary to start the web service, but the web service can still run with limited functionality, if some databases are not available. Since not all databases are under my control, it is crucial for my web service to handle a connection error.</p>
  
  <p>Therefore my question:</p>
  
  <p>Is there a way to either</p>
  
  <ol>
  <li>handle the connection error by overriding some 'onError' method or insert a try-catch at the right place or</li>
  <li>manually create the Datasources at runtime to handle the error when they are created</li>
  </ol>
  
  <p>I would prefer solution 2.</p>
  
  <p>I am using play version 2.4.2 with scala version 2.11.7. </p>
  
  <p>Since the whole exceptions fills multiple pages, I only insert the first lines here:</p>
  
  <pre><code>CreationException: Unable to create injector, see the following errors:
  
  1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [foo]]
    while locating play.api.db.DBApiProvider
    while locating play.api.db.DBApi
      for field at play.api.db.NamedDatabaseProvider.dbApi(DBModule.scala:80) 
    while locating play.api.db.NamedDatabaseProvider 
    at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149) 
    at play.api.db.DBModule$$anonfun$namedDatabaseBindings$1.apply(DBModule.scala:34): 
  Binding(interface play.api.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=appstate)) to ProviderTarget(play.api.db.NamedDatabaseProvider@1a7884c6)) (via modules: com.google.inject.util.Modules$OverrideModule -&gt; play.api.inject.guice.GuiceableModuleConversions$$anon$1) 
  Caused by: Configuration error: Configuration error[Cannot connect to database [foo]] 
      at play.api.Configuration$.configError(Configuration.scala:178)
      at play.api.Configuration.reportError(Configuration.scala:829) 
      at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:48) 
      at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42) 
      at scala.collection.immutable.List.foreach(List.scala:381) 
      at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42) 
      at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
  </code></pre>