Hibernate:ThreadLocalSessionContext:在调用 bind() 时已经绑定了会话

发布于 2024-07-15 19:36:18 字数 487 浏览 7 评论 0原文

当我尝试按以下方式使用 ThreadLocalSessionContext 时:

Session hsession = HibernateUtils.getSession();
ThreadLocalSessionContext.bind(hsession);
// do stuff
hsession.close();

我对每个 Struts Action 都执行此操作。 我是否做错了什么,导致出现以下错误?

[ThreadLocalSessionContext] Already session bound on call to bind(); make sure you clean up your sessions!

我检查了所有包含 ThreadLocalSessionContext.bind 的文件,并确保这些会话已显式关闭。 有没有办法监视会话何时绑定到会话工厂?

谢谢!!

When I try to use the ThreadLocalSessionContext in the following way:

Session hsession = HibernateUtils.getSession();
ThreadLocalSessionContext.bind(hsession);
// do stuff
hsession.close();

I do this for every single Struts Action. Is there something I am doing wrong, causing me to get the following error?

[ThreadLocalSessionContext] Already session bound on call to bind(); make sure you clean up your sessions!

I checked all my files that had ThreadLocalSessionContext.bind in them and made sure these sessions are explicitly closed. Is there a way to monitor when sessions are binded onto session factories?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-07-22 19:36:18

事实证明,尽管 文档表示“如果在此类管理的会话上调用 close(),它将自动解除绑定。” 它实际上不是自动的!

您必须显式调用 ThreadLocalSessionContext.unbind(sessionFactory) 来取消绑定会话。 否则,即使会话关闭,它仍然保持与 SessionFactory 的绑定。

As it turns out, although the documentation indicates that "If close() is called on a session managed by this class, it will be automatically unbound." It is actually NOT automatic!

You have to explicitly call ThreadLocalSessionContext.unbind(sessionFactory) to unbind the session. Otherwise, even if a session gets closed, it still stays binded to the SessionFactory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文