Opening and Closing a Hibernate Session

This is how I get a Hibernate session and make a request.

HSession.getSession().createQuery("query here").executeUpdate(); 

AND

 Critaria cr=HSession.getSession().createCritaria(..).. ; 

HSession is where my factory session is located and the getSession() method returns a new session

 (getSessionFactory().openSession();) 

I want to know,

  • After calling cr.list(); Is the session still alive?
  • If alive, getting this criterion or querying is not good? and
  • Create a session as

    Session s = HSession.getSession ();
    s.createCriteria ...

    Is it possible to use a session and close it with s.close(); ?

+6
source share
2 answers

Yes, the session will be alive until you close it. You can perform several operations against the session, but only close() will close it.

In your case, it looks like the sessions are controlled by any HSession . You need to look at this to see if any transactions are running, how sessions are managed, etc.

+8
source

I read about it today ... he said: "The session opens when getCurrentSession () is called for the first time and closes when the transaction ends." Therefore, according to this: if you have a transaction wrapped around it (and you have to guess) and call transaction.commit () ... the session is closed.
In your case, it should still be open.

Please correct me if I am wrong about this ...! :)

+5
source

Source: https://habr.com/ru/post/901699/


All Articles