tags:

views:

34

answers:

2

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

If i have a web 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)

+3  A: 

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.

Bozho
...and you dont even need to use a checked exception. Subclass RuntimeException and catch that exception at the topmost level. That avoids carrying the _throws_ declaration around. This is somewhat dirty though..
f1sh
+1  A: 

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.

Vineet Reynolds