【问题标题】:Play Framework and non blocking requests to PostgreSQLPlay 框架和对 PostgreSQL 的非阻塞请求
【发布时间】:2017-10-10 11:26:37
【问题描述】:

在内部,Play 框架自下而上是异步的。 Play 以异步、非阻塞的方式处理每个请求。

如何向 PostgreSQL 发出非阻塞请求?

目前的代码

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

【问题讨论】:

    标签: postgresql playframework playframework-2.0


    【解决方案1】:

    你是对的,play 在访问 Postgres 时使用了阻塞 api。 查看 Mauricio 编写的这个异步 driver
    此外,您还可以查看quill(基于 Mauricio 异步驱动程序构建)来使用一种不错的 DSL 语言来构建您的查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2020-06-28
      • 2021-11-14
      • 2013-11-12
      • 2021-08-28
      相关资源
      最近更新 更多