4

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Matthew Fontana
  • 3,790
  • 2
  • 30
  • 50

1 Answers1

8

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()
Matthew Fontana
  • 3,790
  • 2
  • 30
  • 50
  • I tried this, but it is throwing an error the new HikariDataSource requires 2 other parameters:- ExecutionContext and Blocker – elraphty Jun 29 '21 at 13:13