0
votes

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way.

How to make non blocking requests to PostgreSQL?

Code at the moment

@Singleton
class Application @Inject()(usersRepossitory: UsersRepository, 
                            cc: ControllerComponents) extends AbstractController(cc) {

  def index = Action {
    Ok(usersRepository.list().map(_.id).mkString(","))
  }
}

case class User(id: Long)

@Singleton
class UsersRepository @Inject()(dbapi: DBApi) {

  private val db = dbapi.database("default")

  val parser: RowParser[User] = Macro.namedParser[User]

  def list: List[User] = db.withConnection { implicit connection =>
    val result: List[User] = SQL"SELECT id FROM users".as(parser.*)
    result
  }
}
1

1 Answers

0
votes

You are correct, play uses a blocking api while accessing Postgres. Check out this async driver written by Mauricio.
Also, you can check quill (which is built on top of Mauricio async driver) to have a nice DSL language to build your queries.