I have just got the relationship between Hibernate Session and Connection. But now, I get another question: how does hibernate sessionfactory manage session? In the following code segment: save() method of a DAO class:
Session session = sessionFactory.openSession();
Transaction tx=null;
tx=session.beginTransaction();
session.save(transientInstance);
session.flush();
tx.commit();
When we call sessionFactory.openSession()
, it will create a new session attached to the current thread (through the ThreadLocal), this session is also attached to a JDBC connection,
But, as you can see, we don't need to close the session (session.close()), neither the connection.
So, what is the lifecycle of a Hibernate session, in what circumstances it will be closed? automatically?