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,23 +57,24 @@ 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");
while (resultSet.next()) {
String id = resultSet.getString(1);
UpdateStatement statement = new UpdateStatement(null, null, correctedTableName)
.addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret())
.setWhereClause("ID=?").addWhereParameters(id);
statements.add(statement);
if (!resultSet.isFirst()) {
sb.append(", ");
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);
UpdateStatement statement = new UpdateStatement(null, null, correctedTableName)
.addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret())
.setWhereClause("ID=?").addWhereParameters(id);
statements.add(statement);
if (!resultSet.isFirst()) {
sb.append(", ");
}
sb.append(id);
}
sb.append(id);
}
if (!statements.isEmpty()) {
confirmationMessage = sb.toString();
if (!statements.isEmpty()) {
confirmationMessage = sb.toString();
}
}
}

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;