Jooq连接意外关闭

时间:2015-03-18 08:44:28

标签: playframework-2.0 jooq

我正在使用jooq和移动应用程序的播放框架。当我更频繁地向play框架的api发送请求时,偶尔会有一个与连接关闭的jooq异常抛出。这种情况只发生在我经常打电话给api的时候。

我们对此有任何线索吗?

我正在使用jooq 3.5.0,播放2.6.2,mysql 5.6.16

2015-03-18 15:41:41,097 - [ERROR] - Cannot invoke the action, eventually got an error: org.jooq.exception.DataAccessException: SQL [select `miyou_core`.`login_status_cache`.`ID`, `miyou_core`.`login_status_cache`.`USER_ID`, `miyou_core`.`login_status_cache`.`DEVICE_TOKEN`, `miyou_core`.`login_status_cache`.`USER_TOKEN`, `miyou_core`.`login_status_cache`.`TIME_EXPIRY`, `miyou_core`.`login_status_cache`.`TIME_LAST_LOGIN`, `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` from `miyou_core`.`login_status_cache` where (`miyou_core`.`login_status_cache`.`USER_TOKEN` = ? and `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` = ?)]; Connection is closed!
2015-03-18 15:41:41,100 - [ERROR] - 

! @6lg7i2paj - Internal server error, for (GET) [/api/order/fetch/obix] ->

play.api.Application$$anon$1: Execution exception[[DataAccessException: SQL [select `miyou_core`.`login_status_cache`.`ID`, `miyou_core`.`login_status_cache`.`USER_ID`, `miyou_core`.`login_status_cache`.`DEVICE_TOKEN`, `miyou_core`.`login_status_cache`.`USER_TOKEN`, `miyou_core`.`login_status_cache`.`TIME_EXPIRY`, `miyou_core`.`login_status_cache`.`TIME_LAST_LOGIN`, `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` from `miyou_core`.`login_status_cache` where (`miyou_core`.`login_status_cache`.`USER_TOKEN` = ? and `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` = ?)]; Connection is closed!]]
    at play.api.Application$class.handleError(Application.scala:296) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.DefaultApplication.handleError(Application.scala:402) [com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at scala.Option.map(Option.scala:145) [org.scala-lang.scala-library-2.11.4.jar:na]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:320) [com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:316) [com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [org.scala-lang.scala-library-2.11.4.jar:na]
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:46) [com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.Promise$class.complete(Promise.scala:55) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [org.scala-lang.scala-library-2.11.4.jar:na]
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41) [com.typesafe.akka.akka-actor_2.11-2.3.4.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393) [com.typesafe.akka.akka-actor_2.11-2.3.4.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [org.scala-lang.scala-library-2.11.4.jar:na]
Caused by: org.jooq.exception.DataAccessException: SQL [select `miyou_core`.`login_status_cache`.`ID`, `miyou_core`.`login_status_cache`.`USER_ID`, `miyou_core`.`login_status_cache`.`DEVICE_TOKEN`, `miyou_core`.`login_status_cache`.`USER_TOKEN`, `miyou_core`.`login_status_cache`.`TIME_EXPIRY`, `miyou_core`.`login_status_cache`.`TIME_LAST_LOGIN`, `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` from `miyou_core`.`login_status_cache` where (`miyou_core`.`login_status_cache`.`USER_TOKEN` = ? and `miyou_core`.`login_status_cache`.`USER_TOKEN_STATUS` = ?)]; Connection is closed!
    at org.jooq.impl.Utils.translate(Utils.java:1553) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:571) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:347) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:290) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2315) ~[org.jooq.jooq-3.5.0.jar:na]
    at com.miyou.models.sql.LoginStatusCacheModel.findByUserToken(LoginStatusCacheModel.java:66) ~[miyoucore.miyoucore-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at com.miyou.controllers.user.UserController.fetchLoginStatus(UserController.java:340) ~[miyoucore.miyoucore-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at com.miyou.utilities.secureapi.SecureApiAction.call(SecureApiAction.java:64) ~[miyoucore.miyoucore-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
    at play.core.j.JavaAction$$anonfun$11.apply(JavaAction.scala:82) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.core.j.JavaAction$$anonfun$11.apply(JavaAction.scala:82) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [org.scala-lang.scala-library-2.11.4.jar:na]
    at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:46) [com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at scala.concurrent.impl.Future$.apply(Future.scala:31) ~[org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.Future$.apply(Future.scala:492) ~[org.scala-lang.scala-library-2.11.4.jar:na]
    at play.core.j.JavaAction$class.apply(JavaAction.scala:82) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.core.Router$HandlerInvokerFactory$JavaActionInvokerFactory$$anon$15$$anon$1.apply(Router.scala:252) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:129) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:128) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at scala.Option.map(Option.scala:145) [org.scala-lang.scala-library-2.11.4.jar:na]
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:128) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:121) ~[com.typesafe.play.play_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:483) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:483) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:519) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:519) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:496) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:496) ~[com.typesafe.play.play-iteratees_2.11-2.3.7.jar:2.3.7]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [org.scala-lang.scala-library-2.11.4.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [org.scala-lang.scala-library-2.11.4.jar:na]
    ... 6 common frames omitted
Caused by: java.sql.SQLException: Connection is closed!
    at com.jolbox.bonecp.ConnectionHandle.checkClosed(ConnectionHandle.java:459) ~[com.jolbox.bonecp-0.8.0.RELEASE.jar:na]
    at com.jolbox.bonecp.ConnectionHandle.prepareStatement(ConnectionHandle.java:1011) ~[com.jolbox.bonecp-0.8.0.RELEASE.jar:na]
    at org.jooq.impl.ProviderEnabledConnection.prepareStatement(ProviderEnabledConnection.java:112) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.SettingsEnabledConnection.prepareStatement(SettingsEnabledConnection.java:76) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.AbstractResultQuery.prepare(AbstractResultQuery.java:216) ~[org.jooq.jooq-3.5.0.jar:na]
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:307) ~[org.jooq.jooq-3.5.0.jar:na]

在这里继续关于我如何使用ConnectionProvider:

public class PlayConnectionProvider implements ConnectionProvider {

  public Connection connection = null;

  @Override
  public Connection acquire() {
    if (connection == null) {
      connection = DB.getConnection();
    }
    return connection;
  }

  @Override
  public void release(Connection released) {
    if (this.connection != released) {
      throw new IllegalArgumentException("Expected " + this.connection + " but got " + released);
    }
    try {
      connection.close();
      connection = null;
    } catch (SQLException e) {
      MiyouException miyouException = MiyouException.convertToMiyouException(e);
      MiaLogger.error("message=error closing connection", miyouException);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

这个特定的实施现在一直困扰着社区。现在有一个Stack Overflow问题很好。

这是错误的,因为它不是线程安全的。您应该使用以下实现:

public class PlayConnectionProvider implements ConnectionProvider {

  @Override
  public Connection acquire() {
    return DB.getConnection();
  }

  @Override
  public void release(Connection released) {
    try {
      connection.close();
    } catch (SQLException e) {
      MiyouException miyouException = MiyouException.convertToMiyouException(e);
      MiaLogger.error("message=error closing connection", miyouException);
    }
  }
}