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

harness.sql.JDBCConnectionPool.scala Maven / Gradle / Ivy

There is a newer version: 5.1.3
Show newest version
package harness.sql

import harness.sql.autoSchema.{MigrationRunner, PlannedMigrations}
import harness.sql.error.ConnectionError
import harness.zio.*
import zio.*

final class JDBCConnectionPool private (val pool: ZPool[ConnectionError, JDBCConnection])
object JDBCConnectionPool {

  def apply(cf: ConnectionFactory, min: Int, max: Int, duration: Duration): ZIO[Logger & Scope, ConnectionError, JDBCConnectionPool] =
    Logger.log.debug(s"Creating ConnectionPool($min, $max)") *>
      ZPool
        .make(cf.getJDBCConnection, min to max, duration)
        .map(new JDBCConnectionPool(_))
  // .withFinalizerExit { (_, _) => Logger.log.debug("Releasing ConnectionPool") }

  def layer(min: Int, max: Int, duration: Duration): ZLayer[ConnectionFactory & Logger & Scope, ConnectionError, JDBCConnectionPool] =
    ZLayer.fromZIO { ZIO.service[ConnectionFactory].flatMap(JDBCConnectionPool(_, min, max, duration)) }

  val configLayer: ZLayer[DbConfig & Logger & Scope, ConnectionError, JDBCConnectionPool] =
    ZLayer.fromZIO {
      ZIO.serviceWithZIO[DbConfig] { config =>
        JDBCConnectionPool(
          ConnectionFactory(config.psqlJdbcUrl, config.credentials.username, config.credentials.password),
          config.pool.minConnections,
          config.pool.maxConnections,
          config.pool.duration,
        )
      }
    }

  def configLayerWithMigrations(migrations: PlannedMigrations): RLayer[HarnessEnv & DbConfig & Scope, JDBCConnectionPool] =
    ZLayer.makeSome[HarnessEnv & DbConfig & Scope, JDBCConnectionPool](
      configLayer,
      ZLayer.fromZIO { MigrationRunner.runMigrationsFromPool(migrations) },
    )

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy