dcsimg
Help! setMaxInactiveInterval problem with Tomcat!!!
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   spoutnika_spoutnika
Posted On:   Monday, October 21, 2002 01:00 PM

Hi everybody I ll try to explain my setMaxInactiveInterval problem. I would to logg off a user. Then i use the HttpSessionBindingListener implementation: public class TimeOut implements HttpSessionBindingListener { public TimeOut () { } public void valueBound(HttpSessionBindingEvent event) { System.out.println("Connect"); System.out.println("Time start"+new java.util.Date()); } public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("Disconnect"); System.out.println("Time finish"+new java.util.Date());    More>>

Hi everybody


I ll try to explain my setMaxInactiveInterval problem.


I would to logg off a user. Then i use the HttpSessionBindingListener implementation:


public class TimeOut implements HttpSessionBindingListener {


public TimeOut () {
}


public void valueBound(HttpSessionBindingEvent event) {


System.out.println("Connect");


System.out.println("Time start"+new java.util.Date());


}


public void valueUnbound(HttpSessionBindingEvent event) {


System.out.println("Disconnect");


System.out.println("Time finish"+new java.util.Date());


}


}


In my jsp page, i call my constructor and set it in an HTTP attribute:


...


HttpSession mySession = request.getSession(true);


...


TimeOut timeOut = new TimeOut ();


mySession.setAttribute("timeOut ",timeOut );




If I put the timeout in the web.xml

1



or if I use mySession.setMaxInactiveInterval(60) in the jsppage, the log are (for example):


Starting service Tomcat-Standalone

Apache Tomcat/4.0.4

Starting service Tomcat-Apache

Apache Tomcat/4.0.4

Connect

Time startMon Oct 21 15:32:01 GMT-06:00 2002

Disconnect

Time finishMon Oct 21 15:33:04 GMT-06:00 2002

Connect

Time startMon Oct 21 15:33:30 GMT-06:00 2002

Disconnect

Time finishMon Oct 21 15:35:04 GMT-06:00 2002

Connect

Time startMon Oct 21 15:35:24 GMT-06:00 2002

Disconnect

Time finishMon Oct 21 15:37:04 GMT-06:00 2002

Connect

Time startMon Oct 21 15:41:29 GMT-06:00 2002

Disconnect

Time finishMon Oct 21 15:43:05 GMT-06:00 2002



You can see easily that the timeout is not 1 mn but already more than one minute!!!


Somebody can explain this problem or maybe this bug?


txxxxxxxxx:)


S.

   <<Less

Re: Help! setMaxInactiveInterval problem with Tomcat!!!

Posted By:   michael_dean  
Posted On:   Wednesday, October 23, 2002 06:32 PM

Excellent question! If you look closely at the output in your logs, it hints at the answer. Notice how every session timed out at exactly*** 4 seconds after the minute.



Although the setMaxInactiveInterval(int) method allows you to specify the timeout interval with a resolution of one second, the servlet specification does not guarantee any specific resolution with which sessions timeouts must occur. Therefore, to improve performance, some servlet containers (Tomcat included) chose to implement session timeouts by having a worker thread wake once per minute and check for expired sessions. In other words, in the case shown, a session is destroyed if the session's timeout period has elapsed by 4 seconds past the minute.



This is (IMHO) a valid performance-enhancing decision because, as you noted, the attribute in web.xml accepts a whole number of minutes for session timeouts. This implies a lifetime on the order of several minutes, meaning sessions need only timeout within +/-1 minute of an exact time. This significantly improves performance over creating a new timer for each session or even checking all sessions once per second--especially as the number of sessions grows. Similarly, Java makes no guarantees that a thread will awaken at any particular time--only that it will sleep for at least the requested interval. As the interval becomes smaller, the "at least" becomes less and less negligible, so the spec/containers generally choose the "no promises made, no promises broken" approach. :)



Note that there is no "magical" number of seconds after the minute at which any specific servlet container will check for expired sessions. Depending on when the servlet container is started, the check could just as easily be performed at 44 seconds after the minute, or 13 seconds after the minute, etc.



***In the case of the last timeout, the message was printed at 5 seconds after the minute. This can be attributed to the fact that nothing is instantaneous in a computer (method calls to the listener objects and output through System.out take time), and threads are not guaranteed to wake after exactly the requested sleep time. Depending upon what other threads are executing, the thread may not awaken until some time after its sleep period has expired.



Also, I'm sure you realize this (your test class is probably a modified version of an HttpSessionBindingListener that you already had), but for others reading this post, instead of using an HttpSessionBindingListener--which is notified when the object is bound to or unbound from an HttpSession--a more direct method of determining the lifetime of a session would be to implement an HttpSessionListener--which is notified when a session is created or destroyed.

About | Sitemap | Contact