typelevel / typelevel/doobie

Extend the ConnectionPool Example to showcase using the transactor in a larger app

Open
#795 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.2k
Forks
379
Avg merge
14m
Merged PRs (30d)
8

Description

Thank you very much for creating doobie!
Hope you don't mind me posting such a noob question, but I am just not sure if what I intend to do is a dump idea or the way to go?
The connection pool example shows how to create an connection as a ressource

object HikariApp extends IOApp {
  // Resource yielding a transactor configured with a bounded connect EC and an unbounded
  // transaction EC. Everything will be closed and shut down cleanly after use.
  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.h2.Driver",                        // driver classname
      "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",   // connect URL
      "sa",                                   // username
      "",                                     // password
      ce,                                     // await connection here
      te                                      // execute JDBC operations here
    )
  } yield xa


  def run(args: List[String]): IO[ExitCode] =
    transactor.use { xa: HikariTransactor[IO] =>
      // Construct and run your server here!
        sql"""
        create table if not exists numbers(id serial auto_increment primary key,
        value integer not null)
        """.update.run.transact(xa).unsafeRunSync()
      sql"insert into numbers (value) values(1)".update.run.transact(xa).unsafeRunSync()

/*
/Pass xa to other functions which need to access the database either synchronously or asynchronously like f(xa, ...), g(xa, ...)
*/

      println(sql"select value from numbers".query[Int].to[List].transact(xa).unsafeRunSync())
     ExitCode.Success.pure[IO]
    }
}

Is it correct to pass a reference to the transactor around like this? Are their any caveats that apply?
Many thanks again, any help would be greatly appreciated!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/example/src/main/scala/example/HikariExample.scala and read how the Resource and HikariTransactor are used in HikariApp.run. Clarify the intended larger-application example and the transactor-passing caveats before changing it; done would be an agreed example that demonstrates the recommended usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
databases, documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.