Connection Pooling
Opening a connection to a database is generally much more expensive than executing an SQL statement. A connection pool is used to minimize the number of connections opened between application and database. It serves as a librarian, checking out connections to application code as needed. Much like a library, your application code needs to be strict about returning connections to the pool when complete, for if it does not do so, your application will run out of available connections.
Hibernate supports a variety of connection pooling mechanisms. If you are using an application server, you may wish to use the built-in pool (typically a connection is obtaining using JNDI). If you can't or don't wish to use your application server's built-in connection pool, Hibernate supports several other connection pools, as shown in Table 10.1.
Table 10.1. Hibernate-Supported Connection Pools
c3p0 |
Distributed with Hibernate |
|
Apache DBCP |
Apache Pool |
|
Proxool |
JDBC Pooling Wrapper |
The choice of a connection pool is up to you, but be sure to remember that a connection pool is necessary for every production use.
If you wish to use c3p0, the version distributed with Hibernate 2.1.2 (0.8.3) is out of date (and GPL is a problem if you wish to distribute a non-GPL application). If you wish to distribute an application that makes use of c3p0, make sure to download the latest (LGPL) release, c3p0-0.8.4-test1 or later.
Because Hibernate ships with c3p0, configuration is a simple matter of adding a few Hibernate configuration properties to your hibernate.properties (or hibernate.cfg.xml) file. Listing 10.2 shows an example of the configuration of c3p0.
Example 10.2. Sample Hibernate c3p0 Configuration
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost/hibernate hibernate.connection.username=root hibernate.connection.password= hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect hibernate.show_sql=false hibernate.c3p0.max_size=1 hibernate.c3p0.min_size=0 hibernate.c3p0.timeout=5000 hibernate.c3p0.max_statements=100 hibernate.c3p0.idle_test_period=300 hibernate.c3p0.acquire_increment=2 hibernate.c3p0.validate=false
The properties shown in Listing 10.2 are as described in Table 10.2.
Table 10.2. c3p0 Configuration Options
Property Meaning |
Property |
Example |
Maximum number |
hibernate.c3p0. |
15 |
Initial number |
hibernate.c3p0. |
3 |
Maximum idle time |
hibernate.c3p0. |
5000 |
Maximum size |
hibernate.c3p0. |
0 |
Number of connections |
hibernate.c3p0. |
3 |
Idle time before a |
hibernate.c3p0. |
300 |
Validate the |
hibernate.c3p0 |
true | false |
If you prefer to use Apache DBCP, make sure that the Apache DBCP library is on your class path, and add the properties to your hibernate.properties file, as shown in Table 10.3.
Table 10.3. Apache DBCP Configuration Options
Property Meaning |
Property |
Example |
Maximum number |
hibernate. |
8 |
Maximum number |
hibernate. |
8 |
Maximum idle |
hibernate. |
-1 |
Set to -1 to turn off |
||
Action to take in case |
hibernate.dbcp. |
1 |
Validate connection |
hibernate.dbcp. |
true | false |
Validate connection |
hibernate.dbcp. |
true | false |
Query to execute |
hibernate.dbcp. |
Valid SQL validation |
Maximum number |
hibernate.dbcp. |
8 |
Maximum number |
hibernate.dbcp. |
8 |
Maximum idle time |
hibernate.dbcp. |
1000 * 60 * 30 |
Action to take in case |
hibernate.dbcp.
ps. |
1 |
Finally, if you wish to use Proxool as your connection pool provider, you will need to specify hibernate.properties values as shown in Table 10.4. Unlike c3p0 and DBCP, you will need to include additional configuration options as described at http://proxool.sourceforge.net/configure.html.
Table 10.4. Proxool Configuration Options
Property Meaning |
Property |
Example |
---|---|---|
Configure Proxool |
hibernate. |
/path/to/ |
Configure the |
hibernate. |
/path/to/ |
Configure the |
hibernate. |
true | |
Proxool pool alias |
hibernate. |
As set by |