Hibernate 3.* C3P0 Configuration

Source: Internet
Author: User
Tags postgresql time interval

Reprint from http://feicer.javaeye.com/blog/549822

The Hibernate connection pool algorithm is quite immature. It's just to get you started, and it's not suitable for product systems or performance testing. For best performance and stability you should use a third party connection pool. You only need to replace the hibernate.connection.pool_size with the settings for a specific connection pool. This will turn off Hibernate's own connection pool. For example, you might want to use C3P0.
C3P0 is an open source JDBC connection pool that is distributed along with Hibernate, located in the Lib directory. If you set the hibernate.c3p0.*-related properties, Hibernate will use C3p0connectionprovider to cache the JDBC connection. If you prefer to use Proxool, refer to the Hibernate.properties in the release package and get more information from the Hibernate website.

This is a hibernate.properties sample file that uses 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.idle_test_period 3000
#hibernate. C3p0.acquire_increment 2
#hibernate. C3p0.validate false


Add the following configuration to the Hibernate.cfg.xml file:

Maximum connection number of <!---->
<property name= "Hibernate.c3p0.max_size" >20</property>

<!--minimum connection number-->
<property name= "Hibernate.c3p0.min_size" >5</property>

<!--get the timeout for the connection, if more than this time, will throw an exception, the unit millisecond ***-->
<property name= "Hibernate.c3p0.timeout" >120</property>

<!--the largest number of PreparedStatement-->
<property name= "Hibernate.c3p0.max_statements" >100</property>

<!--Check the free connections in the connection pool every 120 seconds, in seconds-->
<property name= "Hibernate.c3p0.idle_test_period" >120</property>

<!--when the connection in the connection pool is exhausted, c3p0 the number of new connections you get-->
<property name= "Hibernate.c3p0.acquire_increment" >2</property>

<!--Verify that the connection is available each time-->
<property name= "Hibernate.c3p0.validate" >true</property>

The complete example is as follows (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

+++++++++++++++++++++++++++++++++++++++++++++++

Reprint from http://flynndang.javaeye.com/blog/423147

Hibernate Connection Pool

Hibernate Connection Pool configuration:
1. Use hibernate to bring the connection pool from.
2. Use a configuration file to specify a Third-party database connection pool, the official recommended connection pool is c3p0,proxool, as well as DBCP.
3. Get the connection pool from the container (for example, Tomcat), which is rarely used, is part of the server's configuration, and is not stated here.

1. The connection pool from the belt

If you do not configure, you are using your own connection pool by default, but this connection pool is not performing well and there are many bugs that are recommended for use only in the development environment. Optional Configuration:

Hibernate.connection.pool_size 5

2. DBCP Connection pool:
DBCP Connection pool due to the existence of major bugs, in the Hibernate3 has been abandoned, so also abandoned in this.

3.c3p Connection pool:
Although the c3p algorithm is not optimal and occupies a large resource, it can be used as the preferred connection pool because of its simple configuration.

Hibernate.connection.provider_class Org.hibernate.connection.C3P0ConnectionProvider
Hibernate.c3p0.max_size 2
Hibernate.c3p0.min_size 2
Hibernate.c3p0.timeout 5000
Hibernate.c3p0.max_statements 100
Hibernate.c3p0.idle_test_period 3000
Hibernate.c3p0.acquire_increment 2
Hibernate.c3p0.validate false

need C3p0-0.8.4.5.jar

4.Proxool Connection pool:
This connection pool has the fewest negative evaluations and the best performance. However, the configuration is rather complex and is recommended for performance optimization.

Hibernate.connection.provider_class Org.hibernate.connection.ProxoolConnectionProvider
Hibernate.proxool.pool_alias Pool1
Hibernate.proxool.xml Proxool.xml

Proxool.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<!--the Proxool configuration can be embedded within your own ' s.
Anything outside the "Proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>pool1</alias>
<!--Proxool can only manage connections generated by itself-->
<driver-url>jdbc:mysql://localhost:3306/struts?useunicode=true&characterencoding=gbk</driver-url >
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<driver-properties>
<property name= "User" value= "root"/>
<property name= "password" value= "8888"/>
</driver-properties>
<!--Proxool automatically detects the time interval (milliseconds) of each connection state, and detects an idle connection and reclaims it immediately, destroying-->
<!--refers to the maximum number of requests waiting in the queue because no idle connections can be allocated, and the user connections that exceed the number of requests are not accepted-->
<maximum-new-connections>20</maximum-new-connections>
<!--minimum number of idle connections maintained-->
<prototype-count>5</prototype-count>
<!--allow the maximum number of connections, more than this connection, and then request, queued in the queue, the largest number of waiting requests determined by maximum-new-connections-->
<maximum-connection-count>100</maximum-connection-count>
<!--minimum connection number-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>

Need Proxool-0.8.3.jar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.