今天team成員遇到個很奇怪的問題,在啟動和關閉Tomcat 6時, 拋出如下異常:Cannot serialize session attribute XXX for 。我很是納悶,到apache官網查看了下,發現tomcat 會試圖 serialize存在的session資源. 如果 sessions中相關的對象沒有實現 serializable 介面, 就會出現Cannot serialize session attribute XXX for 異常。由於開始對某個類實現了Serializable介面,啟動了tomcat,然後又取消實現Serializable介面,重啟tomcat就報錯了。原文如下:
Because when you shutdown tomcat, it tries to serial all objects within
the session scope. The
class you are referring to
'org.apache.commons.fileupload.DeferredFileOutputStream' does not
implement 'java.io.Serializable' and therefore tomcat throws this
exception when trying to serialize it.
Whilst I don't know what the class
'org.apache.catalina.session.StandardManager' does, presumably is
performs a check to see if an object stored within the session is
serializable before trying to
write it disk, or it might remove non-serializable object from the
session scope on shutdown.
如果你不想看到該異常, 也不想儲存session. 那麼你可以在項目部署描述檔案中(如. test.xml,)(instead of just exploding the war) 的 <Context> tags中間 加上 :
<Manager className="org.apache.catalina.session.PersistentManager"saveOnRestart="false"/>
這樣 tomcat 在關閉的時候就不會儲存session資源了.你也可以在server.xml中指定上面的值. 這也所有的程式都使用這個設定了.