{"record":{"id":"c7cdf5a09aa348bb","repo":"pentaho/pentaho-kettle","slug":"unable-to-check-if-table-tablename-exists-on-connection","errorCode":null,"errorMessage":"Unable to check if table [\" + tablename + \"] exists on connection [\" + databaseMeta.getName() + \"]","messagePattern":"Unable to check if table \\[\" \\+ tablename \\+ \"\\] exists on connection \\[\" \\+ databaseMeta\\.getName\\(\\) \\+ \"\\]","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":2178,"sourceCode":"   * @return true if the table exists, false if it doesn't.\n   * @deprecated Deprecated in favor of {@link #checkTableExists(String, String)}\n   */\n  @Deprecated\n  public boolean checkTableExists( String tablename ) throws KettleDatabaseException {\n    try {\n      if ( log.isDebug() ) {\n        log.logDebug( \"Checking if table [\" + tablename + \"] exists!\" );\n      }\n      // Just try to read from the table.\n      String sql = databaseMeta.getSQLTableExists( tablename );\n      try {\n        getOneRow( sql );\n        return true;\n      } catch ( KettleDatabaseException e ) {\n        return false;\n      }\n    } catch ( Exception e ) {\n      throw new KettleDatabaseException(\n        \"Unable to check if table [\" + tablename + \"] exists on connection [\" + databaseMeta.getName() + \"]\", e );\n    }\n  }\n\n  /**\n   * See if the table specified exists.\n   *\n   * <p>This is a smarter implementation of {@link #checkTableExists(String)} where\n   * metadata is used first and we only use statements when absolutely necessary.\n   *\n   * <p>Contrary to previous versions of similar duplicated methods, this implementation\n   * does not require quoted identifiers.\n   *\n   * @param tablename The unquoted name of the table to check.<br> This is NOT the properly quoted name of the table or\n   *                  the complete schema-table name combination.\n   * @param schema    The unquoted name of the schema.\n   * @return true if the table exists, false if it doesn't.\n   */","sourceCodeStart":2160,"sourceCodeEnd":2196,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L2160-L2196","documentation":"Thrown by Database.tableExists(tablename) when the check itself fails with a generic Exception — i.e. the library could not determine whether the table exists. Note that a KettleDatabaseException from the internal probe (SELECT ... FROM table) is interpreted as 'table does not exist' and returns false instead.","triggerScenarios":"Database.tableExists(sql-tablename) when getDatabaseMetaData() is null, the connection is closed, or any non-probe exception occurs while running the internal 'SELECT 1 FROM tablename' / catalog query.","commonSituations":"Calling tableExists before connecting; case-sensitivity or schema-qualified names mishandled; database down or credentials expired so even the probe fails with a non-SQL exception.","solutions":["Verify the database connection is open before calling tableExists()","Inspect the cause for the underlying failure (connection state, permissions)","Use checkTableExists(...) (metadata-based variant) which uses DatabaseMetaData.getTables instead of a probe SELECT","Catch KettleDatabaseException around tableExists and treat as connectivity failure, not 'table missing'"],"exampleFix":"// before\nboolean exists = database.tableExists(\"MY_TABLE\"); // connection closed\n// after\ndatabase.connect();\nboolean exists = database.tableExists(\"MY_TABLE\");","handlingStrategy":"try-catch","validationCode":"if (!database.isOpened()) throw new IllegalStateException(\"Connect before tableExists check\");","typeGuard":null,"tryCatchPattern":"boolean exists;\ntry {\n  exists = database.tableExists(tablename);\n} catch (KettleDatabaseException e) {\n  // distinguish 'check failed' from 'table missing'\n  logError(\"Table existence check failed for \" + tablename, e);\n  throw e;\n}","preventionTips":["Connect before any existence check","Remember a KettleDatabaseException from the probe is returned as false — don't rely on tableExists for connectivity checks","Prefer checkTableExists for metadata-based checks","Use correct casing/schema-qualified names"],"tags":["database","table-exists","metadata"],"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"}