Skip to content

Make dbIsValid() return FALSE after disconnecting a JDBC connection - #347

Merged
schuemie merged 2 commits into
OHDSI:developfrom
DG47:fix/dbisvalid-after-disconnect
Nov 26, 2026
Merged

schuemie merged 2 commits into
OHDSI:developfrom
DG47:fix/dbisvalid-after-disconnect

Conversation

@DG47

@DG47 DG47 commented Oct 27, 2026

Copy link
Copy Markdown

Fixes #248, fixes #264.

For JDBC connections dbIsValid() only checked whether the Java connection reference is null. disconnect() closes the connection but leaves the reference in place, so dbIsValid() kept returning TRUE on a closed connection.

Reproduction on develop (Postgres 16, PostgreSQL JDBC 42.7.3):

con <- connect(dbms = "postgresql", server = "localhost/postgres", user = "postgres", password = "...")
DBI::dbIsValid(con)
#> [1] TRUE
disconnect(con)
DBI::dbIsValid(con)
#> [1] TRUE
querySql(con, "SELECT 1 AS x;")
#> Error executing SQL:
#> org.postgresql.util.PSQLException: This connection has been closed.
#> An error report has been created at ...
disconnect(con)   # no "Connection is already closed" warning

Because of this, the existing if (!DBI::dbIsValid(connection)) abort("Connection is closed") checks in executeSql(), querySql(), dbGetQuery(), querySqlToAndromeda() and dbSendQuery() never fired for JDBC connections; the call went on to the driver instead. The "Connection is already closed" warning in disconnect() had the same problem. SQLite and DuckDB connections already behaved correctly, since they delegate to the underlying DBI driver.

Changes:

  • dbIsValid() for DatabaseConnectorJdbcConnection returns FALSE when the reference is null, and otherwise returns !isClosed() from the JDBC connection.
  • disconnect() for JDBC connections uses dbIsValid() to decide whether the connection is already closed. When it is, it warns and returns without calling close() again. Before, it would also try to call close() on a null reference.
  • New test in tests/testthat/test-connection.R, run for every test server: after disconnect(), dbIsValid() is FALSE, querySql() fails with "Connection is closed", and a second disconnect() warns.

Testing:

  • New test against a local Postgres 16 container plus SQLite and DuckDB: 3 failures on Postgres on develop (SQLite and DuckDB already pass). All pass with this change.
  • Full testthat suite with Postgres (Eunomia GiBleed loaded into a cdm schema), SQLite and DuckDB: 65 tests, 0 failures. The one error, in test-dbplyr.R ("sql_translation works with jdbc connections", from the locally installed bigrquery version), also happens on develop.
  • R CMD check --no-manual --ignore-vignettes: Status: OK.

NEWS.md is left for the maintainer.

dbIsValid() only checked whether the Java connection reference was null,
which it never is after disconnect(), so it kept returning TRUE for closed
JDBC connections. It now also asks the driver whether the connection is
closed. This also makes the 'Connection is closed' checks in querySql(),
executeSql() and friends, and the 'Connection is already closed' warning in
disconnect(), work for JDBC connections.

Fixes OHDSI#248, fixes OHDSI#264
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (c7a5bed) to head (eb2a0b6).

Files with missing lines Patch % Lines
R/DBI.R 0.00% 3 Missing ⚠️
R/Connect.R 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #347       +/-   ##
===========================================
- Coverage    56.43%   0.00%   -56.44%     
===========================================
  Files           16      16               
  Lines         2803    2806        +3     
===========================================
- Hits          1582       0     -1582     
- Misses        1221    2806     +1585     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DG47

DG47 commented Oct 27, 2026

Copy link
Copy Markdown
Author

A note on the Codecov report: the 0% comes from the CI run, not from the change. tests/testthat.R only runs the suite when CDM5_POSTGRESQL_SERVER is set, and the test-server secrets aren't passed to workflows on fork PRs, so no tests ran here and covr measured nothing (project coverage 56.43% → 0.00%). Running covr locally against Postgres 16, SQLite and DuckDB, all changed lines in R/DBI.R and R/Connect.R are covered. The null-reference branch of dbIsValid() is covered by a test that needs no server (eb2a0b6). R CMD check is OK. It should report normally when CI runs with secrets.

@DG47

DG47 commented Nov 8, 2026

Copy link
Copy Markdown
Author

For JDBC connections dbIsValid() only checked whether the Java connection reference was null, and disconnect() leaves that reference in place, so dbIsValid() kept returning TRUE on a closed connection and the "Connection is closed" checks in querySql() and friends never fired (#248 and #264). It now returns !isClosed() from the JDBC connection, and disconnect() uses it to warn instead of calling close() twice. @schuemie, can you take a review pass on this when you are next in DatabaseConnector? NEWS.md is left for you.

@DG47

DG47 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Bumping this one, @schuemie. It fixes #248 and #264: dbIsValid() kept returning TRUE after disconnect() on JDBC connections, so the "Connection is closed" checks in querySql() and executeSql() never fired, and it now also checks isClosed() on the JDBC connection. Could you review it?

@schuemie
schuemie merged commit 73bf6ab into OHDSI:develop Nov 26, 2026
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants