session的断开是在org.springframework.orm.hibernate.SessionFactoryUtils中处理的。
如果应用程序使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,那么所有的打开过的session会注册在ThreadLocal里。在当前线程退出前,OpenSessionInViewFilter或者OpenSessionInViewInterceptor将会负责断开这些session。
《1》
如果应用程序没有应用OpenSessionInViewFilter或者OpenSessionInViewInterceptor,那么只要该session没有Transaction就直接关闭。否则在transaction结束后关闭。
《2》
直接使用的是getHibernateTemplate().得到session就不需要你来管理,sparing会帮助你关闭的,无论你用的那种模式
如果你直接使用
Session session = this.getSession();
Query q =session。creatQuery。。。
这个时候就需要你自己来管理session的关闭问题了
可以是在dao中通过
finally {
this.closeSessionIfNecessary(session);
}
或者使用
sessionFactoryUtil中的方法关闭session
《3》
由于this.closeSessionIfNecessary(session);
现在是不推荐使用的方法,所以可以使用
DAO中调用this.releaseSession(session);