Log4j MDC variable or thread local variable

Hi,

I want to share a few variable and values across layers - Presentation, Business, Persistence layer for HttpRequest - i.e single thread application where the app is deployed in tomcat

Is log4J MDC should be used or thread local variable would be the right choice?

Thanks.
Software ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
The MDC is automatically accessible from everywhere. But it is not accessible if you are using internal thread pools or Executors. For thread pools you will need to create your own thread pool, like it is explained here: https://stackoverflow.com/questions/6073019/how-to-use-mdc-with-thread-pools#19329668

With regards to the thread local, I think the MDC implementations do use ThreadLocal, so you could go with just MDC.

Just make sure that you clean the values in the MDC when the requests ends, otherwise you will end up with loads of unused data (which will not be eligible for gc).
0
Software ProgrammerAuthor Commented:
Assume we are not using MDC library in a project and want to have a thread local variable by ourself to propagate the values. Can u show me an example how a thread local variable can be initialized and used via code ??? One code snippet would be fine
0
girionisCommented:
You can do something like this:

Initialisation:
public static final ThreadLocal<String> USERNAME = new ThreadLocal<>();

Open in new window


Set a value when the user logs in:
USERNAME.set("ausername");

Open in new window


Access the value when you need it:
USERNAME.get();

Open in new window


Remove the value when the user logs out:
USERNAME.remove();

Open in new window

0
Software ProgrammerAuthor Commented:
How about following this link - https://stackoverflow.com/questions/24329474/accessing-session-variables-outside-servlet ???

The above code just talks about initializing for each class..

Let me know about the above link, so that will use the same if that is correct.
0
girionisCommented:
From the link you posted I would actually go with what the user said in the last comment:

In my own apps, I use first method in simple cases (few classes) and third method when I want to avoid repeating over and over the same parameters in many methods.

I lean towards the first method (pass it form the controller to wherever you need it) but if your project is very big and complicated I would also consider the third option.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.