[Turn]session persistence problem (restart server session still exists)

Source: Internet
Author: User

Ext.: http://xiaolongfeixiang.iteye.com/blog/560800

Most of the online demographics are implemented using the Sessionlistener listener.

Sessionlistener Trigger Source:

1. When Session is Create

2. When Session timeout

3. When explicitly invoking the Invalidate method of the session

4. When Tomcat is closed, the session destroy event is triggered when the Tomcat setting session is persisted to false

5. When the Tomcat setting session is persistent to true, the session destroy event is triggered when the session timeout is detected after Tomcat restarts (can be classified into the 2nd case)



how does the Tomcat session persist configuration?

By default, TOMCAT6 is providing this functionality. Please see $tomcat_home$/conf/context.xml. In about 24 lines, you'll see a note:

XML code
  1. <!--uncomment this to disable session persistence across Tomcat restarts-
  2. <!--
  3. <manager pathname= ""/>
  4. -
 

This means that the default Tomcat is supported for session persistence. Of course, canceling the comment also cancels the feature.

Session Persistence & Non-persistence


If Tomcat is configured as a non-persistent session, the session destroy event is automatically triggered when Tomcat shuts down.


If Tomcat is configured for session persistence, the session destroy event is not triggered when Tomcat shuts down. Instead, the contents of each session are written to a file. When Tomcat starts again, it reads the parameter and verifies that the session is timeout (but this verification seems a bit delayed, about a few seconds.) If a timeout is detected, Tomcat automatically triggers the session destroy event).


Failure Case Analysis:

In a recent analysis of an application written by someone else, the number of people online has been found to be negative. The reason is that the default configuration of Tomcat is not modified, so the session persistence feature is turned on. When Tomcat restarts, because the number of people online is a static constant in listener, the restart is zeroed. If the session destroy event is triggered after a reboot, there will be a negative online number.

However, what happens to the user after the reboot triggers the session destroy event?

First, the 1 premise is that the user's browser always exists at the interval of the Tomcat restart, and no other operations are performed. (very simply, if you do anything during the reboot, you will definitely see the "server is not responding" error message and leave naturally.) There will be no later. )

Then, in 2 cases:

Scenario 1: If Tomcat shuts down longer than the session time-out. Tomcat automatically triggers the session destroy event. As stated above, this "automatic" is delayed. If the user does anything, the session destroy event will be triggered immediately.

Scenario 2: If Tomcat's shutdown time is less than the session timeout, then the user clicks "Logout" and other similar logout functions, or the session timeout, will trigger the session destroy event.

The solution:


Scenario 1: Turn off the Tomcat Session persistence feature. Cancel the comment.

Scenario 2: Keep the online population in the database and access the database every time you change the number of people online. (Increased database traffic)

[Turn]session persistence problem (restart server session still exists)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.