Merge pull request #1757 from mposolda/master

KEYCLOAK-1983 Fix Oracle and Sybase
This commit is contained in:
Marek Posolda 2015-10-19 12:27:45 +02:00
commit 944c416b45
4 changed files with 62 additions and 45 deletions

View file

@ -31,7 +31,7 @@
<constraints nullable="false"/>
</column>
<column name="LAST_SESSION_REFRESH" type="INT"/>
<column name="OFFLINE" type="BOOLEAN" defaultValueBoolean="false">
<column name="OFFLINE_FLAG" type="VARCHAR(4)">
<constraints nullable="false"/>
</column>
<column name="DATA" type="CLOB"/>
@ -47,14 +47,14 @@
<column name="CLIENT_ID" type="VARCHAR(36)">
<constraints nullable="false"/>
</column>
<column name="OFFLINE" type="BOOLEAN" defaultValueBoolean="false">
<column name="OFFLINE_FLAG" type="VARCHAR(4)">
<constraints nullable="false"/>
</column>
<column name="TIMESTAMP" type="INT"/>
<column name="DATA" type="CLOB"/>
</createTable>
<addPrimaryKey columnNames="USER_SESSION_ID, OFFLINE" constraintName="CONSTRAINT_OFFLINE_US_SES_PK" tableName="OFFLINE_USER_SESSION"/>
<addPrimaryKey columnNames="CLIENT_SESSION_ID, OFFLINE" constraintName="CONSTRAINT_OFFLINE_CL_SES_PK" tableName="OFFLINE_CLIENT_SESSION"/>
<addPrimaryKey columnNames="USER_SESSION_ID, OFFLINE_FLAG" constraintName="CONSTRAINT_OFFLINE_US_SES_PK" tableName="OFFLINE_USER_SESSION"/>
<addPrimaryKey columnNames="CLIENT_SESSION_ID, OFFLINE_FLAG" constraintName="CONSTRAINT_OFFLINE_CL_SES_PK" tableName="OFFLINE_CLIENT_SESSION"/>
</changeSet>
</databaseChangeLog>

View file

@ -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<PersistentClientSessionEntity> 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<PersistentClientSessionEntity> getClientSessionsByUserSession(String userSessionId, boolean offline) {
String offlineStr = offlineToString(offline);
TypedQuery<PersistentClientSessionEntity> 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<UserSessionModel> loadUserSessions(int firstResult, int maxResults, boolean offline) {
String offlineStr = offlineToString(offline);
TypedQuery<PersistentUserSessionEntity> 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<PersistentClientSessionEntity> query2 = em.createNamedQuery("findClientSessionsByUserSessions", PersistentClientSessionEntity.class);
query2.setParameter("userSessionIds", userSessionIds);
query2.setParameter("offline", offline);
query2.setParameter("offline", offlineStr);
List<PersistentClientSessionEntity> 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";
}
}

View file

@ -17,14 +17,14 @@ import javax.persistence.Table;
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@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;
}
}

View file

@ -22,12 +22,12 @@ import org.keycloak.models.jpa.entities.UserEntity;
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@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;
}
}