Search code examples
scaladoobie

Why does Doobie use free monad?


It seems that simple

type Db[F[_], A] = Kleisli[F, Connection, A]
type Transactor[DB[_], F[_]] = DB ~> F

Сan be used to build functional JDBC layer as well


Solution

  • Summed up from @SystemFw and @tpolecat answers from https://gitter.im/scala/scala?at=5cfe6505bf4cbd167c619960

    Pros of Free Monad:

    • client code has no access to Connection instance and therefore can't leak it
    • there is no user's F[_] so improper (asynchronous) effect can't be used within transaction boundary. It is important since most JDBC drivers have designed java.sql.Connection implementation to be single-threaded.

    Cons of Free Monad:

    • client code has no access to Connection and therefore can't use alternative ORMs (like JOOQ)
    • there is no user's F[_] so you can't nest specific effects within transaction.

    Doobie will have tagless version in the future.