mysql – Scala/Slick,“超时后20000ms等待连接”错误

下面的代码块已经抛出了错误。

  Timeout after 20000ms of waiting for a connection.","stackTrace":[{"file":"BaseHikariPool.java","line":228,"className":"com.zaxxer.hikari.pool.BaseHikariPool","method":"getConnection"

此外,我的数据库访问看起来太慢了,xs.map()的每个元素大约需要1秒钟。下面,getFutureItem()调用db.run()。

xs.map{ x => 
    val item: Future[List[Sometype], List(Tables.myRow)] = getFutureItem(x)         
    Await.valueAfter(item, 100.seconds) match {
        case Some(i) => i
        case None => println("Timeout getting items after 100 seconds")
    }
}

Slick使用“x”值的每次迭代记录它:

[akka.actor.default-dispatcher-3] [akka://user/IO-HTTP/listener-0/24] Connection was PeerClosed, awaiting TcpConnection termination...
[akka.actor.default-dispatcher-3] [akka://user/IO-HTTP/listener-0/24] TcpConnection terminated, stopping
[akka.actor.default-dispatcher-3] [akka://system/IO-TCP/selectors/$a/0] New connection accepted
[akka.actor.default-dispatcher-7] [akka://user/IO-HTTP/listener-0/25] Dispatching POST request to http://localhost:8080/progress to handler Actor[akka://system/IO-TCP/selectors/$a/26#-934408297]

我的配置:

"com.zaxxer" % "HikariCP" % "2.3.2"

default_db {
  url = ...
  user = ...
  password = ...
  queueSize = -1
  numThreads = 16
  connectionPool = HikariCP
  connectionTimeout = 20000
  maxConnections = 40
}

有没有什么明显的,我做错了,导致这些数据库访问是这么慢,并抛出这个错误?如果需要,我可以提供更多的信息。

编辑:我已经收到一个建议,该问题可能是一个类加载器错误,我可以通过将项目部署为单个.jar来解决它,而不是使用sbt来运行它。

编辑2:进一步检查后,似乎有许多连接被打开,最终导致没有可用的连接。这可能会通过调用db.close()在适当的时间关闭连接来解决。

EDIT3:已解决由slick创建的连接超出了我的mysql配置允许的最大连接数。

最佳答案
OP写道:

EDIT2: After further inspection, it appears that many connections were being left open, which eventually led to no connections being available. This can likely be resolved by calling db.close() to close the connection at the appropriate time.

EDIT3: Solved. The connections made by slick exceeded the max connections allowed by my mysql config.

转载注明原文:mysql – Scala/Slick,“超时后20000ms等待连接”错误 - 代码日志