playframework/playframework — pull request #3107 by cchantep (2014-06-30 20:47:12+00:00)
Title: Anorm leaks resources if all results are not read

Anorm closes the `java.sql.Statement` only once all records have been traversed in the `java.sql.ResultSet`.

https://github.com/playframework/playframework/blob/master/framework/src/anorm/src/main/scala/anorm/Anorm.scala#L366

This introduces the possibility of a resource leak. Examples:

1. The caller intentionally does not read all records.

```
SQL("SELECT id FROM country")().head
```

Admittedly, this is a dumb example. A more realistic example might involve `takeWhile`, or something similar.

2. An exception occurs before all records can be read.

```
val a = SQL("SELECT id FROM country")()
val b = SQL("SELECT id, country_id FROM province")()
```

If there is an exception is thrown while querying for `b`, the `Statement` created by `a` will never be closed.

These examples are not exhaustive.

The severity of this depends on the JDBC. For example, Oracle will keep resources for a open `ResultSet/Statement`, even after the `Connection` has been closed.