Hibernate:ThreadLocalSessionContext:在调用 bind() 时已经绑定了会话
当我尝试按以下方式使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,尽管 文档表示“如果在此类管理的会话上调用 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.