Skip to content

Session pool examples [Question] #980

@mrForest13

Description

@mrForest13

Hello

i am trying to understand how Session.pooled works. Can you help me with understanding this simple example? I'm wondering when the session fetches from the pool and when it returns to it. Also i have no idea how third example works and on which session sql is executed? Its because use method resources documentation says

Allocates a resource and supplies it to the given function. The resource is released as soon as the resulting F[B] is completed, whether normally or as a raised error.

object Example extends IOApp.Simple {

  private class RepositoryExample(session: Resource[IO, Session[IO]]) {

    def firstExample: IO[Completion] = {
      val row: MarketMakingOrderRow = ???

      //here we allocate session (taking from the pool) -> prepare sql -> execute sql -> return session to the pool
      session
        .flatMap(_.prepareR(MarketMakingQuery.insertMarketMakingOrder))
        .use(_.execute(row))
    }

    def secondExample: IO[Completion] = {
      val row: MarketMakingOrderRow = ???

      //same as before
      session.use { s =>
        s
          .prepare(MarketMakingQuery.insertMarketMakingOrder)
          .flatMap(_.execute(row))
      }
    }

    //here we allocate session (taking from the pool) -> prepare sql -> return session to the pool -> execute sql
    def thirdExample: IO[Completion] = {
      val row: MarketMakingOrderRow = ???

      session
        .use(_.prepare(MarketMakingQuery.insertMarketMakingOrder))
        .flatMap(_.execute(row))
    }
  }

  override def run: IO[Unit] = {

    val app = for {
      config       <- Config.loadAndValidate.toResource
      databasePool <- DatabasePool[IO](config.database)
    } yield new RepositoryExample(databasePool.sessionResource)

    app.use(_ => IO.never)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions