RE: HttpSessionListener: Negative session count
Howdy, What would be the advantage of logging the sessionDestroyed event just to be safe. I'm currently solving problems with counting the number of active sessions and would like to know about the strategy of doing so. As the sentence says, it's just a safety measure ;) It's advantage is the same as the advantage of logging in general: if you have a problem or weird behavior with this component (the session counter), this logging may help debug the issue. ;) Yoav Shapira This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Thank you Ralph So I should use the following: public void sessionCreated(HttpSessionEvent event) { synchronized (this) { activeSessions++; } } public void sessionDestroyed(HttpSessionEvent event) { synchronized (this) { if (activeSessions 0) { activeSessions--; } } } Ralph Einfeldt wrote: You have to synchronize the -- and ++ operations. Otherwise you will have unpredictable results. You have to keep in mind that activeSessions++ is not atomic, so another thread can get between the computation of the value and the assignment. One scenario: Thread A: read activeSessions = 0 Thread B: read activeSessions = 0 Thread A: compute activeSessions + 1= 1 Thread B: compute activeSessions + 1= 1 Thread A: store the result in activeSessions = 1 Thread B: store the result in activeSessions = 1 Now activeSessions is 1 although 2 Sessions are active. If the sessions are not destroyed in a very close gap the result will get negative. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
Howdy, The simple check you had before (if session count 1 don't decrement) is not a bad idea. You might want to log the sessionDestroyed event just to be safe. In the 2.4 servlet spec, you will have other options (notifications) that apply to this scenario I think. Yoav Shapira Millennium ChemInformatics -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 10:14 AM To: Tomcat Users List Subject: Re: HttpSessionListener: Negative session count Hi Yoav That could be the problem as well. So if the server is restarted my session counter variable is set to 0, but there might still be 30 active sessions. I didn't think of that problem, because I thought that the sessions would be invalidated when the server is restarted. Has anyone a good solution to this problem? Or should I just prevent the session counter variable from becoming negative and after a while (session timeout for all remaining sessions) the counter will be working correctly again? Cheers, Christian Shapira, Yoav wrote: Howdy, I would mention one other possibility, unrelated to synchronization, which may cause this behavior. If your session listener simply increments a counter on every sessionCreated() and decrements the counter on every sessionDestroyed(), it is vulnerable to this behavior. When the server is restarted, if there are active sessions they are persisted by default. Then the server comes back up, and all these sessions are active, without a session created event, so your listener has a counter value of 0, but there are actually 0 active sessions. When these are destroyed as usual, sessionDestroyed() will be called for them, decrementing your counter and resulting in a negative value. Yoav Shapira Millennium ChemInformatics - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
I have a further question: Is it correct that I don't have to use jsp:useBean id=sessionCounter class=SessionCounter scope=session / and set the scope to session? Because now I don't use useBean. Should I? Thank you in advance, Christian Christian Hauser wrote: Hello J2EE programmers I wrote a JSP some time ago that uses a class SessionCounter (which implements HttpSessionListener) to count the active sessions of a web application and to display them. Unfortunately there seems to be a problem with my program, because the web application says that there are currently -18 active sessions. However, the lowest possible count should be 0. Of course I could implement the sessionDestroyed the following way (to prohibit a negative session count, but then the current session count would not be correct, not? public void sessionDestroyed(HttpSessionEvent event) { if (activeSessions 0) activeSessions--; } Code of JSP file: tr tdConcurrent sessions:/td td%= SessionCounter.getActiveSessions() %/td /tr tr tdLast refresh:/td td%= new Date() %/td /tr tr tdLast session change:/td td%= SessionCounter.getLastChange() %/td /tr Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of The web application. public class SessionCounter implements HttpSessionListener { /** Static variable to keep track of the current number of active sessions. */ private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } public static int getActiveSessions() { return activeSessions; } } I'm using Tomcat 4.1.24 and hope that someone of you might give me a hint on how I can get a reliable session counter. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
You have to synchronize the -- and ++ operations. Otherwise you will have unpredictable results. You have to keep in mind that activeSessions++ is not atomic, so another thread can get between the computation of the value and the assignment. One scenario: Thread A: read activeSessions = 0 Thread B: read activeSessions = 0 Thread A: compute activeSessions + 1= 1 Thread B: compute activeSessions + 1= 1 Thread A: store the result in activeSessions = 1 Thread B: store the result in activeSessions = 1 Now activeSessions is 1 although 2 Sessions are active. If the sessions are not destroyed in a very close gap the result will get negative. -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 10:21 AM To: [EMAIL PROTECTED] Subject: HttpSessionListener: Negative session count public class SessionCounter implements HttpSessionListener { /** Static variable to keep track of the current number of active sessions. */ private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
in this case synchronized public void sessionCreated( synchronized public void sessionDestroyed( should be OK. -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 10:44 AM To: Tomcat Users List Subject: Re: HttpSessionListener: Negative session count public void sessionCreated(HttpSessionEvent event) { synchronized (this) { activeSessions++; } } public void sessionDestroyed(HttpSessionEvent event) { synchronized (this) { if (activeSessions 0) { activeSessions--; } } } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
What would be the advantage of logging the sessionDestroyed event just to be safe. I'm currently solving problems with counting the number of active sessions and would like to know about the strategy of doing so. Thanks in advance, Kevin -Original Message- From: Shapira, Yoav [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 9:15 AM To: Tomcat Users List Subject: RE: HttpSessionListener: Negative session count Howdy, The simple check you had before (if session count 1 don't decrement) is not a bad idea. You might want to log the sessionDestroyed event just to be safe. In the 2.4 servlet spec, you will have other options (notifications) that apply to this scenario I think. Yoav Shapira Millennium ChemInformatics -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 10:14 AM To: Tomcat Users List Subject: Re: HttpSessionListener: Negative session count Hi Yoav That could be the problem as well. So if the server is restarted my session counter variable is set to 0, but there might still be 30 active sessions. I didn't think of that problem, because I thought that the sessions would be invalidated when the server is restarted. Has anyone a good solution to this problem? Or should I just prevent the session counter variable from becoming negative and after a while (session timeout for all remaining sessions) the counter will be working correctly again? Cheers, Christian Shapira, Yoav wrote: Howdy, I would mention one other possibility, unrelated to synchronization, which may cause this behavior. If your session listener simply increments a counter on every sessionCreated() and decrements the counter on every sessionDestroyed(), it is vulnerable to this behavior. When the server is restarted, if there are active sessions they are persisted by default. Then the server comes back up, and all these sessions are active, without a session created event, so your listener has a counter value of 0, but there are actually 0 active sessions. When these are destroyed as usual, sessionDestroyed() will be called for them, decrementing your counter and resulting in a negative value. Yoav Shapira Millennium ChemInformatics - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Hi Yoav That could be the problem as well. So if the server is restarted my session counter variable is set to 0, but there might still be 30 active sessions. I didn't think of that problem, because I thought that the sessions would be invalidated when the server is restarted. Has anyone a good solution to this problem? Or should I just prevent the session counter variable from becoming negative and after a while (session timeout for all remaining sessions) the counter will be working correctly again? Cheers, Christian Shapira, Yoav wrote: Howdy, I would mention one other possibility, unrelated to synchronization, which may cause this behavior. If your session listener simply increments a counter on every sessionCreated() and decrements the counter on every sessionDestroyed(), it is vulnerable to this behavior. When the server is restarted, if there are active sessions they are persisted by default. Then the server comes back up, and all these sessions are active, without a session created event, so your listener has a counter value of 0, but there are actually 0 active sessions. When these are destroyed as usual, sessionDestroyed() will be called for them, decrementing your counter and resulting in a negative value. Yoav Shapira Millennium ChemInformatics - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
You don't need the useBean. A bean with session scope doesn't make sense at all, if you want to access the methods through an object you can make the object application global. Or you can make sessionCreated/sessionDestroyed static and just call them through the class. -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 10:47 AM To: Tomcat Users List Subject: Re: HttpSessionListener: Negative session count Is it correct that I don't have to use jsp:useBean id=sessionCounter class=SessionCounter scope=session / and set the scope to session? Because now I don't use useBean. Should I? private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } public static int getActiveSessions() { return activeSessions; } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: HttpSessionListener: Negative session count
Thank you very much, Ralph. Regards, Christian Ralph Einfeldt wrote: You don't need the useBean. A bean with session scope doesn't make sense at all, if you want to access the methods through an object you can make the object application global. Or you can make sessionCreated/sessionDestroyed static and just call them through the class. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: HttpSessionListener: Negative session count
Howdy, I would mention one other possibility, unrelated to synchronization, which may cause this behavior. If your session listener simply increments a counter on every sessionCreated() and decrements the counter on every sessionDestroyed(), it is vulnerable to this behavior. When the server is restarted, if there are active sessions they are persisted by default. Then the server comes back up, and all these sessions are active, without a session created event, so your listener has a counter value of 0, but there are actually 0 active sessions. When these are destroyed as usual, sessionDestroyed() will be called for them, decrementing your counter and resulting in a negative value. Yoav Shapira Millennium ChemInformatics -Original Message- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 12, 2003 5:02 AM To: Tomcat Users List Subject: Re: HttpSessionListener: Negative session count Thank you very much, Ralph. Regards, Christian Ralph Einfeldt wrote: You don't need the useBean. A bean with session scope doesn't make sense at all, if you want to access the methods through an object you can make the object application global. Or you can make sessionCreated/sessionDestroyed static and just call them through the class. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
HttpSessionListener: Negative session count
Hello J2EE programmers I wrote a JSP some time ago that uses a class SessionCounter (which implements HttpSessionListener) to count the active sessions of a web application and to display them. Unfortunately there seems to be a problem with my program, because the web application says that there are currently -18 active sessions. However, the lowest possible count should be 0. Of course I could implement the sessionDestroyed the following way (to prohibit a negative session count, but then the current session count would not be correct, not? public void sessionDestroyed(HttpSessionEvent event) { if (activeSessions 0) activeSessions--; } Code of JSP file: tr tdConcurrent sessions:/td td%= SessionCounter.getActiveSessions() %/td /tr tr tdLast refresh:/td td%= new Date() %/td /tr tr tdLast session change:/td td%= SessionCounter.getLastChange() %/td /tr Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of The web application. public class SessionCounter implements HttpSessionListener { /** Static variable to keep track of the current number of active sessions. */ private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent event) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; } public static int getActiveSessions() { return activeSessions; } } I'm using Tomcat 4.1.24 and hope that someone of you might give me a hint on how I can get a reliable session counter. Christian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]