Question about MDC in servlet init() method

Hello, Lets assume I call MDC.put("applicationName","whatever") in the servlet init() method. Can I read this later in the service() method? I'm asking because some weeks ago I noticed that this did not work. I assumed that the threads running service() are not childrens of the thread that runs init(). However, I tried this again today, and this time it worked fine. I was not able to reproduce the problem. But before I do such things now in real web applications, I like to be sure if thats Ok. Can I set MDC variables in init() and read them in service() safely, or does this work only randomly or under special conditions?

Question was:
Can I set MDC variables in init() and read them in service() safely, or does this work only randomly or under special conditions?
I found the answer myself, because I managed to reproduce the problem :-) Obviously, MDC variables get lost when the application is idle for a while. So I am sure now that I have to set them again in the first lines of service() before I can read them. In service() I cannot safely use MDC variables that have been set in init(). Is that correct?

Hello Stephan, Values placed in the MDC are valid for the current thread only. When a servlet responds to an HTTP request, it may or may not be running under the same thread that invoked its init() method. You have several options. You can set MDC at the beginning of your servlet's service method. If you have multiple servlets, you can set the MDC from a servlet *filter* for a group of servlets before they get invoked. As for setting the applicationName for your web-application, I think this is such as general requirement that logback should support this feature out of the box. I am creating a bug report to remind myself. I hope this helps, Stefan Frings wrote:
Hello,
Lets assume I call MDC.put("applicationName","whatever") in the servlet init() method. Can I read this later in the service() method?
I'm asking because some weeks ago I noticed that this did not work. I assumed that the threads running service() are not childrens of the thread that runs init().
However, I tried this again today, and this time it worked fine. I was not able to reproduce the problem.
But before I do such things now in real web applications, I like to be sure if thats Ok.
Can I set MDC variables in init() and read them in service() safely, or does this work only randomly or under special conditions?
-- Ceki Gülcü QOS.ch is looking to hire talented developers in Switzerland. If interested, please contact c e k i @ q o s . c h

Thanks for your clarification. In my case the applicationName is nott related to lockback because I do not use logback. But I use the MDC class with my own logger. MDC can be very handy if man understand correctly how it works and where it does not. Message from Ceki Gulcu
Hello Stephan,
Values placed in the MDC are valid for the current thread only. When a servlet responds to an HTTP request, it may or may not be running under the same thread that invoked its init() method.
You have several options. You can set MDC at the beginning of your servlet's service method. If you have multiple servlets, you can set the MDC from a servlet *filter* for a group of servlets before they get invoked.
As for setting the applicationName for your web-application, I think this is such as general requirement that logback should support this feature out of the box. I am creating a bug report to remind myself.
I hope this helps,
Stefan Frings wrote:
Hello,
Lets assume I call MDC.put("applicationName","whatever") in the servlet init() method. Can I read this later in the service() method?
I'm asking because some weeks ago I noticed that this did not work. I assumed that the threads running service() are not childrens of the thread that runs init().
However, I tried this again today, and this time it worked fine. I was not able to reproduce the problem.
But before I do such things now in real web applications, I like to be sure if thats Ok.
Can I set MDC variables in init() and read them in service() safely, or does this work only randomly or under special conditions?
-- Ceki Gülcü QOS.ch is looking to hire talented developers in Switzerland. If interested, please contact c e k i @ q o s . c h
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
participants (2)
-
Ceki Gulcu
-
Stefan Frings