Federico Mariani created CAMEL-26986:
----------------------------------------
Summary: camel-jdbc - the producer does not close a connection
that already reports isClosed(), so an Agroal pool loses one connection per
connection error
Key: CAMEL-26986
URL: https://issues.apache.org/jira/browse/CAMEL-26986
Project: Camel
Issue Type: Bug
Components: camel-jdbc
Affects Versions: 4.14.9, 4.18.5, 4.23.0
Reporter: Federico Mariani
{{JdbcProducer.closeQuietly(Connection)}} only calls {{close()}} when
{{isClosed()}} returns false
({{components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcProducer.java}},
lines 350-360 at {{camel-4.23.0}}):
{code:java}
if (!con.isClosed()) {
con.close();
}
{code}
{{isClosed() == true}} does not mean that {{close()}} was called. The
{{Connection.isClosed()}} javadoc: "A connection is closed if the method close
has been called on it or if certain fatal errors have occurred." pgjdbc, for
one, closes its connection itself when it loses the server
({{QueryExecutorBase.abort()}} on an I/O error, or after the FATAL error of a
terminated session), so the next {{isClosed()}} returns true.
Whether the pooled connection then goes back to the pool depends on the pool:
* *Agroal* (3.2.1, same on master): {{ConnectionWrapper.isClosed()}} returns
{{isClosed()}} of the physical connection. The producer never closes the
wrapper, so the connection stays checked out for good. After {{maxSize}} such
errors every {{getConnection()}} waits for {{acquisitionTimeout}}: forever with
Agroal's default (0), or every exchange fails with {{Sorry, acquisition
timeout!}}. Agroal is the pool of Camel Quarkus.
* *HikariCP* (7.1.0): {{ProxyConnection.isClosed()}} is true only after its own
{{close()}} or after Hikari evicted the broken connection, which already
removed it from the pool. Not affected.
* *DBCP2* (2.14.0): {{PoolableConnection.isClosed()}} returns the connection to
the pool itself when the physical one is closed; its javadoc: "This method
should not be used by a client to determine whether or not a connection should
be return to the connection pool (by calling close()). Clients should always
attempt to return a connection to the pool once it is no longer required." Not
affected.
JDBC defines {{close()}} on a closed connection as a no-op, and the {{catch}}
around it already logs at DEBUG.
The guard came with CAMEL-9845 (2016, "Silent ignore close errors"), whose aim
was to avoid error logs on resources that are already closed; that is covered
by the DEBUG log in the {{catch}}. The same guard is in
{{closeQuietly(ResultSet)}} (line 319) and {{closeQuietly(Statement)}} (line
331).
h3. Steps to reproduce
Route {{from("direct:sql").to("jdbc:ds")}}, with {{ds}} an Agroal pool of 2
connections ({{acquisitionTimeout}} 1 s, {{PostgreSQLExceptionSorter}}). Three
times: an insert (must succeed), all the database sessions end (e.g.
{{pg_terminate_backend}} on PostgreSQL), an insert (fails, as expected).
Reproduced with Agroal over pgjdbc 42.7.13 against PostgreSQL 17, and with H2
behind a small test driver whose connections, like pgjdbc's, close themselves
once their database session has ended (with Agroal, HikariCP and DBCP2).
h3. Actual (4.23.0 and 4.24.0-SNAPSHOT)
Agroal: the third insert fails, both connections of the pool are still checked
out:
{noformat}
java.sql.SQLException: Sorry, acquisition timeout!
{noformat}
HikariCP and DBCP2: the inserts succeed.
h3. Expected
The connection goes back to the pool after each error; the pool replaces it and
later inserts succeed.
h3. Suggested fix
Call {{close()}} unconditionally in {{closeQuietly(Connection)}}, keeping the
{{catch}} with the DEBUG log (and the same in {{closeQuietly(ResultSet)}} and
{{closeQuietly(Statement)}} for consistency). With that change the scenario
above passes with Agroal, and HikariCP and DBCP2 still pass.
{{camel-sql}} is not affected (it closes without the guard). {{camel-duckdb}}
has the same guard in {{DuckDbConnectionScope.close()}} for connections from a
{{DataSource}}.
h3. Related
* CAMEL-9845: added the {{isClosed()}} guard.
* CAMEL-8153: an earlier connection leak of the {{StreamList}} mode, with
another pool.
_Claude Code on behalf of Croway_
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)