{"record":{"id":"dbb749ac18d14bcd","repo":"pentaho/pentaho-kettle","slug":"error-disconnecting-from-database-tostring","errorCode":null,"errorMessage":"Error disconnecting from database '{toString}'","messagePattern":"Error disconnecting from database '(.+?)'","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"warning","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":1005,"sourceCode":"  /**\n   * Only for unique connections usage, typically you use disconnect() to disconnect() from the database.\n   *\n   * @throws KettleDatabaseException in case there is an error during connection close.\n   */\n  public synchronized void closeConnectionOnly() throws KettleDatabaseException {\n    try {\n      if ( connection != null ) {\n        connection.close();\n        if ( !databaseMeta.isUsingConnectionPool() ) {\n          connection = null;\n        }\n      }\n\n      if ( log.isDetailed() ) {\n        log.logDetailed( \"Connection to database closed!\" );\n      }\n    } catch ( SQLException e ) {\n      throw new KettleDatabaseException( \"Error disconnecting from database '\" + toString() + \"'\", e );\n    } finally {\n      closeDynamicClassLoader();\n    }\n  }\n\n  /**\n   * Cancel the open/running queries on the database connection\n   *\n   * @throws KettleDatabaseException\n   */\n  public void cancelQuery() throws KettleDatabaseException {\n    // Canceling statements only if we're not streaming results on MySQL with\n    // the v3 driver\n    //\n    if ( databaseMeta.isMySQLVariant()\n      && databaseMeta.isStreamingResults() && getDatabaseMetaData().getDriverMajorVersion() == 3 ) {\n      return;\n    }","sourceCodeStart":987,"sourceCodeEnd":1023,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L987-L1023","documentation":"KettleDatabaseException thrown by Database.disconnect() when the underlying Connection.close() call throws a SQLException. The dynamic class loader is still released in the finally block, but the caller is told the JDBC-level disconnect failed. Usually indicates the connection was already in a broken state or a driver-level problem during close.","triggerScenarios":"Calling Database.disconnect() while the JDBC Connection.close() throws SQLException — e.g. connection already terminated by the server, network dropped mid-close, or driver bug in close.","commonSituations":"Long-running transformations whose connection was killed by the DB (wait_timeout), network outage during shutdown, closing twice after a broken socket.","solutions":["Check DB/network health — the connection was likely already dead before disconnect","Verify the JDBC driver version; some drivers throw on close of stale connections","Ensure disconnect() is called only once per connection lifecycle; or tolerate/ignore this error during cleanup","Wrap disconnect in try-catch since it typically runs in cleanup paths and the resource is released anyway"],"exampleFix":"// before\ndatabase.disconnect();\n// after\ntry {\n  database.disconnect();\n} catch (KettleDatabaseException e) {\n  log.warn(\"Disconnect failed (connection likely already closed): \" + e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"Connection c = database.getConnection();\nif ( c == null || c.isClosed() ) {\n  log.warn( \"Connection already closed; disconnect unnecessary\" );\n}","typeGuard":"boolean isDisconnectSafe( Database db ) throws SQLException {\n  Connection c = db.getConnection();\n  return c != null && !c.isClosed();\n}","tryCatchPattern":"try {\n  database.disconnect();\n} catch ( KettleDatabaseException e ) {\n  log.warn( \"Disconnect failed; connection likely already closed: \" + e.getMessage() );\n}","preventionTips":["Call disconnect() exactly once per Database instance","Keep-Alive / validate connections on long-running jobs to avoid stale handles","Use try-finally so disconnect always runs, but tolerate its failure","Update JDBC drivers with known close() bugs"],"tags":["jdbc","database","disconnect","cleanup"],"backgroundTag":"database-query-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}