For an enterprise-level application, the management of Session objects is very important. The information of the Sessio object is generally placed in the memory of the server. When the server restarts due to a failure or the application is reloaded, all the session information at this time will be lost. In order to avoid such a situation, in some occasions, the session data of the server can be stored in the file system or database, and such an operation is called persistence of the Session object. When the Session object is persisted, the objects stored in it are stored in a serialized form, which is why the data generally stored in the Session needs to implement the serializable interface (java.io.Serializable).
When a Session starts, the servlet container creates an HttpSession object for the Session. The servlet container in some cases transfers these HttpSession objects from memory to the file system or database, and loads them into memory when needed to access HttpSession information.
Session persistence is managed by the Session Manager. Tomcat provides two implementation classes :
org.apache.catalina.session.StandardManager
org.apache.catalina.session.PersistentManager
The following two implementations of StandardManager and PersistentManager are introduced.
1.StandardManager
StandardManager is the default Session Manager. Its implementation mechanism is: when the Tomcat server is shut down or restarted, or the Web application is reloaded, it will persist the HttpSession objects in memory and save them to the file system. The default file is <CATALINA_HOME>/ work/Catalina/hostname/applicationname/SESSIONS.ser.
2.PersistentManager
PersistentManager can save Session objects to the Session Store. It provides more flexible Session management functions than StandardManager. It has the following functions:
Persist HttpSession objects in memory and save them to the Session Store.
It has the fault tolerance function, and can back up the session to the Session Store in time. When the Tomcat server is unexpectedly shut down and then restarted, the Session object can be restored from the Session Store.
You can flexibly control the number of Sessions in memory, and transfer some Sessions to the Session Store.
The interface that Tomcat implements the persistent Session Store is org.apache.Catalina.store. Currently, it provides two classes that implement this interface, namely org.apache.Catalina.FileStore and org.apache.Catalina.JDBCStore.
Most of the implementation and configuration of PersistentManager are in two forms
Form 1: The context.xml file in the conf directory needs to be configured in a local file
Add the following <Manager> node under the <Context> node:
1
2
3
4
5
6
7
8
9
|
<Manager className="org.apache.catalina.session.PersistentManager" >
debug=0
saveOnRestart="true"
maxActiveSession="-1"
minIdleSwap="-1"
maxIdleSwap="-1"
maxIdleBackup="-1"
<Store className="org.apache.catalina.session.FileStore" directory="../session" />
</Manager>
|
Form 2: To store in the database, you need to configure the store node
1
2
3
4
5
|
<Store calssName="org.apache.catalina.JDBCStore" driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/session?usename=xxx&password=xxx"
sessionTable="session" sessionIdCol="session_id" sessionDataCol="session_data"
sessionValidCol="sessionValid" sessionMaxInactiveCol="maxInactive"
sessionLastAccessedCol="lastAccess" sessionAppCol="appName" checkInterval="60" debug="99" />
|