Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session count tracking is lost when Tomcat restarted

 
sundaramoorthi thangavel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sundaramoorthi thangavel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Invalidate all sessions on server restart.
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod Iyer
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic