Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand on hikari example #2013

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/docs/src/main/mdoc/docs/14-Managing-Connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ And running this program gives us the desired result.
HikariApp.main(Array())
```

A runnable example with Hikari + Postgres can be found [here](https://github.com/tpolecat/doobie/blob/main/modules/example/src/main/scala/example/HikariExample.scala)

### Using the JDBC DriverManager

JDBC provides a bare-bones connection provider via `DriverManager.getConnection`, which has the advantage of being extremely simple: there is no connection pooling and thus no configuration required. The disadvantage is that it is quite a bit slower than pooling connection managers, and provides no upper bound on the number of concurrent connections. It executes blocking operations on a similar unbounded pool of daemon threads.
Expand Down
59 changes: 48 additions & 11 deletions modules/example/src/main/scala/example/HikariExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,61 @@ import cats.effect._
import doobie._
import doobie.hikari._
import doobie.implicits._
import com.zaxxer.hikari.HikariConfig

object HikariExample extends IOApp.Simple {

// Typically you construct a transactor this way, using lifetime-managed thread pools.
val transactor: Resource[IO, HikariTransactor[IO]] =
val transactorRes: Resource[IO, Transactor[IO]] =
for {
ce <- ExecutionContexts.fixedThreadPool[IO](32)
xa <- HikariTransactor.newHikariTransactor[IO](
"org.h2.Driver",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
"sa", "",
ce
)
} yield xa
hikariConfig <- Resource.pure {
val config = new HikariConfig()
config.setDriverClassName("org.postgresql.Driver")
config.setJdbcUrl("jdbc:postgresql:world")
config.setUsername("postgres")
config.setPassword("password")
config
}
transactor <- HikariTransactor.fromHikariConfig[IO](hikariConfig)
} yield transactor

def run: IO[Unit] =
transactor.use { xa =>
FirstExample.examples.transact(xa).void
// Note how we allocate the Transactor once (`use`) and reuse it
// throughout our application
transactorRes.use { transactor =>
val personService = new UserService(transactor)

for {
_ <- sql"DROP TABLE if exists person".update.run.transact(transactor)
_ <- sql"CREATE TABLE person(id INT NOT NULL PRIMARY KEY, name TEXT NOT NULL)".update.run.transact(transactor)
_ <- personService.insertPerson(1, "Alice")
_ <- personService.insertPerson(2, "Bob")
name1 <- personService.getPersonName(1)
name2 <- personService.getPersonName(2)
name3 <- personService.getPersonName(3)
_ <- IO {
println(s"person 1: $name1")
println(s"person 2: $name2")
println(s"person 3: $name3")
}
} yield ()
}

}

class UserService(transactor: Transactor[IO]) {
def insertPerson(id: Int, name: String): IO[Unit] = {
sql"insert into person (id, name) values ($id, $name)"
.update
.run
.transact(transactor)
.as(())
}

def getPersonName(id: Int): IO[Option[String]] = {
sql"select name from person where id = $id"
.query[String]
.option
.transact(transactor)
}
}