且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关闭打开的休眠会话(如果存在)

更新时间:2022-10-24 08:22:15

Why dont you close the session when your database operation finished? I mean, In DAO classes you get opened session perform database operation. And in finally block, Close your session. You can close session like :

finally {
   if(session!=null){
      session.close();
   }
}

OR

You can get the current session using

Session sess = sessionFactory.getCurrentSession();

And close session on closing event of JFrame.

I get following lines from this link

The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory. The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping. Implementors must be threadsafe.

And it is our duty to close session when finished the operation or transaction. When we close sessionfactory all resources(connection pools etc) are released properly.

相关阅读

技术问答最新文章