All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.cohort.dbcp.DbcpConnectionsHealthCheck.kt Maven / Gradle / Ivy

The newest version!
package com.sksamuel.cohort.dbcp

import com.sksamuel.cohort.HealthCheck
import com.sksamuel.cohort.HealthCheckResult
import org.apache.commons.dbcp2.BasicDataSource

/**
 * A Cohort [HealthCheck] that checks for the number of connections in Apache DBCP2 [BasicDataSource].
 *
 * This is useful to detect when connections are unable to be made.
 *
 * The check is considered healthy if the total connection count (sum of idle and active) is >= [minConnections].
 */
class DbcpConnectionsHealthCheck(
  private val ds: BasicDataSource,
  private val minConnections: Int,
  override val name: String = "dbcp_connections",
) : HealthCheck {
   override suspend fun check(): HealthCheckResult {
    val total = ds.numActive + ds.numIdle
    val msg = "Database connections is $total [min required is $minConnections]"
    return if (total >= minConnections) HealthCheckResult.healthy(msg) else HealthCheckResult.unhealthy(msg, null)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy