5

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

Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35
  • 7
    This is a very interesting question, however it may be closed as it is mainly opinion based, and the only one who can give an objective answer is Rob Norris himself. Anyways, I took the liberty of crosspost this in the **Scala** Gitter channel. Rob and Fabio Labella did answer: _"The Free representation has the advantage that you cannot mix connections (accidentally) and the disadvantage that you cannot mix connections (on purpose, e.g streaming from one Db to another)"_ - Fabio. – Luis Miguel Mejía Suárez Jun 10 '19 at 14:37
  • 5
    _"An early version of doobie was concrete Kleisli like that, and the current default free interpreter interprets into Kleisli. As Fabio notes, you can't leak the connection with Free because there is no connection. This was the motivation for using Free. It was kind of in response to Slick (pre-3.0) which made it very easy to leak connections"_ - Rob. – Luis Miguel Mejía Suárez Jun 10 '19 at 14:38
  • 4
    Also, both of them confirmed that there will be a **tagless** version of doobie. Apparently there is a branch that already does that and probably the next release will be fully **tagless**. Source of all that I have _"said"_: https://gitter.im/scala/scala?at=5cfe6505bf4cbd167c619960 – Luis Miguel Mejía Suárez Jun 10 '19 at 14:41
  • @LuisMiguelMejíaSuárez Thank you for your efforts! – Sergey Alaev Jun 10 '19 at 17:26

1 Answers1

4

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.

Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35