DbSource leaks the previous JDBC connection when db.properties is touched #163

Closed
opened 2026-09-06 07:56:04 +00:00 by tobi · 0 comments
Owner

Summary

DbSource.getConnection() opens a replacement JDBC connection whenever db.properties has been modified since it was last read, but discards the previously cached connection without closing it — leaking one open socket per thread that still held a live connection to that DbSource at the time.

Where

src/main/java/helma/objectmodel/db/DbSource.java, getConnection():

boolean fileUpdated = props.lastModified() > lastRead || ...;

if (con == null || con.isClosed() || fileUpdated) {
    init();
    con = DriverManager.getConnection(url, conProps);
    if (tx != null) {
        tx.registerConnection(this, con);
    } else {
        connection.set(con);
    }
}

When fileUpdated is true but the cached con is still open and healthy (not null, not isClosed()), this branch still runs: it opens a brand-new Connection via DriverManager.getConnection() and overwrites the cache entry — tx.registerConnection(this, con) replaces whatever Transactor.sqlConnections held for this DbSource, and connection.set(con) does the same for the non-Transactor ThreadLocal path. Neither branch closes the connection being replaced. Nothing else references it afterward, so it stays open on the database side until the driver's finalizer runs (unreliable) or the server's own wait_timeout eventually kills it.

This is distinct from #161 (closed, wrong diagnosis) — worker-thread idle timeout is already handled correctly via the finally { tx.closeConnections(); } block wrapping run()'s outer loop. This leak instead fires on every thread that still holds a live connection to a given DbSource at the moment its db.properties is next read as modified — one leaked connection per such thread, per file touch.

Suggested fix

Close the stale connection before replacing it:

if (con == null || con.isClosed() || fileUpdated) {
    if (con != null && !con.isClosed()) {
        try {
            con.close();
        } catch (SQLException ignore) {
            // already going away
        }
    }
    init();
    con = DriverManager.getConnection(url, conProps);
    ...
}
## Summary `DbSource.getConnection()` opens a replacement JDBC connection whenever `db.properties` has been modified since it was last read, but discards the previously cached connection without closing it — leaking one open socket per thread that still held a live connection to that `DbSource` at the time. ## Where `src/main/java/helma/objectmodel/db/DbSource.java`, `getConnection()`: ```java boolean fileUpdated = props.lastModified() > lastRead || ...; if (con == null || con.isClosed() || fileUpdated) { init(); con = DriverManager.getConnection(url, conProps); if (tx != null) { tx.registerConnection(this, con); } else { connection.set(con); } } ``` When `fileUpdated` is true but the cached `con` is still open and healthy (not `null`, not `isClosed()`), this branch still runs: it opens a brand-new `Connection` via `DriverManager.getConnection()` and overwrites the cache entry — `tx.registerConnection(this, con)` replaces whatever `Transactor.sqlConnections` held for this `DbSource`, and `connection.set(con)` does the same for the non-Transactor `ThreadLocal` path. Neither branch closes the connection being replaced. Nothing else references it afterward, so it stays open on the database side until the driver's finalizer runs (unreliable) or the server's own `wait_timeout` eventually kills it. This is distinct from #161 (closed, wrong diagnosis) — worker-thread idle timeout is already handled correctly via the `finally { tx.closeConnections(); }` block wrapping `run()`'s outer loop. This leak instead fires on every thread that still holds a live connection to a given `DbSource` at the moment its `db.properties` is next read as modified — one leaked connection per such thread, per file touch. ## Suggested fix Close the stale connection before replacing it: ```java if (con == null || con.isClosed() || fileUpdated) { if (con != null && !con.isClosed()) { try { con.close(); } catch (SQLException ignore) { // already going away } } init(); con = DriverManager.getConnection(url, conProps); ... } ```
tobi self-assigned this 2026-09-06 07:56:04 +00:00
tobi closed this issue 2026-09-06 08:43:59 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
antville/helma#163
No description provided.