Created
June 13, 2019 11:34
-
-
Save rajeevprasanna/a242f4143a11f51e16ca108ea4103864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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