package com.azavea.rf.database

import com.zaxxer.hikari.{HikariConfig, HikariDataSource}


/**
  * PostGIS Database for Raster Foundry data
  *
  * @param jdbcUrl connection url for JDBC
  * @param dbUser user to authenticate in the database as
  * @param dbPassword password to use to authenticate user
  */
class Database(jdbcUrl: String, dbUser: String, dbPassword: String) {

  private val hikariConfig = new HikariConfig()
  // This property is now being manually set because it seemed not
  // to get picked up automatically through the JDBC url. See the hikari documentation
  // under the jdbcUrl for more information.
  hikariConfig.setDriverClassName("org.postgresql.Driver")
  hikariConfig.setJdbcUrl(jdbcUrl)
  hikariConfig.setUsername(dbUser)
  hikariConfig.setPassword(dbPassword)
  hikariConfig.setConnectionTimeout(2000)

  private val dataSource = new HikariDataSource(hikariConfig)

  /**
    * Utility function for closing the underlying DB connection pool
    */
  def closeConnectionPool() = dataSource.close()

  // Custom driver to handle postgres column types
  val driver = ExtendedPostgresDriver
  import driver.api._

  val db = {
    val executor = AsyncExecutor("slick", numThreads=64, queueSize=1000)
    driver.api.Database.forDataSource(dataSource, executor)
  }
}

object Database extends Config {
  def DEFAULT = new Database(jdbcUrl, dbUser, dbPassword)
}

package com.azavea.rf.database

import com.zaxxer.hikari.{HikariConfig, HikariDataSource}


/**
  * PostGIS Database for Raster Foundry data
  *
  * @param jdbcUrl connection url for JDBC
  * @param dbUser user to authenticate in the database as
  * @param dbPassword password to use to authenticate user
  */
class Database(jdbcUrl: String, dbUser: String, dbPassword: String) {

  private val hikariConfig = new HikariConfig()
  // This property is now being manually set because it seemed not
  // to get picked up automatically through the JDBC url. See the hikari documentation
  // under the jdbcUrl for more information.
  hikariConfig.setDriverClassName("org.postgresql.Driver")
  hikariConfig.setJdbcUrl(jdbcUrl)
  hikariConfig.setUsername(dbUser)
  hikariConfig.setPassword(dbPassword)

  private val dataSource = new HikariDataSource(hikariConfig)

  /**
    * Utility function for closing the underlying DB connection pool
    */
  def closeConnectionPool() = dataSource.close()

  // Custom driver to handle postgres column types
  val driver = ExtendedPostgresDriver
  import driver.api._

  val db = {
    val executor = AsyncExecutor("slick", numThreads=5, queueSize=1000)
    driver.api.Database.forDataSource(dataSource, executor)
  }
}

object Database extends Config {
  def DEFAULT = new Database(jdbcUrl, dbUser, dbPassword)
}

Fix tiler deadlocks when interacting with the database
Fix tiler deadlocks when interacting with the database

The connection timeout for our database connection pool was initially set too
low, which in conjunction with the number of threads allocated for Slick, caused
many of the Slick threads to spin in a parked state while eating up all of the
available CPU.

These changes reduce the overall number of threads to 5 ((2 * CPU Cores) + 1)
and remove the explicit 2000ms connection timeout in favor of the 30s default.
