Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session pool examples [Question] #980

Open
mrForest13 opened this issue Sep 15, 2023 · 0 comments
Open

Session pool examples [Question] #980

mrForest13 opened this issue Sep 15, 2023 · 0 comments

Comments

@mrForest13
Copy link

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)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant