Skip to content

Commit

Permalink
perf: use HikariCP instead of JDBC DriverManager for manage conne…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
yoshinorin committed Jun 6, 2024
1 parent da271c3 commit 6b5cefe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object Dependencies {
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % jsoniterVersion % "test-internal",
"org.mariadb.jdbc" % "mariadb-java-client" % "3.3.3",
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-hikari" % doobieVersion,
// "org.tpolecat" %% "doobie-scalatest" % doobieVersion % Test,
"org.wvlet.airframe" %% "airframe-ulid" % "24.5.2",
"com.github.ben-manes.caffeine" % "caffeine" % "3.1.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import scala.concurrent.ExecutionContextExecutor
import cats.implicits.catsSyntaxApplicativeId
import doobie.*
import doobie.implicits.*
import doobie.util.transactor.Transactor.Aux
// import doobie.util.transactor.Transactor.Aux
import doobie.hikari.*
import com.zaxxer.hikari.HikariConfig
import cats.effect.*
import cats.effect.IO
import net.yoshinorin.qualtet.actions.{Action, Continue, Done}
Expand All @@ -20,39 +22,64 @@ class DoobieTransactor(config: DBConfig) extends Transactor[ConnectionIO, IO] {
// NOTE: No-need ContextShift: https://typelevel.org/cats-effect/docs/migration-guide#contextshift
// implicit val cs: ContextShift[IO] = IO.contextShift(executionContexts)

/* use `DriverManager` version
val transactor: Aux[IO, Unit] = Transactor.fromDriverManager[IO](
driver = "org.mariadb.jdbc.Driver",
url = config.url,
user = config.user,
password = config.password,
logHandler = None
)
*/

val transactor: Resource[IO, HikariTransactor[IO]] =
for {
hikariConfig <- Resource.pure {
val hConfig = new HikariConfig()
hConfig.setDriverClassName("org.mariadb.jdbc.Driver")
hConfig.setJdbcUrl(config.url)
hConfig.setUsername(config.user)
hConfig.setPassword(config.password)
// TODO: add more configs from env
hConfig
}
xa <- HikariTransactor.fromHikariConfig[IO](hikariConfig)
} yield xa

override def perform[R](action: Action[R]): ConnectionIO[R] = action match {
case continue: Continue[ConnectionIO, R, _] => continue.request.flatMap { t => perform(continue.next(t)) }
case done: Done[R] => done.value.pure[ConnectionIO]
}

override def transact[T](connectionIO: ConnectionIO[T]): IO[T] = connectionIO.transact(transactor)
override def transact[T](connectionIO: ConnectionIO[T]): IO[T] =
transactor.use { xa =>
connectionIO.transact(xa)
}

override def transact[R](action: Action[R]): IO[R] = transact(perform(action))

override def transact2[T1, T2](ts: (ConnectionIO[(T1, T2)])): IO[T2] = {
for {
r <- ts.transact(transactor)
} yield r._2
transactor.use { xa =>
for {
r <- ts.transact(xa)
} yield r._2
}
}

override def transact4[T1, T2, T3, T4](ts: (ConnectionIO[(T1, T2, T3, T4)])): IO[T4] = {
for {
r <- ts.transact(transactor)
} yield r._4
transactor.use { xa =>
for {
r <- ts.transact(xa)
} yield r._4
}
}

override def transact8[T1, T2, T3, T4, T5, T6, T7, T8](ts: (ConnectionIO[(T1, T2, T3, T4, T5, T6, T7, T8)])): IO[T8] = {
for {
r <- ts.transact(transactor)
} yield r._8
transactor.use { xa =>
for {
r <- ts.transact(xa)
} yield r._8
}
}

/*
Expand Down

0 comments on commit 6b5cefe

Please sign in to comment.