I had a problem when exceptions appear in my production system, but I really do not have good information about who causes them. The username is stored as a variable in their tomcat session, access to which I have, for example, in my doPost or doGet , but if I do not pass this information as a parameter for each of my business objects, t have access to the session . For obvious reasons, I would like to bind the username to the registration message so that I have an idea about what is going on.
So my solution is to do something like this
public class ExceptionUtil { private ExceptionUtil() { }
Then in my posts / receives, I can do it
String username = request.getSession().getAttribute("username"); ExceptionUtil.set(username);
Then, with my exceptions, I could do it (a far-fetched, bad practice example)
catch(SQLException e) { logger.error(ExceptionUtil.get() + " did something dumb in sql", e); throw e; }
The only problem I'm worried about is how Tomcat will manage my threads. What if they save threads? Will they persist? Will ThreadLocal values โโbe saved? If I saved the entire session in ThreadLocal instead of just String, this would be a serious potential memory leak. It also means that if someone forgot to reinstall (or forget to clear when this is done) the username / session in the stream, which is stored for several requests, there may be outdated data.
Call me cynical, but I do not want to rely on programmers (even, especially on myself!), Not forgetting to do something for the correctness of the program. If I can idiotize my code, I would like to. And that means a better understanding of how Tomcat will use threads.
So the question is in the form of a single sentence:
If I use ThreadLocal in a webapp running on Tomcat (7.0.27), do I run the risk of using Thread for several requests and with it the data from the previous request is saved?
I should note that even if they do not answer the exact question "Tomcat / ThreadLocal shenanigans", I am open to alternative solutions that allow me to elegantly access session variables for logging purposes. I am also open to comments about potential errors of my decision. I have a business problem to solve, and I am not married to any one solution. I just want to know who continues to throw exceptions from my prod system :)