I wrote the following wrapper around play's Action that will use a function that takes both a session and a request. Here is the first version:
def ActionWithSession[A](bp: BodyParser[A])(f: Session => Request[A] => Result): Action[A] =
Action(bp) {
db.withSession {
session: DbSession =>
request => f(session)(request)
}
}
This version works well (the correct Result is returned to the browser), however each call will leak a database connection. After several calls, I started getting the following exceptions:
java.sql.SQLException: Timed out waiting for a free available connection.
When I change it to the version below (by moving the request =>
right after the Action, the connection leakage goes away, and it works.
def ActionWithSession[A](bp: BodyParser[A])(f: Session => Request[A] => Result): Action[A] =
Action(bp) { request =>
db.withSession {
session: DbSession =>
f(session)(request)
}
}
Why is the first version causing a connection to leak, and how the second version fixes that?