首页 > 解决方案 > 为什么我的数据库读取会阻止我的播放框架服务器?

问题描述

我有一个使用 AWS EC2 实例托管在 AWS RDS 和 Play 框架服务器上的 mySQL 数据库。我所有的其他 API 端点都工作正常,但是每次对我的数据库进行多个连接时,我的服务器似乎都会关闭一段时间。我做了一些研究,我认为这可能与线程池或请求被阻塞有关。这是我的操作代码:

def getDataFromDB = Action {
  val conn = db.getConnection()
  var qJsonArray: JsArray = Json.arr()

  try {
    val stmt = conn.createStatement
    val query = "SELECT * FROM table"
    val rs = stmt.executeQuery(query)
    val rsmd = rs.getMetaData
    val columnCount = rsmd.getColumnCount

    while (rs.next) {
      var index = 1

      var rsJson: JsObject = Json.obj()
      while (index <= columnCount) {
        val column = rsmd.getColumnLabel(index)
        val columnLabel = column.toLowerCase()
        val value = rs.getObject(column)

        if (value == null) {
          rsJson = rsJson ++ Json.obj(
            columnLabel -> JsNull
          )
        } else if (value.isInstanceOf[Integer]) {
          rsJson = rsJson ++ Json.obj(
            columnLabel -> value.asInstanceOf[Int]
          ) 
        } else if (value.isInstanceOf[String]) {
          rsJson = rsJson ++ Json.obj(
            columnLabel -> value.asInstanceOf[String]
          )
        } else if (value.isInstanceOf[Date]) {
          rsJson = rsJson ++ Json.obj(
            columnLabel -> value.asInstanceOf[Date].getTime
          )
        } else {
          throw new IllegalArgumentException("oh no")
        }
        index += 1
      }
      qJsonArray = qJsonArray :+ rsJson
    }
  } finally {
    conn.close()
  }
  Ok(qJsonArray)
}

标签: databasescalaplayframeworkthreadpoolblocking

解决方案


推荐阅读