Tomcat-Persistent Session

Source: Internet
Author: User

The session is stored in memory, and if the server restarts and goes down, the session will be lost. Sometimes, we need to persist the Session to deal with unexpected situations. For example, the client and the server in the process of interaction, may be due to the loss of the Session caused by the loss of data. There is another situation where we need to persist the Session. If the current user has a large amount of access, a large number of sessions will take up a large amount of memory on the server, causing the performance of the server to be affected. If you can swap out the sessions that have been idle for a long time and store them on disk, you can save memory space.

One thing to note is that to persist the session, the object that is stored in the session must be serializable, which implements the Java.io.Serializable interface.

Tomcat uses two session management classes to achieve session persistence: Org.apache.catalina.session.StandardManager and Org.apache.catalina.session. You can configure how the Session is persisted by $CATALINA _home/conf/context.xml <Manager> nodes. Tomcat has started the persistent configuration by default, to disable the persistence feature, simply configure the <manager pathname= ""/> in the <Context> node.

Standardmanager

This is the default Session management class for Tomcat. Standardmanager does not use any store to store the session, and when Tomcat shuts down, restarts, or the WEB application reloads, Standardmanager serializes the in-memory session to the $CATALINA _ho The Me/work/catalina/hostname/webappname/sessions.ser file. When Tomcat restarts or the WEB application loads, Tomcat deserializes the session back into memory. It is important to note that if the server crashes or is terminated without a graceful shutdown, all sessions will be lost because Standardmanager does not have the opportunity to serialize persistent storage processing.

Persistentmanager

Persistentmanager copies the session in memory to a file or database by using the Store. If the number of Session objects currently active exceeds the upper limit or the session object is idle for too long, the session object will be swapped out and stored on disk to save memory space. When Tomcat gracefully shuts down, restarts, or the WEB application reloads, Persistentmanager will persist the session object to disk as well as Standardmanager. When the session object is copied to disk, the original session object may still remain in memory. Therefore, if the Web app suddenly terminates abnormally or the server crashes, the persisted session can be restored from disk when the server restarts and the Web app reloads.

There are two types of stores: Filestore and Jdbcstore, which are used to store the session to files and databases.

Filestore

Filestore is used to store the session to a file, specifying the directory in which the file resides through the <Store/> element's directory properties.

<?XML version= "1.0" encoding= "Utf-8"?><Context><ManagerClassName= "Org.apache.catalina.session.PersistentManager"Saveonrestart= "true"maxactivesession= "-1"Minidleswap= "0"Maxidleswap= "+"Maxidlebackup= "0">        <StoreClassName= "Org.apache.catalina.session.FileStore"Checkinterval= "$"Directory= "./session"/>    </Manager></Context>

In the above configuration, the session object will be stored in the $CATALINA _home/work/catalina/hostname/webappname/session/sessionid.session file.

Jdbcstore

The Jdbcstore is used to store the session to the database.

1. Configuration of Context.xml:

<?XML version= "1.0" encoding= "Utf-8"?><Context>    <ManagerClassName= "Org.apache.catalina.session.PersistentManager"maxactivesessions= "-1"Minidleswap= "-1"Maxidleswap= "-1"Maxidlebackup= "-1">        <StoreClassName= "Org.apache.catalina.session.JDBCStore"drivername= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://localhost:3306/tomcat?user=root&amp;password=root"sessiontable= "Tomcat_sessions"Sessionidcol= "session_id"Sessiondatacol= "Session_data"Sessionvalidcol= "Session_valid"Sessionmaxinactivecol= "Max_inactive"Sessionlastaccessedcol= "Last_access"Sessionappcol= "App"Checkinterval= "$"/>    </Manager>    </Context>

2. Database configuration, note the need to add a database corresponding driver package in the $CATALINA _home/lib directory:

DROP TABLE IF EXISTStomcat_sessions;CREATE TABLEtomcat_sessions (session_idVARCHAR( -) not NULL PRIMARY KEY, Session_data Mediumblob, Session_validVARCHAR( -) not NULL, Max_inactiveINT  not NULL, Last_accessBIGINT  not NULL, the appVARCHAR(255),    INDEX(APP));
<Manager> Part parameter Description

Maxactivesessions: The upper value of the number of sessions that can be active, the default value is-1, which means there is no limit.

Minidleswap:session the shortest time that can be idle, the Manager may persist the session to the Store for more than that time, and the session will not persist in memory. The unit is seconds, and the default value is-1, which means there is no limit.

Maxidleswap:session the maximum amount of time that can be idle, the Manager will persist the session to the Store, and the session will not persist in memory. The unit is seconds, and the default value is-1, which means there is no limit.

Maxidlebackup: Idle time, more than that time, the Manager will persist the session to the Store, but the session object remains in memory. The unit is seconds, and the default value is-1, which means there is no limit.

For more information, please refer to:

Http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html

Tomcat-Persistent Session

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.