Chapter 17. Configuration

Table of Contents

Connection Pools
Built-in connection pool

Connection Pools

Built-in connection pool

If you do not configure a connection pool yourself, the built-in connection pool is used. Of course, it usually does work when you develop, but in some rare cases it does not work properly. So just don’t use it. When you test your Hibernate layer without an application server use C3P0 or DBCP. Normally it is a good idea to use the connection pool provided by your application server if you only run tests within the application server. You may use C3P0 or DBCP also within an application server but there is normally no reason to deploy any further libraries when the functionality is already there.


You can configure a minimal pool in Hibernate just by adding the following settings:

    <property name="c3p0.max_size">4</property>
    <property name="c3p0.min_size">2</property>
    <property name="c3p0.timeout">1800</property>


Do not use the following tag in combination with this connection pool.

<property name="">update</property>

The schema generation will close connections in a way the Pool cannot cope with.

Some optional settings are

    <property name="c3p0.acquire_increment">1</property>
    <property name="c3p0.idle_test_period">100</property><!-- seconds -->
    <property name="c3p0.max_statements">0</property>
    <property name="c3p0.AcquireRetryDelay">1001</property>
    <property name="c3p0.AcquireRetryAttempts">5</property>
    <property name="c3p0.MaxStatementsPerConnection">0</property>

Documentation can be found in the download package. The libraries are included in the Hibernate dialogue, but I recommend to download the most current version from

The library needed is

  • c3p0….some version.jar


DBCP is not included in the Hibernate 3 distribution any more. If you want to use it have a look at to get a custom connection provider. This connection provider must be specified in the hibernate.cfg.xml

<property name="connection.provider_class">

Download and documentation can be found at

DBCP depends on two libraries, which are provided by jakarta as well:

  • commons-dbcp
  • commons-pool

To configure DBCP add the following properties to your hibernate.cfg.xml

<property name="connection.provider_class">

<property name="dbcp.maxActive">17</property>
<property name="dbcp.maxWait">120000</property>
<property name="dbcp.maxIdle">3</property>
<!-- Action to take in case of an exhausted DBCP connection pool
   ( 0 = fail, 1 = block, 2= grow) -->
  <property name="dbcp.whenExhaustedAction">1</property>
  <property name="">150</property>
  <property name="">2</property>
  <property name="">120000</property>
  <property name="">10</property>
  <property name="dbcp.validationQuery">select 1</property>
  <property name="dbcp.testOnBorrow">true</property>
  <property name="dbcp.testOnReturn">false</property>


JNDI is the Java Naming and Directory Interface. It allows to bind resources to names. You can bind Java classes like Data sources, session factories or other to JNDI and retrieve them using their name.

You have to setup the JNDI datasource in your application server or servlet engine.

Here is an extract of the standalone.xml of a JBoss application server.

<datasource jndi-name="java:/jboss/datasources/playDS" pool-name="playDS">

In your Hibernate configuration, the JNDI is referenced.

<property name="connection.datasource">java:/jboss/datasources/playDS</property>