aspose file tools*
The moose likes Servlets and the fly likes Session count tracking is lost when Tomcat restarted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session count tracking is lost when Tomcat restarted" Watch "Session count tracking is lost when Tomcat restarted" New topic
Author

Session count tracking is lost when Tomcat restarted

sundaramoorthi thangavel
Greenhorn

Joined: Jul 18, 2006
Posts: 6
Hi,
We are doing a web application. We are providing Licence for that application( including No. of concurrent users). To track the user sessions, I created a class SessionCounter with a static field for Active sessions. When a session is created, I'm checking the Active session counter against the Maximum users (taken from License file). If the Maximum count is reached, I'm redirecting to a 'Invalid Session' page. We are using only servlets.
Now what the problem is, whenever the Tomcat is restarted, the Active session count which is a static field, is initialized to 0. For example, the Maximum allowed user is 3. Now 3 users are accessing. If the Tomcat is restarted, further 3 users can access the application. Again if restarted 9 users can access the application.

Anybody has any other solution for this problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Naturally if you are storing the counetr in memory, it will get reset when the classes are reloaded.

If you need the count to persist across reloads, you'll need to persist it to a database or the file system.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sundaramoorthi thangavel
Greenhorn

Joined: Jul 18, 2006
Posts: 6
Hi Bear Bibeault,
Thanks for your reply. I considered that option also. But it is also having some problem. Consider the scenario below.

* We gave license for 2 users
* When the first session is created, we are incrementing the count by 1 in database
* When the second user accesses, we are again incrementing the count by 1.
* If the 3rd user tries to access, we are redirecting to the error page. No Problem.( because it reached the Maximum users allowed)

* Suppose network is down for a minute. At that time those two users close the application. When the net is up, again they are tring to access the application. In this scenario, no users will be allowed to access the application. Because the count in database is 2(Maximum users). We have to manually reset the counter in database otherwise it won't work.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Invalidate all sessions on server restart.
Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
When the two users log-out, you have to reset the counters in the database to zero. These counters have to be updated everytime a user logs in and logs out.


RAGHU<br /> <br />"When the going gets tough, the tough get going"
Vinod Iyer
Ranch Hand

Joined: Apr 13, 2006
Posts: 53
If the server ( tomcat ) is restarted how can the previous three access the system ??? They wont be holding valid sessions right , since the server would have destroyed all session objects while restarting....

So the method you choose is correct , but instead of storing the field in some static attribute store it in ApplicationContext . When any one logs in , increase the cound in application context and put some identifier in clients current session object . Now if network is down , first see that if the session client wants is still alive if yes no problem , if no session is alive , check in application context if the count is lesss than N , if yes create a new session and continue. !! Write a session listener that will reduce the cound in application context variable
sundaramoorthi thangavel
Greenhorn

Joined: Jul 18, 2006
Posts: 6
Thank you all for giving me a good direction. Now I realize, we haven't implemented proper session tracking. I'm going to rewrite. Thank you all.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by sundaramoorthi thangavel:
....
* Suppose network is down for a minute. At that time those two users close the application. When the net is up, again they are tring to access the application. In this scenario, no users will be allowed to access the application. Because the count in database is 2(Maximum users). We have to manually reset the counter in database otherwise it won't work.


If the app is down, they're not going to be able to invalidate their sessions.
They can close the browser but that won't invalidate the sessions.
This will be the case whether even if the app is not down.

Servlet apps have no, reliable, way of knowing that a user has closed their browser (or lost network connectivity, etc...)

No matter where you store the current session count, your going to have the problem of people taking seats that they are no longer using; until the unused session times out on the server.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session count tracking is lost when Tomcat restarted