logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ceki Gülcü <c...@qos.ch>
Subject Re: NDC , Servlets and thread safety
Date Wed, 06 Nov 2002 16:36:24 GMT
At 16:23 06.11.2002 +0000, you wrote:

>This may have been asked before but I cant find it in the archives. I am
>trying to use the log4j NDC class to log and distinguish between multiple
>client requests coming into a single servlet instance (we are using the
>frontcontroller pattern). We issue a NDC.push() at the start of the doPost
>method passing in the session ID and a NDC.pop() at the end of the doPost()
>As the servlet operates in the same thread and doPost() is not synchronized
>isn't it quite possible that two users could get into the same method and
>effectively push() two lots of information into the NDC so that the output
>from log4j contains session ids from BOTH of the clients.

That is not possible. The NDC is managed on a per thread basis. Different 
threads have different NDCs.

>Apologies if I'm being stupid but if this is a problem has anyone
>encountered this before and have an elegant solution?

There can't be a solution to a non-existent problem.

>By the way we don't really want to make the servlet implement the
>SingleThreadModel or synchronize the doGet, doPost methods as we believe
>this would kill the application.

You do not need to use SingleThreadModel or synchronize the doGet, doPost 

However, you should make sure to call NDC.remove from time to time. Also 
check out the MDC class.

BTW, please do not cross post to log4j-user *and* log4j-dev. The next time 
I won't be answering.

>Thanks in advance of any comments
>Graham Mead


TCP implementations will follow a general principle of robustness: be
conservative in what you do, be liberal in what you accept from
others. -- Jon Postel, RFC 793

To unsubscribe, e-mail:   <mailto:log4j-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:log4j-user-help@jakarta.apache.org>

View raw message