79 Set<String> clientSessionIdsToDelete =
new HashSet<>();
81 String tableName =
getTableName(
"OFFLINE_CLIENT_SESSION");
82 String colClientSessionId =
database.correctObjectName(
"CLIENT_SESSION_ID", Column.class);
84 try (PreparedStatement ps =
connection.prepareStatement(String.format(
85 "SELECT t.CLIENT_SESSION_ID, t.USER_SESSION_ID, t.CLIENT_ID, t.OFFLINE_FLAG" +
87 " (SELECT USER_SESSION_ID, CLIENT_ID, OFFLINE_FLAG" +
89 " GROUP BY USER_SESSION_ID, CLIENT_ID, OFFLINE_FLAG" +
90 " HAVING COUNT(*) > 1) t1" +
91 " WHERE t.USER_SESSION_ID = t1.USER_SESSION_ID" +
92 " AND t.CLIENT_ID = t1.CLIENT_ID" +
93 " AND t.OFFLINE_FLAG = t1.OFFLINE_FLAG" +
94 " ORDER BY t.USER_SESSION_ID, t.CLIENT_ID, t.OFFLINE_FLAG", tableName));
96 ResultSet resultSet = ps.executeQuery()
99 Key origKey =
new Key(null, null, null);
100 while (resultSet.next()) {
101 String clientSessionId = resultSet.getString(1);
102 Key key =
new Key(resultSet.getString(2), resultSet.getString(3), resultSet.getString(4));
104 if (key.equals(origKey)) {
105 clientSessionIdsToDelete.add(clientSessionId);
110 }
catch (Exception e) {
111 throw new CustomChangeException(
getTaskId() +
": Exception when updating data from previous version", e);
114 AtomicInteger ai =
new AtomicInteger();
115 clientSessionIdsToDelete.stream()
116 .collect(Collectors.groupingByConcurrent(id -> ai.getAndIncrement() / 20, Collectors.toList()))
119 .map(ids ->
new DeleteStatement(null, null,
"OFFLINE_CLIENT_SESSION")
120 .setWhere(
":name IN (" + ids.stream().map(
id ->
"?").collect(Collectors.joining(
",")) +
")")
121 .addWhereColumnName(colClientSessionId)
122 .addWhereParameters(ids.toArray())
124 .forEach(statements::add);
String getTableName(String tableName)
Definition: CustomKeycloakTask.java:132
Database database
Definition: CustomKeycloakTask.java:51
Connection connection
Definition: CustomKeycloakTask.java:53
String getTaskId()
Definition: RemoveDuplicateOfflineSessions.java:128