You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Roland Nygren <ro...@msn.com> on 2003/08/21 15:48:12 UTC

How to keep same thread during a session

I have an application consisting of JSPs, Servlets and Java classes running 
in Tomcat 4.1.
Normal HTTPSession isused to keep customer session.
Each new customer session is given a unique Id called CallId.
I.e. must this CallId be used in all logging.
I would like to not have to pass this CallId as a parameter in every method.
Ex.
Servlet or JSP code:
int CallId = session.getParameter("CallId");
fareFinder.search(from, to, departureDate, returnDate, numberOfPassengers, 
callId);

FareFinder code:
search(...) {
	log(msg, callId);
}

To solve this I first thought of using
session.getParameter("CallId");
in the Java code (log method) but that only means I have to pass the session 
object as a aprameter in every method call instead.

Next thought was that if each session keeps running on the same thread 
during the lifetime of the session I could keep a list in a singleton (or 
JNDI) with thread hashcodes and CallIds.
Ex. log method:
log (String msg) {
	int threadId = Thread.currentThread().hashCode();
	callId = callIdListSingleton.get(threadId);
	...
}

To test if the same thread is used during a session I tried to put the 
thread hashcode in the HTTPSession
test.jsp:
<html> <br>
<%
	out.print(session.getMaxInactiveInterval() / 60);
	out.print("<hr>");
	session.setAttribute("hash", new Integer(Thread.currentThread().hashCode()) 
);
	out.print(Thread.currentThread().hashCode());
	out.print("<hr>");
	out.print(session.getAttribute("hash"));
%>
</html>

Reloading this page in intervals of 10-20 seconds show that a session does 
NOT execute on tha same thread.
I tried to find a configuration that solves this but didn't.
Is there any way to keep the same thread during the session.
The application is not heavily used the number of threads will not be a 
problem.

_________________________________________________________________
Hitta rätt köpare på MSN Köp & Sälj http://www.msn.se/koposalj