[轉]tomcat中的session管理

來源:互聯網
上載者:User

標籤:blog   http   檔案   資料   art   cti   

轉載地址:http://blog.csdn.net/iloveqing/article/details/1544958

當一個sesson開始時,Servlet容器會建立一個HttpSession對象,在某些情況下把這些Httpsession對象從記憶體中轉移到檔案 系統中或資料庫中,需要訪問的時候在把它們載入到記憶體中來。這樣做的好處有兩點:節約了記憶體的消耗,當web伺服器產生故障時,還可以從檔案系統或資料庫 中恢複Session的資料。
對於Session的管理,小貓提供了兩個實作類別:org.apache.catalina.session.StandardManager和org.apache.catalina.session.PersistentManager。
StandardManager -是預設的方法,當Tomcat伺服器重啟或重載的時候,會把Session對象儲存到
<%CATALINA_HOME%>/work/Catalina/honstname/applicatonname/SESSIONS.ser(預設值)檔案中,每個對象對應一個檔案,以Session ID為檔案名稱,例如:

<Context path="/helloapp" docBase="helloapp" debug="0" reloadable="true">
<Manager className="org.apache.catalina.session.StandardManager" debug="0"
maxActiveSessions="-1" checkInterval="60" />
</Context>
參數說明:checkInterval-檢查session是否到期的時間間隔,以秒為單位,預設值是60秒;
maxActiveSessions-可處於活動狀態的session數。

PersistentManager -提供了更加靈活的管理方式,具有容錯能力,可以及時把Session備份到Session Store中,可以控制記憶體中Session的數量。
小貓還提供了實現持久化Session Store的介面,org.apache.catalina.Store,目前提供了兩個具體實作類別:org.apache.catalina.FileStore和org.apache.catalina.JDBCStore。
server.xml中的配置File Store -

<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>
參數說明:saveOnRestart-伺服器關閉時,是否將所有的session儲存到檔案中;
maxActiveSessions-可處於活動狀態的session數;
minIdleSwap/maxIdleSwap-session處於不活動狀態最短/長時間(s),sesson對象轉移到File Store中;
maxIdleBackup-超過這一時間,將session備份。(-1表示沒有限制) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.