Updated Branches: refs/heads/master dfc566742 -> d662e37ed
WICKET-4574 ThreadContext does not properly remove the ThreadLocal from the current thread Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/d662e37e Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/d662e37e Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/d662e37e Branch: refs/heads/master Commit: d662e37edb1f5f90e4fab1182ce20595a140eea9 Parents: dfc5667 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Mon May 28 15:16:06 2012 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Mon May 28 15:16:06 2012 +0300 ---------------------------------------------------------------------- .../main/java/org/apache/wicket/ThreadContext.java | 28 ++++++++++++--- 1 files changed, 23 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/d662e37e/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java b/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java index a01fd51..a4f3f7f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java +++ b/wicket-core/src/main/java/org/apache/wicket/ThreadContext.java @@ -31,7 +31,7 @@ public class ThreadContext private Session session; - private static ThreadLocal<ThreadContext> threadLocal = new ThreadLocal<ThreadContext>(); + private static final ThreadLocal<ThreadContext> threadLocal = new ThreadLocal<ThreadContext>(); /** * INTERNAL METHOD @@ -42,10 +42,21 @@ public class ThreadContext public static ThreadContext get(boolean createIfDoesNotExist) { ThreadContext context = threadLocal.get(); - if (createIfDoesNotExist && context == null) + if (context == null) { - context = new ThreadContext(); - threadLocal.set(context); + if (createIfDoesNotExist) + { + context = new ThreadContext(); + threadLocal.set(context); + } + else + { + /* + * There is no ThreadContext set, but the threadLocal.get() operation has registered + * registered the threadLocal in this Thread's ThreadLocal map. We must now remove it. + */ + threadLocal.remove(); + } } return context; } @@ -140,7 +151,14 @@ public class ThreadContext */ public static void restore(ThreadContext threadContext) { - threadLocal.set(threadContext); + if (threadContext == null) + { + threadLocal.remove(); + } + else + { + threadLocal.set(threadContext); + } } /**
