3

I'm using play-slick with slick 3.0.0 in this way:

I got a connection by

val conn = db.createSession.conn

then got statement:

val statement = conn.prepareStatement(querySQL)

and return ResultSet:

Future{statement.executeQuery()}

But I got a problem: I tried to use this query about 50 times, then, I got the exception:

SQLTimeoutException: Timeout after 1000ms of waiting for a connection.

I know this may caused by connections are not closed and I didn't close the connection or session in my code manually.

I want to know:

  • Will connection create by my way close and return to connection pool automatically?
  • Was my situation caused by connection didn't release?
  • How to close a connection manually?

Any help would be greatly appreciated!

4

1 回答 1

1

备注:如果您发布完整代码(包括执行 50 次的调用),这将是最有帮助的

以我的方式创建的连接会关闭并自动返回连接池吗?

不会。即使 Java 7(及更高版本)提供了所谓的 try-with-resources(请参阅https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)来自动关闭您的资源。,AFAIK,这种机制在 Scala 中不可用(请纠正我,如果这不是真的)。尽管如此,Scala 提供了 LOAN 模式(尤其是参见https://wiki.scala-lang.org/display/SYGN/Loanusing),它提供了一种最终关闭资源的 FP 方式。

我的情况是连接没有释放造成的吗?

只要您不提供完整的代码,这只是一个猜测。是的,不关闭连接会使连接池超出,因此最终没有新的连接可用。

如何手动关闭连接?

connection.close()

于 2015-10-08T08:37:30.087 回答