From e22125a7b28b2ae6667d7d3ae664498d6fbbbdca Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Fri, 7 Jun 2024 19:01:43 +0900 Subject: [PATCH] Revert "perf: use `HikariCP` instead of `JDBC DriverManager` for manage connection" This reverts commit 6b5cefeee325ba561d948b36b2e9b382dd9e142b. --- project/Dependencies.scala | 1 - .../db/doobie/DoobieTransactor.scala | 49 +++++-------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 83019b77..009064b8 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -24,7 +24,6 @@ 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", diff --git a/src/main/scala/net/yoshinorin/qualtet/infrastructure/db/doobie/DoobieTransactor.scala b/src/main/scala/net/yoshinorin/qualtet/infrastructure/db/doobie/DoobieTransactor.scala index 885073e7..075583a6 100644 --- a/src/main/scala/net/yoshinorin/qualtet/infrastructure/db/doobie/DoobieTransactor.scala +++ b/src/main/scala/net/yoshinorin/qualtet/infrastructure/db/doobie/DoobieTransactor.scala @@ -5,9 +5,7 @@ import scala.concurrent.ExecutionContextExecutor import cats.implicits.catsSyntaxApplicativeId import doobie.* import doobie.implicits.* -// import doobie.util.transactor.Transactor.Aux -import doobie.hikari.* -import com.zaxxer.hikari.HikariConfig +import doobie.util.transactor.Transactor.Aux import cats.effect.* import cats.effect.IO import net.yoshinorin.qualtet.actions.{Action, Continue, Done} @@ -22,7 +20,6 @@ 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, @@ -30,56 +27,32 @@ class DoobieTransactor(config: DBConfig) extends Transactor[ConnectionIO, IO] { 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] = - transactor.use { xa => - connectionIO.transact(xa) - } + override def transact[T](connectionIO: ConnectionIO[T]): IO[T] = connectionIO.transact(transactor) override def transact[R](action: Action[R]): IO[R] = transact(perform(action)) override def transact2[T1, T2](ts: (ConnectionIO[(T1, T2)])): IO[T2] = { - transactor.use { xa => - for { - r <- ts.transact(xa) - } yield r._2 - } + for { + r <- ts.transact(transactor) + } yield r._2 } override def transact4[T1, T2, T3, T4](ts: (ConnectionIO[(T1, T2, T3, T4)])): IO[T4] = { - transactor.use { xa => - for { - r <- ts.transact(xa) - } yield r._4 - } + for { + r <- ts.transact(transactor) + } 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] = { - transactor.use { xa => - for { - r <- ts.transact(xa) - } yield r._8 - } + for { + r <- ts.transact(transactor) + } yield r._8 } /*