Skip to content

[collector]bugfix: close jdbc connection on statement creation failure#4184

Open
hutiefang76 wants to merge 1 commit into
apache:masterfrom
hutiefang76:codex/hertzbeat-4181-close-jdbc-connection
Open

[collector]bugfix: close jdbc connection on statement creation failure#4184
hutiefang76 wants to merge 1 commit into
apache:masterfrom
hutiefang76:codex/hertzbeat-4181-close-jdbc-connection

Conversation

@hutiefang76

Copy link
Copy Markdown

What changed

This closes the new JDBC connection when statement creation or statement setup fails in JdbcCommonCollect.

The existing code closed cached JDBC connections on failure, but a freshly opened connection could remain open if setReadOnly(true), createStatement(), setQueryTimeout(...), or setMaxRows(...) threw before the method returned the statement. That path can leave PostgreSQL PgConnection instances waiting for finalization.

Validation

  • Reproduced the leak with a regression test: before the fix, JdbcCommonCollectTest#testCloseConnectionWhenCreateStatementFails failed because connection.close() was not called.
  • JAVA_HOME=$(/usr/libexec/java_home -v 26) ./mvnw -s <central-only-settings> -pl hertzbeat-collector/hertzbeat-collector-basic -am -Dtest=JdbcCommonCollectTest#testCloseConnectionWhenCreateStatementFails -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false test
  • JAVA_HOME=$(/usr/libexec/java_home -v 26) ./mvnw -s <central-only-settings> -pl hertzbeat-collector/hertzbeat-collector-basic -am -Dtest=JdbcCommonCollectTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false test
  • JAVA_HOME=$(/usr/libexec/java_home -v 26) ./mvnw -s <central-only-settings> -pl hertzbeat-collector/hertzbeat-collector-basic -am -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false test
  • git diff --check

Fixes #4181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant