java Tomcat:在数据库中存储会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat: Store session in database
提问by Fabe
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?
我正在寻找一种方法来避免内存中会话复制/集群并将会话存储在数据库中。此时使用 Tomcat 的 JDBCStore 是无用的,因为它只在数据库中存储非活动会话以节省服务器内存。有什么建议?
Thanks upfront Fabian
预先感谢法比安
回答by Bozho
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
.
如果您不想以应该使用的方式使用会话,那么根本不要使用它 - 开发您自己的会话对象。它仍然可以实现HttpSession
,甚至可以从 的实现扩展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.
您可以使用 aFilter
来包装您的请求,以便它返回您的会话对象而不是标准对象。在您的会话中,您可以将内容存储在数据库中而不是内存中。
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.
您可以使用Hazelcast,而不是写入数据库,它提供分布式集合。但我想这将花费与配置会话复制相同的工作量。会话复制并不是那么难——所有容器都支持它。
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.
为了避免复制的需要,您可以尝试使用粘性会话 - 即当用户被负载均衡器定向到服务器时,该用户的每个后续请求都会发送到同一台服务器。
回答by mindas
You might want to look at this project, I would rather prefer storing sessions in memcached rather than in the DB.
您可能想看看这个项目,我宁愿将会话存储在 memcached 中而不是在数据库中。
回答by Fabe
Seems to me there is no way then implementing an JDBC Session Manager on my own, but thanks for your answers and time - Hazelcast needs some closer look, seems very powerful for me. I will do something similar like J. Brisbin: http://jbrisbin.com/web2/articles/tomcat-session-manager-backed-by-riak/
在我看来,没有办法自己实现 JDBC 会话管理器,但感谢您的回答和时间 - Hazelcast 需要仔细研究,对我来说似乎非常强大。我会做类似 J. Brisbin 的事情:http: //jbrisbin.com/web2/articles/tomcat-session-manager-backed-by-riak/