1
我在這個項目中使用Hibernate和Spring MVC。(Hibernate)Session.close()和releaseSession(Session)之間的區別
我有這樣一段代碼:
...
int count = (Integer) this.getSession().createSQLQuery(sql).list().get(0);
this.getSession().close();
return count;
我應該使用this.getSession.close()
,還是應該使用releaseSession(this.getSession())
??
有I'm煩惱理解這兩種方法之間的區別..
謝謝!
這個魔術'releaseSession'方法來自哪裏? –
http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/orm/hibernate3/support/HibernateDaoSupport.html#releaseSession(org.hibernate.Session) –
那麼你的答案是: _關閉給定的Hibernate Session,通過這個DAO的SessionFactory創建**,如果它沒有綁定到線程**(即不是事務性會話)._(強調增加)。 'close'方法**總是**關閉'session'。如果需要,'releaseSession'方法只會關閉'session'。如果你關閉了一個線程綁定的會話,那麼你的應用程序中的其他地方就會出現問題,因爲Spring並不期望它被關閉。 –