Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doobie with Hikari setup

I want to get the hikari transactor setup to act just like the standard transactor

val xa = HikariTransactor.newHikariTransactor[IO](
  "com.mysql.jdbc.Driver",
  JdbcUrl,
  Username,
  Password
)

sql"""select DISTINCT gcpProject FROM JobStatus"""
     .query[String]    // Query0[String]
     .stream           // Stream[ConnectionIO, String]
     .take(5)          // Stream[ConnectionIO, String]
     .compile.toList   // ConnectionIO[List[String]]
     .transact(xa)     // IO[List[String]]
     .unsafeRunSync    // List[String]
     .foreach(println) // Unit

Unfortunately this gives me:

Type mismatch, expected: tansactor.Transactor[NotInferedM], actual: IO[hikari.HikariTransactor[IO]]

Any ideas on how can can get this working properly?

Just a note that the previous solution used a single connection each time and works correctly:

val xa = Transactor.fromDriverManager[IO](
  "com.mysql.jdbc.Driver",
  JdbcUrl,
  Username,
  Password
)

But I could really use a connection pool.

like image 788
Matthew Fontana Avatar asked Jan 28 '23 15:01

Matthew Fontana


1 Answers

Okay after a bunch of testing I finally worked it out!

I'll post my solution here just in case someone else finds this helpful:

val config = new HikariConfig()
config.setJdbcUrl(JdbcUrl)
config.setUsername(Username)
config.setPassword(Password)
config.setMaximumPoolSize(databaseConnectionPoolSize)

val DbTransactor: IO[HikariTransactor[IO]] =
  IO.pure(HikariTransactor.apply[IO](new HikariDataSource(config)))

sql"""select DISTINCT gcpProject FROM JobStatus"""
     .query[String]
     .stream
     .take(5)
     .compile.toList

val prog = for {
  xa <- transactor
  result <- query.transact(xa)
} yield result
prog.unsafeRunSync()
like image 193
Matthew Fontana Avatar answered Feb 05 '23 00:02

Matthew Fontana