Usually, you want to have the org.hibernate.SessionFactory
create and pool JDBC connections for you. If you take this approach, opening a org.hibernate.Session
is as simple as:
Session session = sessions.openSession(); // open a new Session
Dès que vous ferez quelquechose qui requiert un accès à la base de données, une connexion JDBC sera récupérée dans le 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 java.sql.DriverManager
if you set the following properties:
Tableau 3.1. Propriétés JDBC d'Hibernate
Property name | Purpose |
---|---|
hibernate.connection.driver_class | Classe du driver jdbc |
hibernate.connection.url | URL jdbc |
hibernate.connection.username | utilisateur de la base de données |
hibernate.connection.password | mot de passe de la base de données |
hibernate.connection.pool_size | nombre maximum de connexions dans le pool |
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 lib
directory. Hibernate will use its org.hibernate.connection.C3P0ConnectionProvider
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 information.
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:
Tableau 3.2. Propriété d'une Datasource Hibernate
Property name | Purpose |
---|---|
hibernate.connection.datasource | Nom JNDI de la datasource |
hibernate.jndi.url | URL of the JNDI provider (optional) |
hibernate.jndi.class | class of the JNDI InitialContextFactory (optional)
|
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 datasource:
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
Les connexions JDBC obtenues à partir d'une datasource JNDI participeront automatiquement aux transactions gérées par le conteneur du serveur d'applications.
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 interface org.hibernate.connection.ConnectionProvider
, and specifying your custom implementation via the hibernate.connection.provider_class property.