Hibernate support third-party connection pool, the official recommendation of the connection pool is C3P0, Proxool, and DBCP. When configuring connection pool to note three points:
1, Apche of DBCP in the Hibernate2 is supported, but is no longer recommended in Hibernate3, the official explanation is that this connection pool flawed. If for some reason you need to Hibernate3 to use DBCP, recommended JNDI approach.
2, by default (that is not configured connection pool case), Hibernate will use the built-in connection pooling. But the poor performance of the connection pool, and there are many BUG, so the government has also just a proposal to use only in development environment.
3, Hibernate2 and Hibernate3 namespace change. For example, configure the C3P0 when provider_class have Hibernate2 environments net.sf.hibernate.connection.C3P0ConnectionProvider, in Hibernate3 environments org.hibernate.connection.C3P0ConnectionProvider.
The following are several common circumstances Hibernate connection pool configuration is to connect MySQl as an example.
<! - JDBC Driver - "
<property name="connection.driver_class"> org.gjt.mm.mysql.Driver </ property>
<property name="connection.url"> jdbc: mysql: / / localhost: 3306/struts? useUnicode = true
& CharacterEncoding = GBK </ property>
"! - Database user name -"
<property name="connection.username"> root </ property>
"! - Database password -"
<property name="connection.password"> 8888 </ property>
Section above configuration, in the c3p0 and dbcp in, are necessary because the hibernate configuration based on the above to generate connections, re-dbcp to c3p0 or management. However, proxool can not, you can let proxool to generate their own connections, which in the following repeat that.
Online google a bit and found that there are basically three kinds of solutions can be implemented:
1, using hibernate native connection pool;
2, using c3po connection pool package functions;
3, using the dbcp connection pooling feature package;
4, using Proxool package connection pooling function;
The first program, Hibernate default connection pool, that is, myeclipse generated hibernate.cfg.xml add an attribute:
<property name="connection.pool_size"> 20 </ property>
The second program is: C3P0
Just add the hibernate.cfg.xml
<property name="c3p0.min_size"> 2 </ property>
<property name="c3p0.max_size"> 10 </ property>
<property name="c3p0.timeout"> 1800 </ property>
<property name="c3p0.acquireRetryAttempts"> 4 </ property>
<property name="c3p0.acquireIncrement"> 1 </ property>
<property name="c3p0.idleConnectionTestPeriod"> 36000 </ property>
<property name="c3p0.initialPoolSize"> 2 </ property>
<property name="c3p0.maxPoolSize"> 10 </ property>
<property name="c3p0.maxIdleTime"> 1200 </ property>
<property name="c3p0.maxStatements"> 30 </ property>
<property name="c3p0.minPoolSize"> 2 </ property>
<property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </ property>
There are in classespath added c3p0-0.8.4.5.jar
The third in the program are: dbcp
Adding in the hibernate.cfg.xml
<property name="dbcp.maxActive"> 100 </ property>
<property name="dbcp.whenExhaustedAction"> 1 </ property>
<property name="dbcp.maxWait"> 60000 </ property>
<property name="dbcp.maxIdle"> 10 </ property>
<property name="dbcp.ps.maxActive"> 100 </ property>
<property name="dbcp.ps.whenExhaustedAction"> 1 </ property>
<property name="dbcp.ps.maxWait"> 60000 </ property>
<property name="dbcp.ps.maxIdle"> 10 </ property>
There are in classespath adding commons-pool-1.2.jar and commons-dbcp-1.2.1.jar.
The fourth in the program are: Proxool
The establishment of a Proxool.xml file:
<? xml version = "1.0" encoding = "UTF-8"?>
<! - The proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. ->
jdbc: mysql: / / localhost: 3306/jackdemo
<property name="user" value="root" />
<property name="password" value="jack" />
Now look at the configuration of meaning:
<alias> to configure connection pool alias;
<driver-url> and writing JDBC connect to the database when the same URL.
<driver-class> and writing JDBC connect to the database the same as when the Driver.
<driver-properies> that connect to the database when the user name and password.
<house-keeping-sleep-time> proxool automatically detect connection state at all time intervals (ms), detected idle connection is immediately recovered, the destruction of overtime
<prototype-count> at a minimum the number of idle connections
<maximum-connection-count> maximum number of connections.
<minimum-connection-count> the smallest number of connections.
The following is the Hibernate configuration file for connection pool configuration:
<? xml version = '1 .0 'encoding =' UTF-8 '?>
<! DOCTYPE hibernate-configuration PUBLIC
"- / / Hibernate / Hibernate Configuration DTD 3.0 / / EN"
<! - Proxool connection pool load of classes - "
"! - Connection pool alias, that is, when you configure the connection pool from an alias -"
"! - Connection pool file addresses -"
config / proxool / proxool.xml
"! - Whether run-time generated SQL output to the log for debugging -"
<property name="show_sql"> true </ property>
<mapping resource="com/jack/ssh/demo/bo/City.hbm.xml" />
<mapping resource="com/jack/ssh/demo/bo/Province.hbm.xml" />
Five . JNDI connection pool. Data source has been configured by the application services (such as the Web server), Hibernate needs to be done only through this data source JNDI name lookup. Application server connection pool outside appears as a data source JNDI binding, it is an instance of the class javax.jdbc.Datasource. As long as a Hibernate configuration file, such as:
hibernate.connection.datasource = java: / comp / env / jdbc / portal / / JNDI name of
hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_loopup_class = org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.MySQLDialect