Closing resultSets explicitly
@@ -74,10 +74,11 @@ public final class DatabaseMetaDataHelper {
static Set<String> fetchTables(final DatabaseMetaData meta, final String catalog,
final String schemaPattern, final String tableNamePattern) throws SQLException {
Set<String> tablesInSchema = new HashSet<>();
- ResultSet rs = meta.getTables(catalog, schemaPattern, tableNamePattern, new String[] { "TABLE" });
+ try (ResultSet rs = meta.getTables(catalog, schemaPattern, tableNamePattern, new String[] { "TABLE" });) {
while (rs.next()) {
tablesInSchema.add(rs.getString(3).toUpperCase(Locale.US));
}
+ }
return tablesInSchema;
}
@@ -110,14 +111,15 @@ public final class DatabaseMetaDataHelper {
@SuppressWarnings("PMD.RemoteInterfaceNamingConvention")
private static List<ColumnMetaData> getColumnMetaData(final DatabaseMetaData meta, String catalog, //NOPMD
String schema, String tableName, String columnName, int expectedSize) throws SQLException { //NOPMD
- ResultSet columns = meta.getColumns(catalog, schema, tableName, columnName);
+ try (ResultSet columns = meta.getColumns(catalog, schema, tableName, columnName);) {
List<ColumnMetaData> columnList = convert(columns);
if (columnList.isEmpty()) {
//Postgresql does lowercase instead, so let's try that if we don't have a match
String table = tableName.toLowerCase(Locale.US);
String column = columnName == null ? null : columnName.toLowerCase(Locale.US);
- columns = meta.getColumns(catalog, schema, table, column);
- columnList = convert(columns);
+ try (ResultSet columnMeta = meta.getColumns(catalog, schema, table, column)) {
+ columnList = convert(columnMeta);
+ }
}
if (columnList.size() < expectedSize) {
String msg = String.format("Invalid SQL, the number of columns (%s) should match the number of number of input parameters (%s)",
@@ -127,11 +129,13 @@ public final class DatabaseMetaDataHelper {
return columnList;
}
+ }
+
/* default */ static List<SqlParam> getOutputColumnInfo(final Connection connection,
final String sqlSelectStatement) throws SQLException {
List<SqlParam> paramList = new ArrayList<>();
- Statement stmt = connection.createStatement();
- ResultSet resultSet = stmt.executeQuery(sqlSelectStatement);
+ try (Statement stmt = connection.createStatement();
+ ResultSet resultSet = stmt.executeQuery(sqlSelectStatement);) {
ResultSetMetaData metaData = resultSet.getMetaData();
if (metaData.getColumnCount()>0){
for (int i=1; i<=metaData.getColumnCount(); i++) {
@@ -142,6 +146,7 @@ public final class DatabaseMetaDataHelper {
}
return paramList;
}
+ }
private static List<ColumnMetaData> convert(ResultSet resultSet) throws SQLException {
List<ColumnMetaData> list = new ArrayList<>();
