Skip to content

Commit 5ed5859

Browse files
committed
Merge branch 'master' of github.com:jcoleman/tomcat-redis-session-manager into tomcat-7
2 parents 5d74d95 + 4f8d7c3 commit 5ed5859

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.markdown

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@ Copy the tomcat-redis-session-manager.jar and jedis-2.0.0.jar files into the `li
3838

3939
Reboot the server, and sessions should now be stored in Redis.
4040

41+
Session Change Tracking
42+
-----------------------
43+
44+
As noted in the "Overview" section above, in order to prevent colliding writes, the Redis Session Manager only serializes the session object into Redis if the session object has changed (it always updates the expiration separately however.) This dirty tracking marks the session as needing serialization according to the following rules:
45+
46+
* Calling `session.removeAttribute(key)` always marks the session as dirty (needing serialization.)
47+
* Calling `session.setAttribute(key, newAttributeValue)` marks the session as dirty if any of the following are true:
48+
* `previousAttributeValue == null && newAttributeValue != null`
49+
* `previousAttributeValue != null && newAttributeValue == null`
50+
* `!newAttributeValue.getClass().isInstance(previousAttributeValue)`
51+
* `!newAttributeValue.equals(previousAttributeValue)`
52+
53+
This feature can have the unintended consequence of hiding writes if you implicitly change a key in the session or if the object's equality does not change even though the key is updated. For example, assuming the session already contains the key `"myArray"` with an Array instance as its corresponding value, and has been previously serialized, the following code would not cause the session to be serialized again:
54+
55+
List myArray = session.getAttribute("myArray");
56+
myArray.add(additionalArrayValue);
57+
58+
If your code makes these kind of changes, then the RedisSession provides a mechanism by which you can mark the session as dirty in order to guarantee serialization at the end of the request. For example:
59+
60+
List myArray = session.getAttribute("myArray");
61+
myArray.add(additionalArrayValue);
62+
session.setAttribute("__changed__");
63+
64+
In order to not cause issues with an application that may already use the key `"__changed__"`, this feature is disabled by default. To enable this feature, simple call the following code in your application's initialization:
65+
66+
RedisSession.setManualDirtyTrackingSupportEnabled(true);
67+
68+
This feature also allows the attribute key used to mark the session as dirty to be changed. For example, if you executed the following:
69+
70+
RedisSession.setManualDirtyTrackingAttributeKey("customDirtyFlag");
71+
72+
Then the example above would look like this:
73+
74+
List myArray = session.getAttribute("myArray");
75+
myArray.add(additionalArrayValue);
76+
session.setAttribute("customDirtyFlag");
77+
78+
4179
Possible Issues
4280
---------------
4381

src/main/java/com/radiadesign/catalina/session/RedisSession.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
public class RedisSession extends StandardSession {
1313
private static Logger log = Logger.getLogger("RedisSession");
1414

15+
protected static Boolean manualDirtyTrackingSupportEnabled = false;
16+
17+
public void setManualDirtyTrackingSupportEnabled(Boolean enabled) {
18+
manualDirtyTrackingSupportEnabled = enabled;
19+
}
20+
21+
protected static String manualDirtyTrackingAttributeKey = "__changed__";
22+
23+
public void setManualDirtyTrackingAttributeKey(String key) {
24+
manualDirtyTrackingAttributeKey = key;
25+
}
26+
27+
1528
protected HashMap<String, Object> changedAttributes;
1629
protected Boolean dirty;
1730

@@ -34,6 +47,11 @@ public void resetDirtyTracking() {
3447
}
3548

3649
public void setAttribute(String key, Object value) {
50+
if (manualDirtyTrackingSupportEnabled && manualDirtyTrackingAttributeKey.equals(key)) {
51+
dirty = true;
52+
return;
53+
}
54+
3755
Object oldValue = getAttribute(key);
3856
if ( value == null && oldValue != null
3957
|| oldValue == null && value != null

0 commit comments

Comments
 (0)