?
當(dāng)一個(gè) Session 開始時(shí), Servlet 容器會(huì)為 Session 創(chuàng)建一個(gè) HttpSession 對(duì)象。 Servlet 容器在某些情況下把這些 HttpSession 對(duì)象從內(nèi)存中轉(zhuǎn)移到文件系統(tǒng)或數(shù)據(jù)庫中,在需要訪問 HttpSession 信息時(shí)再把它們加載到內(nèi)存中。
Session 的持久化是由 Session Manager 來管理的。 Tomcat 提供了兩個(gè)實(shí)現(xiàn)類
l???????? org.apache.catalina.session.StandardManager
l???????? org.apache.catalina.session.PersistentManager
1.???????? StandardManager
Standard Manager 是默認(rèn)的 Session Manager. 它的實(shí)現(xiàn)機(jī)制為:當(dāng) Tomcat 服務(wù)器關(guān)閉或重啟,或者 web 應(yīng)用被重新加載時(shí),會(huì)對(duì)在內(nèi)存中的 HttpSession 對(duì)象進(jìn)行持久化,把它們保存到文件系統(tǒng)中,默認(rèn)的文件為:
<CATALINA_HOME>/work/Catalina/hostname/applicationname/SESSIONS.ser
2.???????? PersistentManager
PersistentManager 能夠把 Session 對(duì)象保存到 Session Stor 中,它提供了比 StandardManager 更為靈活的 Session 管理功能,它具有以下功能:
l???????? 對(duì)內(nèi)存中的 HttpSession 對(duì)象進(jìn)行持久化,把它們保存到 Session Store 中
l???????? 具有容錯(cuò)功能,及時(shí)把 Session 備份到 Session Store 中,當(dāng) Tomcat 服務(wù)器意外關(guān)閉后再重啟時(shí),可以從 Session Store 中恢復(fù) Session 對(duì)象。
l???????? 可以靈活控制在內(nèi)存中的 Session 數(shù)目,將部分 Session 轉(zhuǎn)移到 Session Store 中
Tomcat 實(shí)現(xiàn)持久化 Session Store 的接口為 org.apache.Catalina.store, 目前提供了兩個(gè)實(shí)現(xiàn)這一接口的類: org.apache.Catalina.FileStore 和 org.apache.Catalina.JDBCStore.
下面討論如何配置 PersistentManager 以及兩種持久化 Session Store.
配置 FileStore
FileStore 將 HttpSession 對(duì)象保存在一個(gè)文件中。這個(gè)文件的默認(rèn)目錄為
<CATALINA_HOME>/work/Catalina/hostname/applicationname. 每個(gè) HttpSession 對(duì)象都會(huì)對(duì)應(yīng)一個(gè)文件,它以 Session 的 ID 作為文件名,擴(kuò)展名為: .session.
例:為 helloapp 應(yīng)用配置 FileStore
Server.xml
<!-- configure FileStore -->
<Context path="/helloapp" docBase="helloapp" debug="0"
reloadable="true">
<Manager className="org.apache.catalina.session.PersistentManager" >
debug=0;
saveOnRestart="true"
maxActiveSessions="-1"
minIdleSwap="-1"
maxIdleSwap="-1"
maxIdleBackup="-1"
<Store className="org.apache.catalina.session.FileStore" directory="mydir" />
</Manager>
</Context>
配置 JDBCStore
JDBCStore 將 HttpSession 對(duì)象保存在數(shù)據(jù)為庫的一張表中。
例: MySQL 中創(chuàng)建 Session 表的步驟,假定表的名字為 tomcat_sessions, 這張表所在的數(shù)據(jù)庫為 tomcatsessionDB.
CREATE DATABASE tomcatsessionDB;
Use tomcatsessionDB
Create table tomcat_session(
session_id? varchar(100) not null primary key,
valid_session chart(1)not null,
max_inactive int not null,
last_access bigint not null,
app_name varchar (255),
session_data mediumblob,
KEY kapp_name(app_name)
};
Server.xml
<!-- configure JDBCStore -->
<Context path="/helloapp" docBase="helloapp" debug="0"
reloadable="true">
<Manager className="org.apache.catalina.session.PersistentManager" >
debug=99;
saveOnRestart="true"
maxActiveSessions="-1"
minIdleSwap="-1"
maxIdleSwap="-1"
maxIdleBackup="-1"
<Store className="org.apache.catalina.session.JDBCStore"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/tomcatsessionDB?user=dbuser;password=1234"
sessionTable="tomcat_sessions"
sessionIdCol="session_id"
sessionDataCol="session_data"
sessionValidCol="valid_session"
sessionMaxInactiveCol="max_inactive"
sessionLastAccessedCol="last_access"
sessionAppCol="app_name"
checkInterval="60"
debug="99"
/>
</Manager>
</Context>