使用 JDBC 和 Tomcat 进行持久会话
我们有一个 Tomcat 服务器集群,它们共享一个运行 mod_jk 的公共 Web 服务器。我们目前使用粘性会话来处理会话处理,但我们希望转向 JDBC 会话共享。有没有人有好的资源或分步解决方案来处理这个问题?
我不确定这个问题是针对 stackoverflow、serverfault 还是 DBA,但它就是这样。 :)
编辑:
我认为我的问题的内容一定令人困惑。我所指的会话是用户会话 (JSESSIONID),而不是与数据库的连接。我想要做的是使用数据库来处理用户会话,以便当集群中的一台服务器出现故障时,用户可以无缝过渡到另一台服务器。现在,当服务器发生错误时,用户将被注销。
We have a cluster of Tomcat servers that share a common web server running mod_jk. We currently use sticky sessions to take care of session handling, but we would like to move to JDBC session sharing. Does anyone have a good resource or step-by-step solution to deal with this?
I was not sure if this question was meant for stackoverflow, serverfault, or DBA, but here it is. :)
EDIT:
I think the content of my question must be confusing. The sessions to which I am referring are user sessions (JSESSIONID), not connections to the database. What I want to do is use the database to handle the user sessions so that when one server in the cluster goes down, the transition to another server is seamless to the user. Right now, the user is logged out when an error on the server occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中大部分内容可在 Tomcat 文档 中找到,请参阅持久管理器实施。
您还可以查看 这个。
Most of this is available in Tomcat documentation, see Persistent Manager Implementation.
You can also look at this.
既然你说 JDBC,我假设你指的是 Java?您的问题似乎有些含糊不清,所以我不确定这是否是您想要的,但根据我的理解,我会尝试一下。不管怎样,我使用连接池(Apache commons dbcp)和Spring,这使得它变得非常简单。
然后在代码中我使用 Spring jdbctemplate,通过此设置,到数据库的连接将被池化并重用。数据源作为 Spring bean 进行管理,然后将依赖项注入到使用它的地方。 Spring 为您处理了 jdbc 会话的共享,瞧!以下是我如何使用注释进行依赖项注入:
即使您没有使用 Spring 进行 MVC 或其他任何东西,Spring JDBC 工具也非常好用。
Since you say JDBC, I'm assuming you mean in Java? Your question seems to have some ambiguity, so I'm not sure this is what you are looking for, but based on my understanding, I'll give it a shot. Anyway, I use connection pooling (Apache commons dbcp) and Spring, which makes it pretty easy.
Then in the code I use Spring jdbctemplate, and with this setup, the connections to the database are pooled and reused. The datasource is managed as a Spring bean, then dependency-injected into where it is used. Spring handled the sharing of the jdbc sessions for you, and voila! Here is how I do the dependency injection with annotations:
Even if you aren't using Spring for MVC or anything else, the Spring JDBC tools are really nice.