Connection Pooling Implementation

Connection Pooling Implementation

In Hibernate applications, there are 2 ways to implement Connection Pooling.

  1. Hibernate Default Connection Pooling Mechanism
  2. Third Party Vendors provided Connection pooling mechanism
1. Hibernate Default Connection Pooling Mechanism

In Hibernate applications, to interact with databases hibernate software is generating Connection objects by using its built-in Connection pooling mechanism. Hibernate Software has implemented its built-in connection pooling mechanism in the form of a predefined class like

org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl

Hibernate provided built-in Connection pooling mechanism is able to allow 20 Connections as max count and 1 connection as min count, we can modify this pool size in hibernate applications as per the requirement by using “connection.pool_size” property in hibernate configuration file.

<hibernate-configuration>
   <session-factory>
      -----
      <property name="connection.pool_size">10</property>
      -----
   </session-factory>
</hibernate-configuration>

Note

Hibernate Software provided built-in connection pooling mechanism is suggestible up to Development and Testing phases, it is not suggestible for Production mode of our project.

2. Third Party Vendors provided Connection pooling mechanism

In general, in database related applications we will use the following three third party vendors provided connection pooling mechanisms.

  1. DBCP
  2. C3P0
  3. Proxool

Note

Hibernate is not providing support for DBCP Connection pooling mechanism, but, Hibernate has provided predefined support for C3P0 and Proxool connection pooling mechanisms.

C3P0 Connection Pooling Mechanism In Hibernate

If we want to use C3P0 Connection pooling mechanism in hibernate applications then we have to declare the following properties in hibernate configuration file.

hibernate.connection.provider_class

This property will take Connection Pooling provider class which was provided by hibernate software for C3P0 connection pooling mechanism in the form of “org.hibernate.c3p0.internal.C3P0ConnectionProvider”.

Note

C3P0 ConnectionProvider class will activate C3P0 connection pooling mechanism in Hibernate applications.

hibernate.c3p0.min_size

It will take an int value which is representing minimum no of Connection objects in a pool.

hibernate.c3p0.max_size

It will take int value which is representing maximum no of Connection objects in a pool.

hibernate.c3p0.timeout

It will take connections idle time to destroy.

hibernate.c3p0.max_statements

It will take an int value representing no of statements max for Connection objects.

E.g

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <property name="show_sql">true</property>
      <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
      <property name="hibernate.c3p0.min_size">1</property>
      <property name="hibernate.c3p0.max_size">19</property>
      <property name="hibernate.c3p0.timeout">120</property>
      <property name="hibernate.c3p0.max_statements">10</property>
      
      <mapping class="com.ashok.hibernate.pojo.Employee"/>
   </session-factory>
</hibernate-configuration>

Note

Add the following jars to the Hibernate Library.

  1. c3p0-0.9.2.1.jar
  2. hibernate-c3p0-4.3.11.Final.jar
  3. mchange-commons-java-0.2.3.4.jar
Proxool Connection Pooling Mechanism

If we want to use Proxool Connection Pooling mechanism in hibernate applications then we have to use the following steps.

proxool configuration file

It is an XML file, it includes all configuration details of proxool connection pooling mechanism which includes driver class name, driver URL, database user name, database password, minimum connection count, maximum connection count

To provide the above configuration details we have to use the following XML tags.

proxool.xml

<?xml version="1.0" encoding="UTF-8"?>
<proxool-config>
   <proxool>
      <alias>proxool</alias>
      <driver-class>oracle.jdbc.OracleDriver</driver-class>
      <driver-url>jdbc:oracle:thin:@localhost:1521:xe</driver-url>
      <driver-properties>
         <property name="user" value="system" />
         <property name="password" value="ashok" />
      </driver-properties>
      <minimum-connection-count>10</minimum-connection-count>
      <maximum-connection-count>30</maximum-connection-count>
   </proxool>
</proxool-config>

To configure proxool configuration file in hibernate configuration file we have to use the following tags along with dialect and mapping configurations.

hibernate.connection.provider_class

It will take Proxool connection pooling provider class which was provided by hibernate software in the form of “org.hibernate.connection. ProxoolConnectionProvider” in order to activate proxool connection
pooling mechanism.

hibernate.proxool.pool_alias

It will take proxool alias name which we defined in proxool configuration file.

hibernate.proxool.xml

It will take the name and location of proxool configuration file.

E.g

<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
      <property name="hibernate.proxool.pool_alias">proxool</property>
      <property name="hibernate.proxool.xml">proxool.xml</property>
      <mapping class="com.ashok.hibernate.pojo.Employee" />
   </session-factory>
</hibernate-configuration>

Note

To use this mechanism, we have to add the following JAR files in Hibernate Library.

  1. proxool-0.8.3.jar
  2. hibernate-proxool-4.3.11.Final.jar
Connection Pooling Implementation
Scroll to top