【发布时间】:2013-07-29 15:00:42
【问题描述】:
我在这个项目中使用 Hibernate 和 Spring MVC。
我有这段代码:
...
int count = (Integer) this.getSession().createSQLQuery(sql).list().get(0);
this.getSession().close();
return count;
我应该使用this.getSession.close(),还是应该使用releaseSession(this.getSession())??
我很难理解这两种方法之间的区别..
谢谢!
【问题讨论】:
-
这个神奇的
releaseSession方法从何而来? -
那么这就是你的答案:关闭通过这个 DAO 的 SessionFactory 创建的给定 Hibernate Session,如果它没有绑定到线程(即不是一个事务性会话)。(添加了重点)。
close方法将总是关闭session。releaseSession方法只会在需要时关闭session。如果你关闭一个线程绑定会话,那么你的应用程序的其他地方就会出现问题,因为 Spring 不希望它被关闭。 -
@BoristheSpider 感谢您的回答,现在.. 可以使用 releaseSession(this.getSession()) ??
标签: java hibernate session spring-mvc