15

I am confused about the hibernate session and connection pool, are they the same thing?

1 Answer 1

21

Hibernate is an ORM, it is a layer between a SQL database and your POJOs.

A connection pool provides a way to store and reuse java.sql.Connection instances for speed and robustness.

A hibernate Session is a wrapper around a Connection in order to allow you to save your POJOs without directly writing the SQL.

So a hibernate Session is a wrapper around a Connection. Connections are held in a connection pool.

When you call SessionFactory.openSession hibernate first takes a Connection from the supplied connection pool. It then creates a Session around that Connection and returns it.

4
  • Does it close the connection on closing the session?
    – user_mda
    Aug 10, 2015 at 20:03
  • @user_mda you should not be thinking about connections when using JPA. It can do what it wants under the hood. Practically speaking it would probably call close on the Connection, yes, but that's an implementation detail. Aug 10, 2015 at 20:04
  • what is the difference between session.close() and session.flush()?
    – user_mda
    Aug 11, 2015 at 15:34
  • but HibernateUtil.getNewSession() does not create a new connection? May 24, 2022 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.