Skip to content

Commit

Permalink
ATS-14 do not use THREAD_PROPERTY, ThreadLocal does that job!
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Aug 23, 2019
1 parent 49a49a2 commit 7a6a0ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/appng/tomcat/session/Utils.java
Expand Up @@ -129,7 +129,7 @@ public void warn(Object message, Throwable t) {
}

public void warn(Object message) {
callRealMethod("trace", message);
callRealMethod("warn", message);
}

public void trace(Object message, Throwable t) {
Expand Down
39 changes: 8 additions & 31 deletions src/main/java/org/appng/tomcat/session/mongo/MongoStore.java
Expand Up @@ -76,9 +76,6 @@ public class MongoStore extends StoreBase {
/** Property used to store the Session's data. */
protected static final String sessionDataProperty = "data";

/** Property used to store the name of the thread that loaded the session at last */
private static final String THREAD_PROPERTY = "thread";

/** Default Name of the Collection where the Sessions will be stored. */
protected static final String sessionCollectionName = "tomcat.sessions";

Expand Down Expand Up @@ -276,10 +273,10 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio
StandardSession session = (StandardSession) currentSession.get();
if (null != session) {
if (session.getIdInternal().equals(id)) {
debug("Session from ThreadLocal: %s", id);
trace("Session from ThreadLocal: %s", id);
return session;
} else {
warn("Session from ThreadLocal differed! Requested: %s, found: %s", id, session.getIdInternal());
debug("Session from ThreadLocal differed! Requested: %s, found: %s", id, session.getIdInternal());
removeThreadLocalSession();
session = null;
}
Expand All @@ -290,27 +287,10 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio
BasicDBObject sessionQuery = sessionQuery(id);
DBObject mongoSession = this.collection.findOne(sessionQuery);
if (null == mongoSession) {
info("Session %s not found, returning null!", id);
return null;
}

long waited = 0;
while (waited < maxWaitTime && mongoSession.containsField(THREAD_PROPERTY)) {
debug("Session %s is still used by Thread %s", id, mongoSession.get(THREAD_PROPERTY));
try {
Thread.sleep(waitTime);
waited += waitTime;
} catch (InterruptedException e) {
// ignore
}
mongoSession = this.collection.findOne(sessionQuery);
}
if (waited >= maxWaitTime) {
info("waited more than %sms, proceeding!", maxWaitTime);
}
if (null != mongoSession.get(THREAD_PROPERTY)) {
debug("Session %s is still used by Thread %s", id, mongoSession.get(THREAD_PROPERTY));
}

Container container = manager.getContext();
Context context = (Context) container;
ClassLoader appContextLoader = context.getLoader().getClassLoader();
Expand All @@ -324,19 +304,16 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio
session.readObjectData(ois);
session.setManager(this.manager);

String threadName = Thread.currentThread().getName();
BasicDBObject setThread = new BasicDBObject("$set", new BasicDBObject(THREAD_PROPERTY, threadName));
this.collection.update(sessionQuery, setThread);

debug("Loaded session %s with query %s in %s ms (lastModified %s), owned by thread [%s]", id,
sessionQuery, System.currentTimeMillis() - start, new Date(session.getLastAccessedTime()),
threadName);
debug("Loaded session %s with query %s in %s ms (lastModified %s)", id, sessionQuery,
System.currentTimeMillis() - start, new Date(session.getLastAccessedTime()));

setThreadLocalSession(session);
} catch (ReflectiveOperationException roe) {
warn("Error loading session: %s", id);
getLog().error(String.format("Error loading session: %s", id), roe);
throw roe;
}
} else {
warn("No data for session: %s", id);
}
return session;
}
Expand Down

0 comments on commit 7a6a0ab

Please sign in to comment.