Table of Contents
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>
Warning
Do not use the following tag in combination with this connection pool.
<property name="hibernate.hbm2ddl.auto">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 http://sourceforge.net/projects/c3p0
The library needed is
DBCP is not included in the Hibernate 3 distribution any more. If you want to use it have a look at http://wiki.apache.org/jakarta-commons/DBCP/Hibernate to get a custom connection provider. This connection provider must be specified in the hibernate.cfg.xml
<property name="connection.provider_class"> org.hibernate.connection.DBCPConnectionProvider </property>
Download and documentation can be found at http://jakarta.apache.org/commons/dbcp/
DBCP depends on two libraries, which are provided by jakarta as well:
To configure DBCP add the following properties to your hibernate.cfg.xml
<property name="connection.provider_class"> org.hibernate.connection.DBCPConnectionProvider </property> <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="dbcp.ps.maxActive">150</property> <property name="dbcp.ps.whenExhaustedAction">2</property> <property name="dbcp.ps.maxWait">120000</property> <property name="dbcp.ps.maxIdle">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"> <connection-url>jdbc:postgresql://localhost/play</connection-url> <driver>postgresql</driver> <security> <user-name>postgres</user-name> <password>p</password> </security> </datasource>
In your Hibernate configuration, the JNDI is referenced.
<property name="connection.datasource">java:/jboss/datasources/playDS</property>