pentaho/pentaho-kettle · warning · KettleDatabaseException
Error closing connection while searching primary keys in…
Error message
Error closing connection while searching primary keys in table []
What it means
Thrown in the finally block of Database's primary-key lookup (Database.java:5153, getTableFields/getLookup of primary keys) when closing the ResultSet holding the key metadata fails with a SQLException. The primary keys may still have been read — the failure is in resource cleanup, and the message includes the table name.
Solutions
- Check the wrapped SQLException cause — if the keys were read successfully, treat this as a cleanup-only failure
- Validate/recycle connections with validation queries to avoid mid-query disconnects
- Increase socket/network timeouts for metadata-heavy queries against slow catalogs
- Avoid calling PK lookup on connections that just failed another operation
- Retry the primary-key lookup with a fresh connection if the result was lost
Example fix
// before
} catch ( SQLException e ) {
throw new KettleDatabaseException( "Error closing connection while searching primary keys in table ["
+ tablename + "]", e );
}
// after
} catch ( SQLException e ) {
log.logError( "Error closing primary-key result set for table [" + tablename + "]", e ); // keys already collected
}
return names.toArray( new String[ names.size() ] ); Defensive patterns
Strategy: try-catch
Validate before calling
if ( database.getConnection() == null || database.getConnection().isClosed() ) {
database.connect(); // refresh connection before metadata lookup
} Try / catch
try {
String[] keys = getPrimaryKeys( tablename );
} catch ( KettleDatabaseException e ) {
if ( names.size() > 0 ) {
// keys were read; cleanup failure only — proceed with names
} else {
throw e; // real failure, keys lost
}
} Prevention
- Use connection validation to recycle stale connections before metadata queries
- Set generous socket timeouts for catalog queries on slow DBs
- Prefer structured cleanup over throwing from finally blocks
- Retry metadata lookups on fresh connections after disconnects
When it happens
Trigger: Calling the primary-key retrieval API (e.g. getPrimaryKeyColumnNames / getTableFields path that iterates dbmd.getPrimaryKeys) and the allkeys ResultSet.close() in finally throws — connection dropped or driver error during metadata close.
Common situations: Firewall/timeout killing the connection between metadata fetch and close; flaky drivers throwing on closed result sets; calling PK lookup right after a connection timeout in long-running jobs.
Understand the failure class
Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.
Related errors
- Database.Exception.EmptyConnectionError
- Database.Exception.ErrorClosingCallableStatement
- Database.Exception.UnableToGetMetadata
- DatabaseMeta.Error.UnableRetrieveDbInfo
- Error closing prepared statement
AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13).
Data as JSON: /api/errors/99ea5e8c081b4753.
Report an issue: GitHub.
Appendix: source
Thrown at core/src/main/java/org/pentaho/di/core/database/Database.java:5153
while ( allkeys.next() ) {
String keyname = allkeys.getString( "PK_NAME" );
String columnName = allkeys.getString( "COLUMN_NAME" );
if ( !names.contains( columnName ) ) {
names.add( columnName );
}
if ( log.isRowLevel() ) {
log.logRowlevel( toString(), "getting key : " + keyname + " on column " + columnName );
}
}
} catch ( SQLException e ) {
log.logError( toString(), "Error getting primary keys columns from table [" + tablename + "]" );
} finally {
try {
if ( allkeys != null ) {
allkeys.close();
}
} catch ( SQLException e ) {
throw new KettleDatabaseException( "Error closing connection while searching primary keys in table ["
+ tablename + "]", e );
}
}
return names.toArray( new String[ names.size() ] );
}
/**
* Return all sequence names from connection
*
* @return The sequences name list.
* @throws KettleDatabaseException
*/
public String[] getSequences() throws KettleDatabaseException {
if ( databaseMeta.supportsSequences() ) {
String sql = databaseMeta.getSQLListOfSequences();
if ( sql != null ) {
List<Object[]> seqs = getRows( sql, 0 );
String[] str = new String[ seqs.size() ];View on GitHub (pinned to f3058517a1)