xring
xring

Reputation: 767

SQLTimeoutException in play-slick

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:

Any help would be greatly appreciated!

Upvotes: 3

Views: 522

Answers (1)

Martin Senne
Martin Senne

Reputation: 6059

Remark: It would most helpful, if you post your full code (including your call that is performed 50 times)

Will connection create by my way close and return to connection pool automatically?

No. Even though Java 7 (and up) provides the so called try-with-resources (see https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html ) to auto-close your resource. ,AFAIK, this mechanism is not available in Scala (please correct me somebody, if this is not true). Still, Scala provides the LOAN-Pattern ( see https://wiki.scala-lang.org/display/SYGN/Loan , especially using ) which provides a FP way of closing resources finally.

Was my situation caused by connection didn't release?

As long as you don't provide your full code, it is only a guess. Yes, not closing connections make the connection pool exceed, thus that no new connections are available eventually.

How to close a connection manually?

connection.close()

Upvotes: 1

Related Questions