KEYCLOAK-13167 - JDBC resource leak in custom migrations

This commit is contained in:
Dmitry Telegin 2020-02-29 00:25:07 +03:00 committed by Hynek Mlnařík
parent cec04fecc7
commit 08319db242
2 changed files with 19 additions and 19 deletions

View file

@ -34,6 +34,7 @@ import org.keycloak.models.utils.KeycloakModelUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
/**
@ -56,7 +57,7 @@ public class AddRealmCodeSecret implements CustomSqlChange {
String correctedSchemaName = database.escapeObjectName(database.getDefaultSchemaName(), Schema.class);
if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) {
ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, correctedSchemaName) + " WHERE CODE_SECRET IS NULL");
try (Statement st = connection.createStatement(); ResultSet resultSet = st.executeQuery("SELECT ID FROM " + LiquibaseJpaUpdaterProvider.getTable(correctedTableName, correctedSchemaName) + " WHERE CODE_SECRET IS NULL")) {
while (resultSet.next()) {
String id = resultSet.getString(1);
@ -75,6 +76,7 @@ public class AddRealmCodeSecret implements CustomSqlChange {
confirmationMessage = sb.toString();
}
}
}
return statements.toArray(new SqlStatement[statements.size()]);
} catch (Exception e) {

View file

@ -36,6 +36,7 @@ import org.keycloak.services.DefaultKeycloakSessionFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
@ -107,11 +108,8 @@ public abstract class CustomKeycloakTask implements CustomSqlChange {
try {
String correctedTableName = database.correctObjectName("REALM", Table.class);
if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) {
ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM " + getTableName(correctedTableName));
try {
try (Statement st = connection.createStatement(); ResultSet resultSet = st.executeQuery("SELECT ID FROM " + getTableName(correctedTableName))) {
return (resultSet.next());
} finally {
resultSet.close();
}
} else {
return false;