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

View File

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