Pergunta

I have been using Play 2.0.2 in order to create a Java application. For a few days I'm running into a problem. After ~100 request the server starts to throw this exception:

[[SQLException: Timed out waiting for a free available connection.]]

I create a new instance of Connection with DB.getConnection(). I don't close the Connection instances because there's only one instance of each request and as far as I know it automatically closes Connection instances when the active TCP connection is closed. I tried to increase db.default.connectionTimeout value to 100 seconds but it couldn't fix the problem. Then I checked active Postgresql connections and there was no active connection. Also I restarted Postgresql but it also couldn't fix the problem.

The only solution for this problem for now is killing the Play20 instance and starting a new one.

Here is the log Play2.0 created:

! @6cg9il6ki - Internal server error, for request [GET [AN URL]] ->

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[SQLException: Timed out waiting for a free available connection.]]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.2]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.2]
        at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
        at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.2]
        at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
Caused by: java.sql.SQLException: Timed out waiting for a free available connection.
        at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:503) ~[bonecp.jar:0.7.1.RELEASE]
        at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.java:114) ~[bonecp.jar:0.7.1.RELEASE]
        at play.api.db.DBApi$class.getConnection(DB.scala:64) ~[play_2.9.1.jar:2.0.2]
        at play.api.db.BoneCPApi.getConnection(DB.scala:273) ~[play_2.9.1.jar:2.0.2]
        at play.api.db.DB$$anonfun$getConnection$1.apply(DB.scala:129) ~[play_2.9.1.jar:2.0.2]
        at play.api.db.DB$$anonfun$getConnection$1.apply(DB.scala:129) ~[play_2.9.1.jar:2.0.2]
Foi útil?

Solução

You mention that:

I don't close the Connection instances because there's only one instance of each request

That's wrong, you have to close it otherwise you end up with the issue you see.

The reason is that you are getting a direct JDBC connection, not one managed by the ORM (EBeans or some other JPA-compliant) so if you don't close it the connection may be dangling for a while.

Outras dicas

Use DB.withConnection, it closes the connection once its done.

Play framework Java implementation for database should be something like this.

try{
      Ebean.beginTransaction();

      // your code goes here
      // if all things go fine 
       Ebean.commitTransaction();


}catch (Exception e){
      //got error so log it.
      e.printStackTrace();

}finally{
      //End the transactions started
      Ebean.endTransaction();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top