Hibernate Dbcp Connection Pooling Configuration
This tutorial shows you how to configure Hibernate dbcp Connection Pooling. By default hibernate comes with a built-in connection pool. But this connection pool is by no means production ready. They even advice not to use it in production, as you can see in the logging.
HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Maven Dependencies
We are using Apache Maven to manage our project dependencies. Add the following dependencies to your projects pom.xml
file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
Hibernate dbcp Connection Pooling Configuration
The database connections and hibernate dbcp connection pooling configuration are in the hibernate.cfg.xml
file, located on the classpath in the src/main/resources
folder.
By default, dbcp uses sensible defaults, but you can override these settings by setting the following properties.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- database connection properties -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore?serverTimezone=Europe/Brussels</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- show mysql queries output in console -->
<property name="hibernate.show_sql">false</property>
<!-- manage automatic database creation -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- dbcp connection pool configuration -->
<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>
<!-- add annotated resources here -->
<mapping class="com.memorynotfound.hibernate.Book"/>
</session-factory>
</hibernate-configuration>
initialSize
: The initial number of connections that are created when the pool is started.maxTotal
: The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.maxIdle
: The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.minIdle
: The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none.maxWaitMillis
: The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely.
Demo
When using the previous configuration, dbcp initializes the connection pool with 5 connections at startup. So when we run the application – we can see in the following screenshot – that there are 5 connections waiting in the pool. Also that there is one active connection being used.