Java HttpSession setattribute 在 qa 上抛出不可序列化的异常,但在本地环境中不会抛出异常
我保存到会话的对象是一个 LocalizationContext
,它是不可序列化的,我的 Tomcat 是 5.5.28,而 qa 服务器是 Tomcat 5.5.30。这是来自 Tomcat 文档:
每当 Catalina 关闭时 正常并重新启动,或者当 触发应用程序重新加载, 标准经理实施将 尝试序列化当前所有 到位于的磁盘文件的活动会话 通过路径名属性。凡此种种 保存的会话将是 反序列化并激活(假设 他们平均还没有过期 时间)当应用程序重新加载时 已完成。
为了成功恢复 会话属性的状态,所有这些 属性必须实现 java.io.Serialized 接口。你 可能会导致经理强制执行此规定 限制包括 网站中的
元素 应用程序部署描述符 (/WEB-INF/web.xml)。
这让我认为它也应该在我的本地上崩溃,并且您永远无法在 Tomcat 中保存会话属性,除非它实现了 Serialized
。
编辑:
抱歉,更清楚地说,我的问题有两个。所有会话属性都必须是可序列化的吗?如果是的话,为什么它仍然可以在我的本地环境中工作?
The object I'm saving to the session is a LocalizationContext
, which is not serializable, and my Tomcat is 5.5.28, while the qa server is Tomcat 5.5.30. This is from the Tomcat docs:
Whenever Catalina is shut down
normally and restarted, or when an
application reload is triggered, the
standard Manager implementation will
attempt to serialize all currently
active sessions to a disk file located
via the pathname attribute. All such
saved sessions will then be
deserialized and activated (assuming
they have not expired in the mean
time) when the application reload is
completed.In order to successfully restore the
state of session attributes, all such
attributes MUST implement the
java.io.Serializable interface. You
MAY cause the Manager to enforce this
restriction by including the<distributable>
element in your web
application deployment descriptor
(/WEB-INF/web.xml).
This makes me think it should be breaking on my local as well, and that you can never save a session attribute in Tomcat unless it implements Serializable
.
EDIT:
Sorry, to be more clear, my question is two-fold. Do all session attributes have to be serializable, and if they do, why might it still work on my local environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会话属性(以及会话)最好是可序列化的,特别是在开发用户会话只能保留以便稍后恢复的环境时。这或多或少是集群中会话故障转移/迁移的先决条件。
但集群并不是会话被序列化的唯一场景。在单个容器中,会话可以在容器关闭时保留,并在容器再次恢复时反序列化。当然这取决于容器的支持,包括Tomcat在内的很多容器都支持这个功能。
关于为什么错误不会在本地环境中显现的合理答案是,仅当尝试序列化会话时才会标记错误。容器不包括对会话中的属性值是否可序列化的检查。容器不会执行此检查;毕竟,这样的测试将包括会话的实际序列化,这可能是性能密集型的,唯一的好处是防止编程错误。
It is preferable that session attributes (and hence sessions) are serializable, especially when developing for environments where user sessions can be persisted only to be restored later. This is more or less a pre-requisite for failover/migration of sessions in a cluster.
But clustering is not the only scenario where sessions will be serialized. In a single container, sessions can be persisted when the container is being shutdown, and will be de-serialized when the container is brought back again. This of course, depends on the container support, and a lot of containers including Tomcat support this feature.
The rational answer on why the error does not manifest in the local environment is that the error is flagged only when there is an attempt to serialize the session. The container does not include checks on whether the attribute values in the session, on whether they are serializable or not. Containers wouldn't implement this check; after all, such a test would include actual serialization of the session, which can be performance intensive, with the only benefit of being defensive against programming mistakes.