From 028cbe1ce2d57f4974bd1390adba038610785597 Mon Sep 17 00:00:00 2001 From: mposolda Date: Mon, 19 Oct 2015 11:38:08 +0200 Subject: [PATCH] KEYCLOAK-1983 Fix Oracle and Sybase --- .../META-INF/jpa-changelog-1.6.0.xml | 8 ++-- .../JpaUserSessionPersisterProvider.java | 39 +++++++++++++------ .../PersistentClientSessionEntity.java | 32 +++++++-------- .../session/PersistentUserSessionEntity.java | 28 ++++++------- 4 files changed, 62 insertions(+), 45 deletions(-) diff --git a/connections/jpa-liquibase/src/main/resources/META-INF/jpa-changelog-1.6.0.xml b/connections/jpa-liquibase/src/main/resources/META-INF/jpa-changelog-1.6.0.xml index 5b0a0da893..b50a4f465f 100644 --- a/connections/jpa-liquibase/src/main/resources/META-INF/jpa-changelog-1.6.0.xml +++ b/connections/jpa-liquibase/src/main/resources/META-INF/jpa-changelog-1.6.0.xml @@ -31,7 +31,7 @@ - + @@ -47,14 +47,14 @@ - + - - + + \ No newline at end of file diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProvider.java index bf19d96980..6fc2960b84 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/session/JpaUserSessionPersisterProvider.java @@ -43,7 +43,8 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv entity.setUserSessionId(model.getUserSessionId()); entity.setRealmId(adapter.getRealm().getId()); entity.setUserId(adapter.getUser().getId()); - entity.setOffline(offline); + String offlineStr = offlineToString(offline); + entity.setOffline(offlineStr); entity.setLastSessionRefresh(model.getLastSessionRefresh()); entity.setData(model.getData()); em.persist(entity); @@ -59,7 +60,8 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv entity.setClientSessionId(clientSession.getId()); entity.setClientId(clientSession.getClient().getId()); entity.setTimestamp(clientSession.getTimestamp()); - entity.setOffline(offline); + String offlineStr = offlineToString(offline); + entity.setOffline(offlineStr); entity.setUserSessionId(clientSession.getUserSession().getId()); entity.setData(model.getData()); em.persist(entity); @@ -77,7 +79,8 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv PersistentUserSessionModel model = adapter.getUpdatedModel(); - PersistentUserSessionEntity entity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(userSession.getId(), offline)); + String offlineStr = offlineToString(offline); + PersistentUserSessionEntity entity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(userSession.getId(), offlineStr)); if (entity == null) { throw new ModelException("UserSession with ID " + userSession.getId() + ", offline: " + offline + " not found"); } @@ -87,12 +90,14 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv @Override public void removeUserSession(String userSessionId, boolean offline) { + String offlineStr = offlineToString(offline); + em.createNamedQuery("deleteClientSessionsByUserSession") .setParameter("userSessionId", userSessionId) - .setParameter("offline", offline) + .setParameter("offline", offlineStr) .executeUpdate(); - PersistentUserSessionEntity sessionEntity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(userSessionId, offline)); + PersistentUserSessionEntity sessionEntity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(userSessionId, offlineStr)); if (sessionEntity != null) { em.remove(sessionEntity); em.flush(); @@ -101,14 +106,16 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv @Override public void removeClientSession(String clientSessionId, boolean offline) { - PersistentClientSessionEntity sessionEntity = em.find(PersistentClientSessionEntity.class, new PersistentClientSessionEntity.Key(clientSessionId, offline)); + String offlineStr = offlineToString(offline); + PersistentClientSessionEntity sessionEntity = em.find(PersistentClientSessionEntity.class, new PersistentClientSessionEntity.Key(clientSessionId, offlineStr)); if (sessionEntity != null) { em.remove(sessionEntity); // Remove userSession if it was last clientSession List clientSessions = getClientSessionsByUserSession(sessionEntity.getUserSessionId(), offline); if (clientSessions.size() == 0) { - PersistentUserSessionEntity userSessionEntity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(sessionEntity.getUserSessionId(), offline)); + offlineStr = offlineToString(offline); + PersistentUserSessionEntity userSessionEntity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(sessionEntity.getUserSessionId(), offlineStr)); if (userSessionEntity != null) { em.remove(userSessionEntity); } @@ -119,9 +126,11 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv } private List getClientSessionsByUserSession(String userSessionId, boolean offline) { + String offlineStr = offlineToString(offline); + TypedQuery query = em.createNamedQuery("findClientSessionsByUserSession", PersistentClientSessionEntity.class); query.setParameter("userSessionId", userSessionId); - query.setParameter("offline", offline); + query.setParameter("offline", offlineStr); return query.getResultList(); } @@ -159,8 +168,10 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv @Override public List loadUserSessions(int firstResult, int maxResults, boolean offline) { + String offlineStr = offlineToString(offline); + TypedQuery query = em.createNamedQuery("findUserSessions", PersistentUserSessionEntity.class); - query.setParameter("offline", offline); + query.setParameter("offline", offlineStr); if (firstResult != -1) { query.setFirstResult(firstResult); @@ -179,7 +190,7 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv TypedQuery query2 = em.createNamedQuery("findClientSessionsByUserSessions", PersistentClientSessionEntity.class); query2.setParameter("userSessionIds", userSessionIds); - query2.setParameter("offline", offline); + query2.setParameter("offline", offlineStr); List clientSessions = query2.getResultList(); // Assume both userSessions and clientSessions ordered by userSessionId @@ -234,8 +245,10 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv @Override public int getUserSessionsCount(boolean offline) { + String offlineStr = offlineToString(offline); + Query query = em.createNamedQuery("findUserSessionsCount"); - query.setParameter("offline", offline); + query.setParameter("offline", offlineStr); Number n = (Number) query.getSingleResult(); return n.intValue(); } @@ -244,4 +257,8 @@ public class JpaUserSessionPersisterProvider implements UserSessionPersisterProv public void close() { } + + private String offlineToString(boolean offline) { + return offline ? "1" : "0"; + } } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentClientSessionEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentClientSessionEntity.java index 51a523978b..cad8ce88cd 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentClientSessionEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentClientSessionEntity.java @@ -17,14 +17,14 @@ import javax.persistence.Table; * @author Marek Posolda */ @NamedQueries({ - @NamedQuery(name="deleteClientSessionsByRealm", query="delete from PersistentClientSessionEntity sess where sess.userSessionId IN (select u.userSessionId from PersistentUserSessionEntity u where u.realmId=:realmId)"), - @NamedQuery(name="deleteClientSessionsByClient", query="delete from PersistentClientSessionEntity sess where sess.clientId=:clientId"), - @NamedQuery(name="deleteClientSessionsByUser", query="delete from PersistentClientSessionEntity sess where sess.userSessionId IN (select u.userSessionId from PersistentUserSessionEntity u where u.userId=:userId)"), - @NamedQuery(name="deleteClientSessionsByUserSession", query="delete from PersistentClientSessionEntity sess where sess.userSessionId=:userSessionId and sess.offline=:offline"), + @NamedQuery(name="deleteClientSessionsByRealm", query="delete from PersistentClientSessionEntity sess where sess.userSessionId IN (select u.userSessionId from PersistentUserSessionEntity u where u.realmId = :realmId)"), + @NamedQuery(name="deleteClientSessionsByClient", query="delete from PersistentClientSessionEntity sess where sess.clientId = :clientId"), + @NamedQuery(name="deleteClientSessionsByUser", query="delete from PersistentClientSessionEntity sess where sess.userSessionId IN (select u.userSessionId from PersistentUserSessionEntity u where u.userId = :userId)"), + @NamedQuery(name="deleteClientSessionsByUserSession", query="delete from PersistentClientSessionEntity sess where sess.userSessionId = :userSessionId and sess.offline = :offline"), @NamedQuery(name="deleteDetachedClientSessions", query="delete from PersistentClientSessionEntity sess where sess.userSessionId NOT IN (select u.userSessionId from PersistentUserSessionEntity u)"), - @NamedQuery(name="findClientSessionsByUserSession", query="select sess from PersistentClientSessionEntity sess where sess.userSessionId=:userSessionId and sess.offline=:offline"), - @NamedQuery(name="findClientSessionsByUserSessions", query="select sess from PersistentClientSessionEntity sess where sess.offline=:offline and sess.userSessionId IN (:userSessionIds) order by sess.userSessionId"), - @NamedQuery(name="updateClientSessionsTimestamps", query="update PersistentClientSessionEntity c set timestamp=:timestamp"), + @NamedQuery(name="findClientSessionsByUserSession", query="select sess from PersistentClientSessionEntity sess where sess.userSessionId=:userSessionId and sess.offline = :offline"), + @NamedQuery(name="findClientSessionsByUserSessions", query="select sess from PersistentClientSessionEntity sess where sess.offline = :offline and sess.userSessionId IN (:userSessionIds) order by sess.userSessionId"), + @NamedQuery(name="updateClientSessionsTimestamps", query="update PersistentClientSessionEntity c set timestamp = :timestamp"), }) @Table(name="OFFLINE_CLIENT_SESSION") @Entity @@ -45,8 +45,8 @@ public class PersistentClientSessionEntity { protected int timestamp; @Id - @Column(name = "OFFLINE") - protected boolean offline; + @Column(name = "OFFLINE_FLAG") + protected String offline; @Column(name="DATA") protected String data; @@ -83,11 +83,11 @@ public class PersistentClientSessionEntity { this.timestamp = timestamp; } - public boolean isOffline() { + public String getOffline() { return offline; } - public void setOffline(boolean offline) { + public void setOffline(String offline) { this.offline = offline; } @@ -103,12 +103,12 @@ public class PersistentClientSessionEntity { protected String clientSessionId; - protected boolean offline; + protected String offline; public Key() { } - public Key(String clientSessionId, boolean offline) { + public Key(String clientSessionId, String offline) { this.clientSessionId = clientSessionId; this.offline = offline; } @@ -117,7 +117,7 @@ public class PersistentClientSessionEntity { return clientSessionId; } - public boolean isOffline() { + public String getOffline() { return offline; } @@ -129,7 +129,7 @@ public class PersistentClientSessionEntity { Key key = (Key) o; if (this.clientSessionId != null ? !this.clientSessionId.equals(key.clientSessionId) : key.clientSessionId != null) return false; - if (offline != key.offline) return false; + if (this.offline != null ? !this.offline.equals(key.offline) : key.offline != null) return false; return true; } @@ -137,7 +137,7 @@ public class PersistentClientSessionEntity { @Override public int hashCode() { int result = this.clientSessionId != null ? this.clientSessionId.hashCode() : 0; - result = 31 * result + (offline ? 1 : 0); + result = 31 * result + (this.offline != null ? this.offline.hashCode() : 0); return result; } } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentUserSessionEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentUserSessionEntity.java index f56284c899..abc581b65e 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentUserSessionEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/session/PersistentUserSessionEntity.java @@ -22,12 +22,12 @@ import org.keycloak.models.jpa.entities.UserEntity; * @author Marek Posolda */ @NamedQueries({ - @NamedQuery(name="deleteUserSessionsByRealm", query="delete from PersistentUserSessionEntity sess where sess.realmId=:realmId"), - @NamedQuery(name="deleteUserSessionsByUser", query="delete from PersistentUserSessionEntity sess where sess.userId=:userId"), + @NamedQuery(name="deleteUserSessionsByRealm", query="delete from PersistentUserSessionEntity sess where sess.realmId = :realmId"), + @NamedQuery(name="deleteUserSessionsByUser", query="delete from PersistentUserSessionEntity sess where sess.userId = :userId"), @NamedQuery(name="deleteDetachedUserSessions", query="delete from PersistentUserSessionEntity sess where sess.userSessionId NOT IN (select c.userSessionId from PersistentClientSessionEntity c)"), - @NamedQuery(name="findUserSessionsCount", query="select count(sess) from PersistentUserSessionEntity sess where sess.offline=:offline"), - @NamedQuery(name="findUserSessions", query="select sess from PersistentUserSessionEntity sess where sess.offline=:offline order by sess.userSessionId"), - @NamedQuery(name="updateUserSessionsTimestamps", query="update PersistentUserSessionEntity c set lastSessionRefresh=:lastSessionRefresh"), + @NamedQuery(name="findUserSessionsCount", query="select count(sess) from PersistentUserSessionEntity sess where sess.offline = :offline"), + @NamedQuery(name="findUserSessions", query="select sess from PersistentUserSessionEntity sess where sess.offline = :offline order by sess.userSessionId"), + @NamedQuery(name="updateUserSessionsTimestamps", query="update PersistentUserSessionEntity c set lastSessionRefresh = :lastSessionRefresh"), }) @Table(name="OFFLINE_USER_SESSION") @@ -49,8 +49,8 @@ public class PersistentUserSessionEntity { protected int lastSessionRefresh; @Id - @Column(name = "OFFLINE") - protected boolean offline; + @Column(name = "OFFLINE_FLAG") + protected String offline; @Column(name="DATA") protected String data; @@ -87,11 +87,11 @@ public class PersistentUserSessionEntity { this.lastSessionRefresh = lastSessionRefresh; } - public boolean isOffline() { + public String getOffline() { return offline; } - public void setOffline(boolean offline) { + public void setOffline(String offline) { this.offline = offline; } @@ -107,12 +107,12 @@ public class PersistentUserSessionEntity { protected String userSessionId; - protected boolean offline; + protected String offline; public Key() { } - public Key(String userSessionId, boolean offline) { + public Key(String userSessionId, String offline) { this.userSessionId = userSessionId; this.offline = offline; } @@ -121,7 +121,7 @@ public class PersistentUserSessionEntity { return userSessionId; } - public boolean isOffline() { + public String getOffline() { return offline; } @@ -133,7 +133,7 @@ public class PersistentUserSessionEntity { Key key = (Key) o; if (this.userSessionId != null ? !this.userSessionId.equals(key.userSessionId) : key.userSessionId != null) return false; - if (offline != key.offline) return false; + if (this.offline != null ? !this.offline.equals(key.offline) : key.offline != null) return false; return true; } @@ -141,7 +141,7 @@ public class PersistentUserSessionEntity { @Override public int hashCode() { int result = this.userSessionId != null ? this.userSessionId.hashCode() : 0; - result = 31 * result + (offline ? 1 : 0); + result = 31 * result + (this.offline != null ? this.offline.hashCode() : 0); return result; } }