Connecting with a Transactor
Set up database access.
What Is a Transactor?
Doobie is a pure functional JDBC layer for Scala. A Transactor[F] is the bridge between Doobie's pure programs and a real database connection.
A Transactor knows how to acquire a JDBC Connection, run your program inside a transaction, and release resources safely.
It is parameterized by an effect type F[_] such as cats.effect.IO.
import doobie._
import cats.effect.IO
val xa: Transactor[IO] = ???The ConnectionIO Program
Doobie queries are values of type ConnectionIO[A]. They describe a computation that needs a JDBC connection but do not run yet.
You build these programs purely, then hand them to a Transactor to execute them. Nothing touches the database until you run it.
import doobie.implicits._
val program: ConnectionIO[Int] =
sql"select 42".query[Int].uniqueAll lessons in this course
- Connecting with a Transactor
- Running Queries
- Inserts and Updates
- Composing Transactions