typelevel / typelevel/skunk

Session pool examples [Question]

Open
#980 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.7k
Forks
175
Avg merge
5d 14h
Merged PRs (30d)
9

Description

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)
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Session.pooled, Resource.use, and DatabasePool.sessionResource entry points shown in the example. Clarify when the session is acquired and released in each example, especially which session executes the SQL in thirdExample, and document the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgres, scala
Domain
database, documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.