How to handle the "Configuration Error" [Cannot connect to the database [...]] '

I am implementing a web service with a Play Framework that uses multiple databases. All databases are configured in conf / application.conf by setting the properties db.database1 ..., db.database2 ....

When starting, playback will try to establish connections with all the databases configured in the database, and if one connection fails, the service will not start.

In my case, not all databases are needed to run the web service, but the web service may work with limited functionality if some databases are not available. Since not all databases are under my control, it is very important for my web service to handle a connection error.

So my question is:

Is there any way either

  • handle a connection error by overriding some onError method or by inserting try-catch in the right place or
  • Manually create data sources at run time to handle errors when they are created.

I would prefer solution 2.

I am using game version 2.4.2 with scala version 2.11.7.

Since all exceptions fill several pages, I only insert the first lines:

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 -> 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)
+4
source share
2 answers

I remember that there is a global settings configuration file to catch errors when the application starts.

: https://www.playframework.com/documentation/2.0/ScalaGlobal , , , .

2.4.x DI (https://www.playframework.com/documentation/2.4.x/GlobalSettings).

0

, , BoneCP. , ConnectionPools , .

db database ( Play ) database.conf.

object ConnectionPool {
  private var connectionPools = Map.empty[String, BoneCP]
  val config = ConfigFactory.parseFile(new File("conf/database.conf"))

  private def dbConfig(dbId: String): ConfigObject = {
    config.getObject("database." + dbId).asInstanceOf[ConfigObject]
  }

  def createConnectionPool(dbId: String): BoneCP = {
    val dbConf = dbConfig(dbId)
    val cpConfig: BoneCPConfig = new BoneCPConfig()
    cpConfig.setJdbcUrl(dbConf.get("url").unwrapped().toString)
    cpConfig.setUsername(dbConf.get("user").unwrapped().toString)
    cpConfig.setPassword(dbConf.get("password").unwrapped().toString)
    new BoneCP(cpConfig)
  }

  def getConnectionPool(dbId: String): BoneCP = {
    if(!connectionPools.contains(dbId)) {
      val cp = createConnectionPool(dbId)
      connectionPools = (connectionPools + (dbId -> cp))
    }
    connectionPools(dbId)
  }

  def getConnection(dbId: String): Connection = {
    getConnectionPool(dbId).getConnection()
  }

  def withConnection[T](dbId: String)(fun: Connection => T): T = {
    val conn = getConnection(dbId)
    val t = fun(conn)
    conn.close()
    t
  }
}
0

Source: https://habr.com/ru/post/1608296/


All Articles