You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Graham Mead <gr...@uk.royalsun.com> on 2002/11/06 17:23:52 UTC

NDC , Servlets and thread safety

Hi,

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()
method.#

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.

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

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.

Thanks in advance of any comments

Graham Mead


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: NDC , Servlets and thread safety

Posted by Fergus Gallagher <Fe...@OrbisUK.com>.
This is not the case.   There is typically a single *instance* of your
servlet, but is called simultaneously by different threads in the container.

With SingleThreadModel, each instance (maybe only one) is called by only one
thread at any one time.

On Wed, Nov 06, 2002 at 04:23:52PM +0000, Graham Mead wrote:
> 
> 
> As the servlet operates in the same thread 
> 

-- 
Fergus Gallagher          Tel: +44 (20) 8742 1600
Orbis                     Fax: +44 (20) 8742 2649
414 Chiswick High Street  email: Fergus.Gallagher@orbisuk.com
London  W4 5TL            Web: http://www.orbisuk.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: NDC , Servlets and thread safety

Posted by Ceki Gülcü <ce...@qos.ch>.
At 16:23 06.11.2002 +0000, you wrote:

>Hi,
>
>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()
>method.#
>
>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 
methods.

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

--
Ceki

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>