373952 Ensure MongoSessionManager un/binds session attributes on refresh only if necessary

This commit is contained in:
Jan Bartel 2014-02-19 14:59:31 +11:00
parent 8384b0febe
commit fc7e353394
1 changed files with 18 additions and 13 deletions

View File

@ -365,13 +365,16 @@ public class MongoSessionManager extends NoSqlSessionManager
// followed by bindings and then activation. // followed by bindings and then activation.
session.willPassivate(); session.willPassivate();
try try
{ {
session.clearAttributes(); DBObject attrs = (DBObject)getNestedValue(o,getContextKey());
//if disk version now has no attributes, get rid of them
DBObject attrs = (DBObject)getNestedValue(o,getContextKey()); if (attrs == null || attrs.keySet().size() == 0)
if (attrs != null)
{ {
session.clearAttributes();
}
else
{
//iterate over the names of the attributes on the disk version, updating the value
for (String name : attrs.keySet()) for (String name : attrs.keySet())
{ {
//skip special metadata field which is not one of the session attributes //skip special metadata field which is not one of the session attributes
@ -381,23 +384,25 @@ public class MongoSessionManager extends NoSqlSessionManager
String attr = decodeName(name); String attr = decodeName(name);
Object value = decodeValue(attrs.get(name)); Object value = decodeValue(attrs.get(name));
if (attrs.keySet().contains(name)) //session does not already contain this attribute, so bind it
{ if (session.getAttribute(attr) == null)
{
session.doPutOrRemove(attr,value); session.doPutOrRemove(attr,value);
session.bindValue(attr,value); session.bindValue(attr,value);
} }
else else //session already contains this attribute, update its value
{ {
session.doPutOrRemove(attr,value); session.doPutOrRemove(attr,value);
} }
} }
// cleanup, remove values from session, that don't exist in data anymore: // cleanup, remove values from session, that don't exist in data anymore:
for (String name : session.getNames()) for (String str : session.getNames())
{ {
if (!attrs.keySet().contains(name)) if (!attrs.keySet().contains(str))
{ {
session.doPutOrRemove(name,null); session.doPutOrRemove(str,null);
session.unbindValue(name,session.getAttribute(name)); session.unbindValue(str,session.getAttribute(str));
} }
} }
} }