下面的代码块一直在抛出错误。
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 运行它。
EDIT2:经过进一步检查,似乎有许多连接处于打开状态,最终导致没有可用的连接。这可以通过调用 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, "Timeout after 20000ms of waiting for a connection"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930334/