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 Bill Sorensen <bi...@truewill.net> on 2002/07/31 23:06:00 UTC

NDC remove and servlets

I'm trying to use NDCs with servlets, and want to confirm that I'm doing so 
correctly.

Basically, I'm doing the following in doPost:

if (user logs in OK) {
   NDC.push(currentUser.getCompanyId());
   try {
     ...
   } finally {
     // log user out
     NDC.pop();
   }
NDC.remove();

Questions:

1. Will this work?

2. Is there a better way?

3. Is it OK to call NDC.remove() in the case where NDC.push() wasn't called 
(no context)?

4. Based on the code, it looked as though NDC.remove() would not remove the 
context for the current thread (due to lazy removal).  Unfortunately, I 
don't know of any way to call it when the current request thread has 
terminated in a servlet.  Will it properly remove previous thread contexts 
the way I'm calling it?

5. Is there a way to confirm that remove() is working?  I looked into the 
debug logging for Log4j, and defined log4j.debug for Tomcat.  This worked 
(I saw some debug output when the log properties were loaded), but I could 
never get any debug output from NDC.remove() (although there's debug 
logging in the NDC code).  I'd like to be sure I'm not losing resources 
every time someone logs in to the servlet.

Thanks much!

++++
Bill Sorensen, Programmer/Analyst, E-ASI Billing div. of ASItransact
bsorensen@asicentral.com
http://www.asitransact.com/EASI_Billing.html



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


Re: NDC remove and servlets

Posted by Ceki Gülcü <ce...@qos.ch>.
Answers below.

At 16:06 31.07.2002 -0500, you wrote:
>I'm trying to use NDCs with servlets, and want to confirm that I'm doing 
>so correctly.
>
>Basically, I'm doing the following in doPost:
>
>if (user logs in OK) {
>   NDC.push(currentUser.getCompanyId());
>   try {
>     ...
>   } finally {
>     // log user out
>     NDC.pop();
>   }
>NDC.remove();
>
>Questions:
>
>1. Will this work?

Yes. It will work.

>2. Is there a better way?


Since the servler container will pool-threads, you can choose to call
NDC.remove every N (e.g. 100) requests not every time.

The MDC is usually a better alternative.

>3. Is it OK to call NDC.remove() in the case where NDC.push() wasn't 
>called (no context)?

Yes.

>4. Based on the code, it looked as though NDC.remove() would not remove 
>the context for the current thread (due to lazy removal).  Unfortunately, 
>I don't know of any way to call it when the current request thread has 
>terminated in a servlet.  Will it properly remove previous thread contexts 
>the way I'm calling it?

I don't understand the question.

>5. Is there a way to confirm that remove() is working?  I looked into the 
>debug logging for Log4j, and defined log4j.debug for Tomcat.  This worked 
>(I saw some debug output when the log properties were loaded), but I could 
>never get any debug output from NDC.remove() (although there's debug 
>logging in the NDC code).  I'd like to be sure I'm not losing resources 
>every time someone logs in to the servlet.

If you call NDC.remove at each request, lazyRemove will not have any
work left to do. The NDC code is very well tested.

>Thanks much!
>
>++++
>Bill Sorensen, Programmer/Analyst, E-ASI Billing div. of ASItransact
>bsorensen@asicentral.com
>http://www.asitransact.com/EASI_Billing.html

--
Ceki


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