title: Jooq Connection Is Closed Unexpectedly
body: <p>I'm using jooq with play framework for a mobile app. When I sent request to the api of play framework more frequently, occasionally there is an jooq exception thrown related to connection is closed. This only happens when I called the api frequently.</p>
  
  <p>Do we have any clue on this?</p>
  
  <p>I'm using jooq 3.5.0, play 2.6.2, mysql 5.6.16</p>
  
  <pre class="lang-none prettyprint-override"><code>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] -&gt;
  
  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]
  </code></pre>
  
  <p>Continue here related to how I'm using the ConnectionProvider:</p>
  
  <pre><code>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);
      }
    }
  }
  </code></pre>