开发者

Session ID from Thread / Stacktrace

Not so much a problem I am having, just thinking about things.

If i have a web 开发者_Python百科application, lets take a call into a servlet. If the servlet calls into a data manager which calls into a database layer. Lets assueme that one of these levels throws an exception. Is there a way of identifying the session id that is present at the top of the stack without bubbling up the expception all the way to the top and into the servlet layer?

Hopefully that makes sense.... :o)


Generally speaking, it would be better to let the exception bubble up to the layer where it can be handled - i.e. your servlet. And that's what I'd recommend.

You can always make a Filter that intercepts each request and puts the current session ID in a public static ThreadLocal<String> sessionId; somewhere. Then this will be accessible from anywhere from the same thread.

It is another story whether the sessionId would be useful. Perhaps you can use your user Id, or user IP, or other similar information.


Not possible, unless you write your own code to pass the session ID downstream. For all practical purposes, these are two different tiers that can exist in two different JVMs. Take a look at the context object design pattern, on how n-tiered systems achieve this.

By the way, it is a bad idea to pass the session ID for the purpose of logging. A better approach is to pass an execution context object that uniquely identifies a request (and hence, also be used to identify the user behind it).

When you use the context object pattern, you should also be aware of its pitfalls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜