In this tutorial, you will learn how to configure c3p0 - a popular database connection library for Java multi-threaded database applications - in a Hibernate/JPA project.
Hibernate has built-in connection pool but it is not for production use, as state in official Hibernate document and also in the logging information when you start a Hibernate application:
So it is recommended to use a third-party library like c3p0 which is a database connection pooling library for production use, and Hibernate works very well with c3p0.
Basically, it’s simple and easy to add database connection pooling capability to your existing project with c3p0: just add c3p0 dependency to the project’s Maven pom.xml file and specify some properties in Hibernate/JPA configuration file.
The first step to use c3p0 is to add its dependency to your project’s pom.xml file, with the following XML code:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>5.3.6.Final</version> </dependency>
You can see this dependency is provided by Hibernate. Note that the version should be compatible with Hibernate version you are using in your project. Save the file, and you can see there are 3 JAR files have been added to the project:
hibernate-c3p0-5.3.6.Final.jar c3p0-0.9.5.2.jar mchange-commons-java-0.2.11.jar
That means if you don’t use Maven, you can find, download and add these JAR files to your project.
c3p0 offers various configuration properties, but basically you need to use the following ones:
To see the complete list of configuration properties in c3p0, see Configuration Properties.
Then your Hibernate configuration file (hibernate.cfg.xml) would look like this:
<?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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yourdbname</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">200</property> <property name="hibernate.c3p0.timeout">180</property> <property name="hibernate.c3p0.max_statements">50</property> </session-factory> </hibernate-configuration>
In case you use Hibernate with JPA, then the JPA configuration file (persistence.xml) would look like this:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="YourUnitName"> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/yourdbname" /> <property name="javax.persistence.jdbc.user" value="username" /> <property name="javax.persistence.jdbc.password" value="password" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.c3p0.min_size" value="5" /> <property name="hibernate.c3p0.max_size" value="20" /> <property name="hibernate.c3p0.timeout" value="300" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.idle_test_period" value="120" /> </properties> </persistence-unit> </persistence>
And you're done.
You can look at the Hibernate’s log to check if c3p0 is initialized and running, as shown in the following screenshot:
If you use MySQL Workbench, click Server > Client Connections and you can see there are some sleep connections to your database:
That’s how to configure c3p0 database connection pooling library with Hibernate/JPA.