From: http://blog.csdn.net/fenglibing/article/details/4100272
Hibernate built-in connection pool Algorithm Relatively immature. It is only intended to help you get started quickly and is not suitable for product systems or performance tests. For the best performance and stability, you should use a third-party connection pool. You only need to replace hibernate. Connection. pool_size with the settings of a specific connection pool. This will disable the connection pool that comes with hibernate. For example, you may want to use c3p0.
C3p0 is an open source JDBC connection pool that is distributed along with hibernate. It is located under the lib directory. If you set hibernate. c3p0. *, Hibernate uses c3p0connectionprovider to cache JDBC connections. If you prefer proxool, refer to hibernate. properties in the release package and go to the hibernate website for more information.
This is a sample hibernate. properties file using c3p0 (from the etc directory in the hibernate package ):
###########################
### C3p0 connection pool ###
###########################
# Hibernate. c3p0. max_size 2
# Hibernate. c3p0. min_size 2
# Hibernate. c3p0. Timeout 5000
# Hibernate. c3p0. max_statements 100
# Hibernate. c3p0. ID le_test_period 3000
# Hibernate. c3p0. acquire_increment 2
# Hibernate. c3p0. Validate false
Add the following configuration to the hibernate. cfg. xml file:
<! -- Maximum number of connections -->
<Property name = "hibernate. c3p0. max_size"> 20 </property>
<! -- Minimum connections -->
<Property name = "hibernate. c3p0. min_size"> 5 </property>
<! -- Get the connection timeout time. If the timeout value exceeds this time, an exception is thrown, in milliseconds. -->
<Property name = "hibernate. c3p0. Timeout"> 120 </property>
<! -- Maximum number of preparedstatement -->
<Property name = "hibernate. c3p0. max_statements"> 100 </property>
<! -- Check idle connections in the connection pool every 120 seconds. Unit: seconds -->
<Property name = "hibernate. c3p0. idle_test_period"> 120 </property>
<! -- When the connections in the connection pool are used up, c3p0 obtains the new connections -->
<Property name = "hibernate. c3p0. acquire_increment"> 2 </property>
<! -- Verify whether the connection is available every time -->
<Property name = "hibernate. c3p0. Validate"> true </property>
Complete example (hibernate. properties ):
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
Note: In addition to its own connection pool and C3PO connection pool, Hibernate can also use the connection pool function of the DBCP package (although it is said that it is not recommended in hibernate3, it has bugs) use the connection pool function of the proxool package.