Tomcat:在数据库中存储会话

编程入门 行业动态 更新时间:2024-10-24 22:22:09
本文介绍了Tomcat:在数据库中存储会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种避免内存会话复制/群集的方法,并将会话存储在数据库中。此时使用Tomcat的JDBCStore是无用的,因为它只在数据库中存储非活动会话以保存服务器内存。 有什么建议吗?

I am searching for a way to avoid in-memory session replication/clustering and store the session in a database. Using Tomcat's JDBCStore is useless at this point, because it only stores inactive sessions in the database to save the servers memory. Any suggestions?

先谢谢 Fabian

Thanks upfront Fabian

推荐答案

如果您不想按照应该使用的方式使用会话,那么根本不要使用它 - 开发自己的会话对象。它仍然可以实现 HttpSession ,甚至可以从 HttpSession 的实现扩展。

If you don't want to use the session in the way it is supposed to be used, then don't use it at all - develop your own session object. It can still implement HttpSession, and even extend from an implementation of HttpSession.

您可以使用过滤器来包装您的请求,以便它返回您的会话对象而不是标准对象。在您的会话中,您可以将内容存储在数据库而不是内存中。

You can use a Filter to wrap your request so that it returns your session object rather than the standard one. In your session you can store things in DB instead of in-memory.

您可以使用 Hazelcast - 它提供分布式集合。但我想这将花费与配置会话复制相同的工作量。并且会话复制并不困难 - 所有容器都支持它。

Instead of writing to the DB, you can use Hazelcast - it provides distributed collections. But I guess it will take the same amount of effort as to configure session replication. And session replication is not something that hard - it is supported by all containers.

这些是粗略的指导方针,任务不会是微不足道的。我建议坚持使用标准会话使用模式,并且只在真正需要时才将内容存储在数据库中。

These are rough guidelines, the task will not be trivial. And I'd recommend sticking to the standard session usage patterns, and storing things in DB only if really needed.

为了避免复制的需要,您可以尝试使用粘性会话 - 即当负载均衡器将用户定向到服务器时,该用户的每个后续请求发送到同一台服务器。

In order to avoid the need of replication you might try using sticky sessions - i.e. when a user is directed to a server by the load balancer, each subsequent request by that user is sent to the same server.

更多推荐

Tomcat:在数据库中存储会话

本文发布于:2023-10-24 13:39:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   Tomcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!