Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created June 13, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeevprasanna/a242f4143a11f51e16ca108ea4103864 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/a242f4143a11f51e16ca108ea4103864 to your computer and use it in GitHub Desktop.
import cats.effect.{ExitCode, IO, IOApp, Resource}
import cats.implicits._
import config.DataBaseConfig
import doobie.hikari.HikariTransactor
import doobie.implicits._
import doobie.util.ExecutionContexts
object Test extends IOApp {
val dbConfig = DataBaseConfig()
val transactor: Resource[IO, HikariTransactor[IO]] =
for {
ce <- ExecutionContexts.fixedThreadPool[IO](32) // our connect EC
te <- ExecutionContexts.cachedThreadPool[IO] // our transaction EC
xa <- HikariTransactor.newHikariTransactor[IO](
"org.postgresql.Driver", // driver classname
dbConfig.url, // connect URL => "jdbc:postgresql://localhost:5432/datalakes",
dbConfig.userName, // username
dbConfig.password, // password
ce, // await connection here
te // execute JDBC operations here
)
} yield xa
def isDBHealthy =
(xa: HikariTransactor[IO]) =>
sql"select count(*) from DATA_TABLE"
.query[Int]
.option
.transact(xa)
.map(_ != None)
override def run(args: List[String]): IO[ExitCode] =
transactor
.use { implicit xa: HikariTransactor[IO] =>
val res = isDBHealthy(xa).unsafeRunSync()
println(s"result within IOApp ===> $res")
ExitCode.Success.pure[IO]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment