hibernate3 的session.class 源码中关于 close()方法的注释 到底是什么意思啊? /**
* End the <tt>Session</tt> by disconnecting from the JDBC connection and
* cleaning up. It is not strictly necessary to <tt>close()</tt> the
* <tt>Session</tt> but you must at least <tt>disconnect()</tt> it.
*
* @return the connection provided by the application
* or <tt>null</tt>
* @throws HibernateException
*/
public Connection close() throws HibernateException;
* End the <tt>Session</tt> by disconnecting from the JDBC connection and
* cleaning up. It is not strictly necessary to <tt>close()</tt> the
* <tt>Session</tt> but you must at least <tt>disconnect()</tt> it.
*
* @return the connection provided by the application
* or <tt>null</tt>
* @throws HibernateException
*/
public Connection close() throws HibernateException;
因为hibernate官方推荐的sessionFactory用到了ThreadLocal,则一个请求对应一个Thread,而这个thread对应着一个它独享的session. 所以你每次用完了以后没有必要关闭, 应为下次你再次请求的时候,你还用你自己的session。
这样做有两个好处:
1是对一个用户而言,不会频繁的创建session;
2是有时候session关闭会出问题, 比如你如果用到了延迟加载的时候;
建议你看一下ThreadLocal这个东西,然后再看看hibernate sessionFactory的官方实现。