Bug fixes and added redis sentinel supportability changes.

pull/27/head
Ranjith Manickam 2018-12-02 23:43:37 +05:30
parent a95d5d9f5c
commit 92adf755ba
3 changed files with 13 additions and 23 deletions

View File

@ -14,7 +14,7 @@ Going forward, we no need to enable sticky session (JSESSIONID) in Load balancer
- Apache Tomcat 9
## Downloads:
- [latest version (2.0.5)](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/2.0.5)
- [latest version (3.0)](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0)
- [older versions](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki)
#### Pre-requisite:

View File

@ -73,10 +73,8 @@ public class Session extends StandardSession {
super.setAttribute(key, value);
if ((value != null || oldValue != null)
&& (value == null && oldValue != null || oldValue == null && value != null
|| !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) {
if (this.manager instanceof SessionManager && ((SessionManager) this.manager)
.getSaveOnChange()) {
&& (value == null && oldValue != null || oldValue == null && value != null || !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) {
if (this.manager instanceof SessionManager && ((SessionManager) this.manager).getSaveOnChange()) {
((SessionManager) this.manager).save(this, true);
} else {
this.changedAttributes.put(key, value);
@ -100,8 +98,7 @@ public class Session extends StandardSession {
@Override
public void removeAttribute(String name) {
super.removeAttribute(name);
if (this.manager instanceof SessionManager && ((SessionManager) this.manager)
.getSaveOnChange()) {
if (this.manager instanceof SessionManager && ((SessionManager) this.manager).getSaveOnChange()) {
((SessionManager) this.manager).save(this, true);
} else {
this.dirty = true;

View File

@ -121,8 +121,7 @@ public class SessionManager extends ManagerBase implements Lifecycle {
@Override
public Session createSession(String sessionId) {
if (sessionId != null) {
sessionId =
(this.dataCache.setnx(sessionId, SessionConstants.NULL_SESSION) == 0L) ? null : sessionId;
sessionId = (this.dataCache.setnx(sessionId, SessionConstants.NULL_SESSION) == 0L) ? null : sessionId;
} else {
do {
sessionId = generateSessionId();
@ -168,8 +167,7 @@ public class SessionManager extends ManagerBase implements Lifecycle {
@Override
public Session findSession(String sessionId) throws IOException {
Session session = null;
if (sessionId != null && this.sessionContext.get() != null && sessionId
.equals(this.sessionContext.get().getId())) {
if (sessionId != null && this.sessionContext.get() != null && sessionId.equals(this.sessionContext.get().getId())) {
session = this.sessionContext.get().getSession();
} else {
byte[] data = this.dataCache.get(sessionId);
@ -234,7 +232,6 @@ public class SessionManager extends ManagerBase implements Lifecycle {
private void initialize() {
try {
this.dataCache = new RedisCache();
this.serializer = new SerializationUtil();
Context context = getContextIns();
ClassLoader loader = (context != null && context.getLoader() != null) ? context.getLoader().getClassLoader() : null;
@ -254,24 +251,22 @@ public class SessionManager extends ManagerBase implements Lifecycle {
? this.sessionContext.get().getMetadata().getAttributesHash() : null;
byte[] currentHash = serializer.getSessionAttributesHashCode(newSession);
if (forceSave || newSession.isDirty()
|| (isPersisted =
(this.sessionContext.get() != null) ? this.sessionContext.get().isPersisted() : null)
== null
if (forceSave
|| newSession.isDirty()
|| (isPersisted = (this.sessionContext.get() != null) ? this.sessionContext.get().isPersisted() : null) == null
|| !isPersisted || !Arrays.equals(hash, currentHash)) {
SessionMetadata metadata = new SessionMetadata();
metadata.setAttributesHash(currentHash);
this.dataCache
.set(newSession.getId(), serializer.serializeSessionData(newSession, metadata));
this.dataCache.set(newSession.getId(), serializer.serializeSessionData(newSession, metadata));
newSession.resetDirtyTracking();
setValues(true, metadata);
}
int timeout = getSessionTimeout(newSession);
this.dataCache.expire(newSession.getId(), timeout);
LOGGER.trace("Session [" + newSession.getId() + "] expire in [" + timeout + "] seconds.");
LOGGER.debug("Session [" + newSession.getId() + "] expire in [" + timeout + "] seconds.");
} catch (IOException ex) {
LOGGER.error("Error occurred while saving the session object in data cache..", ex);
@ -289,15 +284,13 @@ public class SessionManager extends ManagerBase implements Lifecycle {
} else {
remove(session);
}
LOGGER.trace(
"Session object " + (session.isValid() ? "saved: " : "removed: ") + session.getId());
LOGGER.debug("Session object " + (session.isValid() ? "saved: " : "removed: ") + session.getId());
}
} catch (Exception ex) {
LOGGER.error("Error occurred while processing post request process..", ex);
} finally {
this.sessionContext.remove();
LOGGER.trace(
"Session removed from ThreadLocal:" + ((session != null) ? session.getIdInternal() : ""));
LOGGER.debug("Session removed from ThreadLocal:" + ((session != null) ? session.getIdInternal() : ""));
}
}