播放服务器随机无法与 Mysql 建立连接
Play server randomly unable to establish connection to Mysql
所以在过去的几天里,我一直被这个问题所困扰,我不确定是什么原因造成的,也不知道从哪里开始寻找。我正在使用 docker 容器 (T2.Small) + RDS(T2.micro) 在 Amazon Beanstalk 上 运行ning Play 2.4.1、Anorm 2.5、MySql 5.6。我在向服务器发出 API 请求时遇到间歇性问题。我的日志文件已满,出现以下错误。它抱怨与数据库的连接超时,但如果我检查 RDS,它仍然显示所有连接都处于活动状态。如果我远程进入 MySql 服务器和 运行 SHOW processlist
,我会看到所有连接,它们都说命令:睡眠。有时,如果我只是让服务器静置几分钟,它就会重新开始工作。如果我重新启动 Play 应用程序,它会立即重新开始工作。我似乎无法可靠地重新创建导致它的任何事件集。
任何关于从哪里开始寻找的建议或提示都将不胜感激,因为这让我抓狂。
[ERROR] from play.core.server.netty.PlayDefaultUpstreamHandler in application-akka.actor.default-dispatcher-186 - Cannot invoke the action
play.api.UnexpectedException: Unexpected exception[SQLTimeoutException: Timeout after 30000ms of waiting for a connection.]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:261) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [com.typesafe.play.play-netty-server_2.11-2.4.1.jar:2.4.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [com.typesafe.play.play-netty-server_2.11-2.4.1.jar:2.4.1]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Failure$$anonfun$recover.apply(Try.scala:216) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Failure.recover(Try.scala:216) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [org.scala-lang.scala-library-2.11.7.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [org.scala-lang.scala-library-2.11.7.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [com.typesafe.akka.akka-actor_2.11-2.3.11.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [com.typesafe.akka.akka-actor_2.11-2.3.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [org.scala-lang.scala-library-2.11.7.jar:na]
Caused by: java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection.
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:227) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:182) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at play.api.db.DefaultDatabase.getConnection(Databases.scala:143) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DefaultDatabase.withConnection(Databases.scala:153) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DefaultDatabase.withConnection(Databases.scala:149) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DB$.withConnection(DB.scala:61) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at utils.Util$.getCurrentPersonIdAndRole(Util.scala:29) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at utils.globals$UserAction$.invokeBlock(globals.scala:128) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at utils.globals$UserAction$.invokeBlock(globals.scala:122) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at play.api.mvc.ActionBuilder$$anon.apply(Action.scala:493) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at scala.Option.map(Option.scala:146) ~[org.scala-lang.scala-library-2.11.7.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM.apply(Iteratee.scala:741) ~[com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM.apply(Iteratee.scala:741) ~[com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [org.scala-lang.scala-library-2.11.7.jar:na]
... 6 common frames omitted
没关系,我想通了。在代码中的一个地方,我使用 DB.getConnection 而不是 DB.withConnection{} 并且我忘记将连接释放回池中。因此,在调用有问题的函数 10 次后,连接池为空但仍处于活动状态,因此所有其他调用都失败了。
所以在过去的几天里,我一直被这个问题所困扰,我不确定是什么原因造成的,也不知道从哪里开始寻找。我正在使用 docker 容器 (T2.Small) + RDS(T2.micro) 在 Amazon Beanstalk 上 运行ning Play 2.4.1、Anorm 2.5、MySql 5.6。我在向服务器发出 API 请求时遇到间歇性问题。我的日志文件已满,出现以下错误。它抱怨与数据库的连接超时,但如果我检查 RDS,它仍然显示所有连接都处于活动状态。如果我远程进入 MySql 服务器和 运行 SHOW processlist
,我会看到所有连接,它们都说命令:睡眠。有时,如果我只是让服务器静置几分钟,它就会重新开始工作。如果我重新启动 Play 应用程序,它会立即重新开始工作。我似乎无法可靠地重新创建导致它的任何事件集。
任何关于从哪里开始寻找的建议或提示都将不胜感激,因为这让我抓狂。
[ERROR] from play.core.server.netty.PlayDefaultUpstreamHandler in application-akka.actor.default-dispatcher-186 - Cannot invoke the action
play.api.UnexpectedException: Unexpected exception[SQLTimeoutException: Timeout after 30000ms of waiting for a connection.]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:261) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [com.typesafe.play.play-netty-server_2.11-2.4.1.jar:2.4.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$apply.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [com.typesafe.play.play-netty-server_2.11-2.4.1.jar:2.4.1]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Failure$$anonfun$recover.apply(Try.scala:216) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Try$.apply(Try.scala:192) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.util.Failure.recover(Try.scala:216) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Future$$anonfun$recover.apply(Future.scala:324) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [org.scala-lang.scala-library-2.11.7.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [org.scala-lang.scala-library-2.11.7.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [com.typesafe.akka.akka-actor_2.11-2.3.11.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [com.typesafe.akka.akka-actor_2.11-2.3.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [org.scala-lang.scala-library-2.11.7.jar:na]
Caused by: java.sql.SQLTimeoutException: Timeout after 30000ms of waiting for a connection.
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:227) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:182) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93) ~[com.zaxxer.HikariCP-2.3.7.jar:na]
at play.api.db.DefaultDatabase.getConnection(Databases.scala:143) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DefaultDatabase.withConnection(Databases.scala:153) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DefaultDatabase.withConnection(Databases.scala:149) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at play.api.db.DB$.withConnection(DB.scala:61) ~[com.typesafe.play.play-jdbc_2.11-2.4.1.jar:2.4.1]
at utils.Util$.getCurrentPersonIdAndRole(Util.scala:29) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at utils.globals$UserAction$.invokeBlock(globals.scala:128) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at utils.globals$UserAction$.invokeBlock(globals.scala:122) ~[datagem.datagem-1.0-SNAPSHOT-sans-externalized.jar:na]
at play.api.mvc.ActionBuilder$$anon.apply(Action.scala:493) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:105) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:104) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:103) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at scala.Option.map(Option.scala:146) ~[org.scala-lang.scala-library-2.11.7.jar:na]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:96) ~[com.typesafe.play.play_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM.apply(Iteratee.scala:741) ~[com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM.apply(Iteratee.scala:741) ~[com.typesafe.play.play-iteratees_2.11-2.4.1.jar:2.4.1]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24) [org.scala-lang.scala-library-2.11.7.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [org.scala-lang.scala-library-2.11.7.jar:na]
... 6 common frames omitted
没关系,我想通了。在代码中的一个地方,我使用 DB.getConnection 而不是 DB.withConnection{} 并且我忘记将连接释放回池中。因此,在调用有问题的函数 10 次后,连接池为空但仍处于活动状态,因此所有其他调用都失败了。