close ResultSets in JdbcUtils (#6206)

* close ResultSets in JdbcUtils

* Credit for #6206

* Apply spotless

---------

Co-authored-by: James Agnew <jamesagnew@gmail.com>
This commit is contained in:
Jonas Beyer 2024-08-28 12:11:22 +00:00 committed by GitHub
parent 5e48e38b1d
commit 049c28d3e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 142 additions and 124 deletions

View File

@ -0,0 +1,4 @@
---
type: fix
issue: 6206
title: "A resource leak during database migration on Oracle could cause a failure `ORA-01000 maximum open cursors for session`. This has been corrected. Thanks to Jonas Beyer for the contribution!"

View File

@ -85,19 +85,16 @@ public class JdbcUtils {
try { try {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
ResultSet indexes = getIndexInfo(theTableName, connection, metadata, false);
Set<String> indexNames = new HashSet<>(); Set<String> indexNames = new HashSet<>();
for (boolean unique : Set.of(false, true)) {
try (ResultSet indexes = getIndexInfo(theTableName, connection, metadata, unique)) {
while (indexes.next()) { while (indexes.next()) {
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0)); ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
String indexName = indexes.getString("INDEX_NAME"); String indexName = indexes.getString("INDEX_NAME");
indexNames.add(indexName); indexNames.add(indexName);
} }
}
indexes = getIndexInfo(theTableName, connection, metadata, true);
while (indexes.next()) {
ourLog.debug("*** Next index: {}", new ColumnMapRowMapper().mapRow(indexes, 0));
String indexName = indexes.getString("INDEX_NAME");
indexNames.add(indexName);
} }
indexNames = indexNames.stream() indexNames = indexNames.stream()
@ -124,7 +121,7 @@ public class JdbcUtils {
DatabaseMetaData metadata; DatabaseMetaData metadata;
try { try {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
ResultSet indexes = getIndexInfo(theTableName, connection, metadata, false); try (ResultSet indexes = getIndexInfo(theTableName, connection, metadata, false)) {
while (indexes.next()) { while (indexes.next()) {
String indexName = indexes.getString("INDEX_NAME"); String indexName = indexes.getString("INDEX_NAME");
@ -133,6 +130,7 @@ public class JdbcUtils {
return !nonUnique; return !nonUnique;
} }
} }
}
} catch (SQLException e) { } catch (SQLException e) {
throw new InternalErrorException(Msg.code(30) + e); throw new InternalErrorException(Msg.code(30) + e);
@ -171,8 +169,8 @@ public class JdbcUtils {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
String catalog = connection.getCatalog(); String catalog = connection.getCatalog();
String schema = connection.getSchema(); String schema = connection.getSchema();
ResultSet indexes = try (ResultSet indexes =
metadata.getColumns(catalog, schema, massageIdentifier(metadata, theTableName), null); metadata.getColumns(catalog, schema, massageIdentifier(metadata, theTableName), null)) {
while (indexes.next()) { while (indexes.next()) {
@ -210,14 +208,15 @@ public class JdbcUtils {
return new ColumnType(ColumnTypeEnum.BINARY, length); return new ColumnType(ColumnTypeEnum.BINARY, length);
case Types.VARBINARY: case Types.VARBINARY:
if (DriverTypeEnum.MSSQL_2012.equals(theConnectionProperties.getDriverType())) { if (DriverTypeEnum.MSSQL_2012.equals(theConnectionProperties.getDriverType())) {
// MS SQLServer seems to be mapping BLOB to VARBINARY under the covers, so we need // MS SQLServer seems to be mapping BLOB to VARBINARY under the covers,
// to reverse that mapping // so we need to reverse that mapping
return new ColumnType(ColumnTypeEnum.BLOB, length); return new ColumnType(ColumnTypeEnum.BLOB, length);
} else { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
Msg.code(33) + "Don't know how to handle datatype " + dataType Msg.code(33) + "Don't know how to handle datatype " + dataType
+ " for column " + theColumnName + " on table " + theTableName); + " for column " + theColumnName
+ " on table " + theTableName);
} }
case Types.CLOB: case Types.CLOB:
return new ColumnType(ColumnTypeEnum.CLOB, length); return new ColumnType(ColumnTypeEnum.CLOB, length);
@ -228,8 +227,11 @@ public class JdbcUtils {
case Types.TINYINT: case Types.TINYINT:
return new ColumnType(ColumnTypeEnum.TINYINT, length); return new ColumnType(ColumnTypeEnum.TINYINT, length);
default: default:
throw new IllegalArgumentException(Msg.code(34) + "Don't know how to handle datatype " throw new IllegalArgumentException(
+ dataType + " for column " + theColumnName + " on table " + theTableName); Msg.code(34) + "Don't know how to handle datatype " + dataType
+ " for column " + theColumnName
+ " on table " + theTableName);
}
} }
} }
@ -274,15 +276,15 @@ public class JdbcUtils {
Set<String> fkNames = new HashSet<>(); Set<String> fkNames = new HashSet<>();
for (String nextParentTable : parentTables) { for (String nextParentTable : parentTables) {
ResultSet indexes = metadata.getCrossReference( try (ResultSet indexes = metadata.getCrossReference(
catalog, schema, nextParentTable, catalog, schema, foreignTable); catalog, schema, nextParentTable, catalog, schema, foreignTable)) {
while (indexes.next()) { while (indexes.next()) {
String fkName = indexes.getString("FK_NAME"); String fkName = indexes.getString("FK_NAME");
fkName = fkName.toUpperCase(Locale.US); fkName = fkName.toUpperCase(Locale.US);
fkNames.add(fkName); fkNames.add(fkName);
} }
} }
}
return fkNames; return fkNames;
} catch (SQLException e) { } catch (SQLException e) {
@ -317,9 +319,8 @@ public class JdbcUtils {
Set<String> fkNames = new HashSet<>(); Set<String> fkNames = new HashSet<>();
for (String nextParentTable : parentTables) { for (String nextParentTable : parentTables) {
ResultSet indexes = metadata.getCrossReference( try (ResultSet indexes = metadata.getCrossReference(
catalog, schema, nextParentTable, catalog, schema, foreignTable); catalog, schema, nextParentTable, catalog, schema, foreignTable)) {
while (indexes.next()) { while (indexes.next()) {
if (theForeignKeyColumn.equals(indexes.getString("FKCOLUMN_NAME"))) { if (theForeignKeyColumn.equals(indexes.getString("FKCOLUMN_NAME"))) {
String fkName = indexes.getString("FK_NAME"); String fkName = indexes.getString("FK_NAME");
@ -328,6 +329,7 @@ public class JdbcUtils {
} }
} }
} }
}
return fkNames; return fkNames;
} catch (SQLException e) { } catch (SQLException e) {
@ -348,13 +350,14 @@ public class JdbcUtils {
DatabaseMetaData metadata; DatabaseMetaData metadata;
try { try {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
ResultSet indexes = metadata.getColumns( LinkedCaseInsensitiveMap<String> columnNames = new LinkedCaseInsensitiveMap<>();
try (ResultSet indexes = metadata.getColumns(
connection.getCatalog(), connection.getCatalog(),
connection.getSchema(), connection.getSchema(),
massageIdentifier(metadata, theTableName), massageIdentifier(metadata, theTableName),
null); null)) {
LinkedCaseInsensitiveMap<String> columnNames = new LinkedCaseInsensitiveMap<>();
while (indexes.next()) { while (indexes.next()) {
String tableName = indexes.getString("TABLE_NAME").toUpperCase(Locale.US); String tableName = indexes.getString("TABLE_NAME").toUpperCase(Locale.US);
if (!theTableName.equalsIgnoreCase(tableName)) { if (!theTableName.equalsIgnoreCase(tableName)) {
@ -365,6 +368,7 @@ public class JdbcUtils {
columnName = columnName.toUpperCase(Locale.US); columnName = columnName.toUpperCase(Locale.US);
columnNames.put(columnName, columnName); columnNames.put(columnName, columnName);
} }
}
return columnNames.keySet(); return columnNames.keySet();
} catch (SQLException e) { } catch (SQLException e) {
@ -391,6 +395,7 @@ public class JdbcUtils {
SequenceInformationExtractor sequenceInformationExtractor = SequenceInformationExtractor sequenceInformationExtractor =
dialect.getSequenceInformationExtractor(); dialect.getSequenceInformationExtractor();
ExtractionContext extractionContext = new ExtractionContext.EmptyExtractionContext() { ExtractionContext extractionContext = new ExtractionContext.EmptyExtractionContext() {
@Override @Override
public Connection getJdbcConnection() { public Connection getJdbcConnection() {
return connection; return connection;
@ -404,6 +409,7 @@ public class JdbcUtils {
@Override @Override
public JdbcEnvironment getJdbcEnvironment() { public JdbcEnvironment getJdbcEnvironment() {
return new JdbcEnvironment() { return new JdbcEnvironment() {
@Override @Override
public Dialect getDialect() { public Dialect getDialect() {
return dialect; return dialect;
@ -480,9 +486,11 @@ public class JdbcUtils {
DatabaseMetaData metadata; DatabaseMetaData metadata;
try { try {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
ResultSet tables = metadata.getTables(connection.getCatalog(), connection.getSchema(), null, null);
Set<String> columnNames = new HashSet<>(); Set<String> columnNames = new HashSet<>();
try (ResultSet tables =
metadata.getTables(connection.getCatalog(), connection.getSchema(), null, null)) {
while (tables.next()) { while (tables.next()) {
String tableName = tables.getString("TABLE_NAME"); String tableName = tables.getString("TABLE_NAME");
tableName = tableName.toUpperCase(Locale.US); tableName = tableName.toUpperCase(Locale.US);
@ -497,6 +505,7 @@ public class JdbcUtils {
columnNames.add(tableName); columnNames.add(tableName);
} }
}
return columnNames; return columnNames;
} catch (SQLException e) { } catch (SQLException e) {
@ -516,11 +525,11 @@ public class JdbcUtils {
DatabaseMetaData metadata; DatabaseMetaData metadata;
try { try {
metadata = connection.getMetaData(); metadata = connection.getMetaData();
ResultSet tables = metadata.getColumns( try (ResultSet tables = metadata.getColumns(
connection.getCatalog(), connection.getCatalog(),
connection.getSchema(), connection.getSchema(),
massageIdentifier(metadata, theTableName), massageIdentifier(metadata, theTableName),
null); null)) {
while (tables.next()) { while (tables.next()) {
String tableName = tables.getString("TABLE_NAME").toUpperCase(Locale.US); String tableName = tables.getString("TABLE_NAME").toUpperCase(Locale.US);
@ -539,6 +548,7 @@ public class JdbcUtils {
} }
} }
} }
}
throw new IllegalStateException(Msg.code(42) + "Did not find column " + theColumnName); throw new IllegalStateException(Msg.code(42) + "Did not find column " + theColumnName);
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -934,6 +934,10 @@
<name>Alex Cote</name> <name>Alex Cote</name>
<organization>athenahealth</organization> <organization>athenahealth</organization>
</developer> </developer>
<developer>
<id>plchldr</id>
<name>Jonas Beyer</name>
</developer>
</developers> </developers>
<licenses> <licenses>