Skip to content

SNOW-1903844: Closing an already closed SnowflakeDatabaseMetaDataResultSet triggers logs in DriverManager.getLogWriter() #2057

Description

@fabiencelier

What is the current behavior?

This is the close() method of SnowflakeDatabaseMetaDataResultSet:

  public void close() throws SQLException {
    // no exception
    try {
      getStatement().close(); // should close both result set and statement.
    } catch (SQLException ex) {
      logger.debug("Failed to close", ex);
    }
  }

If the result set is already closed, getStatement() will create a SnowflakeSQLException catched in the catch blog.
But creating such SqlException will call the DriverManager.getLogWriter() and log the whole StackTrace if the log writer is set.

Closing an already closed SnowflakeDatabaseMetaDataResultSet can easily happen as calling next() on the last row will close it, so a code like this will call close() twice:

try (ResultSet columns = connection.getMetaData().getColumns( ... ) ) {
  while (columns.next()) { // Call close on the last row
     // Do something
  }
} // call close to close the ResulSet resource

This is not a big deal but it is really polluting my logs a lot for no reason when setting up a log writer in the DriverManager.

What is the desired behavior?

Do not create a SqlException if the ResultSet is already closed, just do nothing

How would this improve snowflake-jdbc?

It would make the driver easier to debug as we have less noise when enabling the DriverManager log writer.

I can make a PR to improve it if needed, I think you can simply do

  @Override
  public void close() throws SQLException {
    // no exception
    if (!isClosed()) { // Nothing to do if already closed
      try {
        getStatement().close(); // should close both result set and statement.
      } catch (SQLException ex) {
        logger.debug("Failed to close", ex);
      }
    }
  }

References, Other Background

this is a stack trace which I se for every metadata query I'm doing:

net.snowflake.client.jdbc.SnowflakeSQLException: Result set has been closed.
 	at net.snowflake.client.jdbc.SnowflakeBaseResultSet.raiseSQLExceptionIfResultSetIsClosed(SnowflakeBaseResultSet.java:135)
 	at net.snowflake.client.jdbc.SnowflakeBaseResultSet.getStatement(SnowflakeBaseResultSet.java:830)
 	at net.snowflake.client.jdbc.SnowflakeDatabaseMetaDataResultSet.close(SnowflakeDatabaseMetaDataResultSet.java:159)
        [... my code]

Metadata

Metadata

Labels

featurestatus-information_neededAdditional information is required from the reporterstatus-triage_doneInitial triage done, will be further handled by the driver team

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions