✅ How does Hibernate handle database connections? Does it use HikariCP under the hood?
Answer:
🔹 How Hibernate manages connections:
- Hibernate doesn’t open raw database connections manually each time you run a query. Instead, it uses a connection pool to efficiently reuse existing connections.
- The pool manages a fixed number of database connections that are checked out when needed and returned when you close the
Session
.
🔹 Connection providers:
- Hibernate can use different ConnectionProvider implementations to manage connections:
- A direct JDBC connection (inefficient, rarely used in production).
- A third-party connection pool, like C3P0, Apache DBCP, or HikariCP.
- A connection pool managed by an application server (e.g., a JNDI datasource in a Java EE environment).
🔹 Does Hibernate use HikariCP by default?
- Since Hibernate 5.2.10, HikariCP became the default connection pool when using the official Hibernate libraries together with JPA.
- If you don’t explicitly configure a connection pool, Hibernate will automatically use HikariCP as its
ConnectionProvider
, provided the HikariCP library is on the classpath (e.g., it’s included by default if you use Spring Boot withspring-boot-starter-data-jpa
).
🔹 How to configure HikariCP with Hibernate:
- In Spring Boot, Hibernate inherits the datasource configuration, and
spring.datasource.hikari.*
properties directly configure HikariCP. - If you’re configuring Hibernate manually, you’d include HikariCP in your dependencies and set properties like:
hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
hibernate.hikari.maximumPoolSize=20
hibernate.hikari.minimumIdle=5
✅ Key takeaway:
Hibernate doesn’t directly manage raw connections; it delegates connection management to a connection pool — and HikariCP is the recommended and often default pool for modern Hibernate setups.