Connection is closed

517 views
Skip to first unread message

Knut Arne Vedaa

unread,
Jan 24, 2022, 10:03:31 AM1/24/22
to HikariCP
I'm trying to debug a scenario where I get `java.sql.SQLException: Connection is closed`. This is using HikariCP 3.4.5 with Play Framework on Java 8.

The error happens in some cases when I put an SQL query, that is otherwise working fine, inside `scala.concurrent.Future`. I then get the following stack trace:

```
java.sql.SQLException: Connection is closed
        at com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection.lambda$getClosedConnection$0(ProxyConnection.java:515)
        at com.sun.proxy.$Proxy38.prepareStatement(Unknown Source)
        at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
        at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
        at org.shaqal.DBOperations.query(DBOperations.scala:53)
```

The call to `prepareStatement` is coming from my home-grown JDBC toolkit and the relevant code looks like this:

```
   try {
      val conn = getConnection()
      try {
        val prep = conn prepareStatement sql.render
```

Here, ` getConnection()` looks like this:

```
val ds = (new InitialContext).lookup(name).asInstanceOf[DataSource]
def getConnection() = ds.getConnection()
```

In other words: the error (connection is closed) seems to happen in the call to `prepareStatement`. However, the connection was retrieved on the line before. Hence it seems that it must have been closed initially, or have been closed by some other thread in between the call to `getConnection` and the call to `prepareStatement` (both of which seems relatively unlikely).

I've tried to override `getConnection` like this:

```
 override def getConnection() = {
    val conn = super.getConnection()
    logger debug s"Got connection $conn"
    logger debug s"Conn is closed? ${conn.isClosed()}"
    conn
  }
```

in order to investigate wheter the connection was actually closed initially. However, `isClosed` always returns false.

I'm using a custom `ExecutionContext` for the `Future` as recommended in the Play documentation.

What could possibly be the cause of this error? What can I do to further investigate the cause?

Reply all
Reply to author
Forward
0 new messages