Cross posting from my findings here: Doesn't persist attributes in KC 20.0.2 · Issue #8 · ThoreKr/keycloak-last-login-event-listener · GitHub
I’m having problems in Keycloak 20+ persisting UserModel changes from an EventListener. Has anyone else experienced the same thing? I made a simple test (below), and wasn’t able to persist changes to the user without creating a new transaction. Are there cases in which some events aren’t processed until after the transaction is committed?
This doesn’t persist the attribute:
public void onEvent(final Event event) {
if (EventType.LOGIN.equals(event.getType())) {
UserModel u = session.users().getUserById(realm, event.getUserId());
u.setSingleAttribute("foo", "bar");
}
}
This works:
public void onEvent(final Event event) {
if (EventType.LOGIN.equals(event.getType())) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), s -> {
UserModel u = s.users().getUserById(realm, event.getUserId());
u.setSingleAttribute("foo", "bar");
});
}
}