Let’s start by adding the dependency for c3p0 in our application’s pom.xml.
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.2.7.Final</version> </dependency>
Next, let’s edit the hibernate.cfg.xml for configuring the connection pool:
<?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> <!--Connection pool settings--> <property name="hibernate.c3p0.max_size">5</property> <property name="hibernate.c3p0.min_size">2</property> <property name="hibernate.c3p0.timeout">300</property> <property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <!--Logging configuration--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!--Mapping files--> <mapping class="hello.Message"/> </session-factory> </hibernate-configuration>
Let’s discuss about the configuration properties:
- hibernate.c3p0.max_size configures the maximum number of connections in the pool
- hibernate.c3p0.min_size configures the minimum number of connections in the pool
- hibernate.c3p0.timeout specifies the time after which an idle connection is removed from the pool
- hibernate.c3p0.max_statements specifies the number of prepared statements that will be cached
- hibernate.c3p0.idle_test_period specifies the idle time in seconds before a connection is automatically validated
Recent Comments