Repository navigation
Make dbIsValid() return FALSE after disconnecting a JDBC connection - #347
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
|
A note on the Codecov report: the 0% comes from the CI run, not from the change. |
|
For JDBC connections |
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, sodbIsValid()kept returningTRUEon a closed connection.Reproduction on
develop(Postgres 16, PostgreSQL JDBC 42.7.3):Because of this, the existing
if (!DBI::dbIsValid(connection)) abort("Connection is closed")checks inexecuteSql(),querySql(),dbGetQuery(),querySqlToAndromeda()anddbSendQuery()never fired for JDBC connections; the call went on to the driver instead. The "Connection is already closed" warning indisconnect()had the same problem. SQLite and DuckDB connections already behaved correctly, since they delegate to the underlying DBI driver.Changes:
dbIsValid()forDatabaseConnectorJdbcConnectionreturnsFALSEwhen the reference is null, and otherwise returns!isClosed()from the JDBC connection.disconnect()for JDBC connections usesdbIsValid()to decide whether the connection is already closed. When it is, it warns and returns without callingclose()again. Before, it would also try to callclose()on a null reference.tests/testthat/test-connection.R, run for every test server: afterdisconnect(),dbIsValid()isFALSE,querySql()fails with "Connection is closed", and a seconddisconnect()warns.Testing:
develop(SQLite and DuckDB already pass). All pass with this change.testthatsuite with Postgres (Eunomia GiBleed loaded into acdmschema), SQLite and DuckDB: 65 tests, 0 failures. The one error, intest-dbplyr.R("sql_translation works with jdbc connections", from the locally installed bigrquery version), also happens ondevelop.R CMD check --no-manual --ignore-vignettes: Status: OK.NEWS.md is left for the maintainer.