getSession() 和 getNewSession() 之间的区别
使用 Spring 的 Hibernate SessionFactoryUtils,getSession() 和 getNewSession() 之间的实际区别是什么?
我一直在 DAO 方法中使用 getSession() ,但是当它开始经常被调用时,一堆“会话已关闭”异常开始出现。我将其更改为 getNewSession(...),现在好像这些问题已经消失了...但是,我仍然需要知道。
请解释一下。
Using Spring's SessionFactoryUtils for Hibernate, what is the actual difference between getSession() and getNewSession()?
I've been getSession() in a DAO method, but when it started to get called quite often, a bunch of "Session is closed" exceptions started to appear. I changed it to getNewSession(...), and now as if these problems have gone away ... but still, I need to know.
Please, explain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getSession()
将尝试查找绑定到当前线程的预先存在的 Session,并在必要时创建一个。getNewSession()
将始终创建会话。 Javadoc在这里: http:// static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/SessionFactoryUtils.html。如果您觉得自己经常收到会话已关闭错误,请尝试查找会话被释放的位置。
getSession()
will try to find a pre-existing Session that's bound to the current thread, creating one if necessary.getNewSession()
will always create the session. Javadocs here: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/SessionFactoryUtils.html.If you feel like you're getting the session is closed error too often, try to find where your session is being released.