Usually, you want to have the
org.hibernate.SessionFactory create and pool
JDBC connections for you. If you take this approach, opening a
is as simple as:
Session session = sessions.openSession(); // open a new Session
As soon as you do something that requires access to the database, a JDBC connection will be obtained from the pool.
For this to work, we need to pass some JDBC connection properties to Hibernate. All Hibernate property
names and semantics are defined on the class
org.hibernate.cfg.Environment. We will
now describe the most important settings for JDBC connection configuration.
Hibernate will obtain (and pool) connections using
if you set the following properties:
Table 3.1. Hibernate JDBC Properties
|hibernate.connection.driver_class||JDBC driver class|
|hibernate.connection.password||database user password|
|hibernate.connection.pool_size||maximum number of pooled connections|
Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is not intended for use in a production system or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use C3P0.
C3P0 is an open source JDBC connection pool distributed along with Hibernate in the
directory. Hibernate will use its
for connection pooling if you set hibernate.c3p0.* properties. If you'd like to use Proxool
refer to the packaged
hibernate.properties and the Hibernate web site for more
Here is an example
hibernate.properties file for C3P0:
hibernate.connection.driver_class = org.postgresql.Driver hibernate.connection.url = jdbc:postgresql://localhost/mydatabase hibernate.connection.username = myuser hibernate.connection.password = secret hibernate.c3p0.min_size=5 hibernate.c3p0.max_size=20 hibernate.c3p0.timeout=1800 hibernate.c3p0.max_statements=50 hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
For use inside an application server, you should almost always configure Hibernate to obtain connections
from an application server
javax.sql.Datasource registered in JNDI. You'll
need to set at least one of the following properties:
Table 3.2. Hibernate Datasource Properties
|hibernate.connection.datasource||datasource JNDI name|
|hibernate.jndi.url||URL of the JNDI provider (optional)|
class of the JNDI
|hibernate.connection.username||database user (optional)|
|hibernate.connection.password||database user password (optional)|
Here's an example
hibernate.properties file for an application server provided JNDI
hibernate.connection.datasource = java:/comp/env/jdbc/test hibernate.transaction.factory_class = \ org.hibernate.transaction.JTATransactionFactory hibernate.transaction.manager_lookup_class = \ org.hibernate.transaction.JBossTransactionManagerLookup hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
JDBC connections obtained from a JNDI datasource will automatically participate in the container-managed transactions of the application server.
Arbitrary connection properties may be given by prepending "
hibernate.connection" to the
connection property name. For example, you may specify a charSet
connection property using hibernate.connection.charSet.
You may define your own plugin strategy for obtaining JDBC connections by implementing the
org.hibernate.connection.ConnectionProvider, and specifying your
custom implementation via the hibernate.connection.provider_class property.