C3P0 Configuration Combat _jndi

Source: Internet
Author: User
Tags connection pooling

C3P0: An open-source JDBC Connection pool that implements data source and Jndi bindings, and supports standard extensions for JDBC3 specifications and JDBC2. The open source project that uses it now has hibernate,spring etc.

By default (that is, no connection pooling is configured), Hibernate uses a built-in connection pool. However, this connection pool performance is not good, so the official is only recommended in the development environment to use. Hibernate supports a third party connection pool, and the official recommended connection pool is c3p0,proxool.

Here's why you must use C3P0:

1) Hibernate official website recommended

2 solve the problem that Tomcat can regain data connection without reboot after database reboot.

Download: http://www.mchange.com/projects/c3p0/index.html

Here say how to configure, the Internet actually has a lot of, I just according to the project needs to tidy up a bit, altogether 3 kinds, everybody according to need to configure.

One. JNDI (Project is more common, simplest, not associated with code)

1 introduce the latest version of the C3P0 Jar pack in Tomcat or project (I'm using C3p0-0.9.2.1.jar)

If the startup times class is not found: caused By:java.lang.noclassdeffounderror:com/mchange/v2/ser/indirector,

You will need to join Mchange-commons-java-0.2.3.4.jar.

2 Modify the data source configuration in Tomcat as follows: XML code    <resource name= "Jdbc/dbsource"                   type= " Com.mchange.v2.c3p0.ComboPooledDataSource "                  maxpoolsize= " minpoolsize=" "5"  acquireincrement= "2"    Initialpoolsize= "  maxidletime="                 factory= "Org.apache.naming.factory.BeanFactory"                   user= "xxxx"  password= "xxxx"                   driverclass= " Oracle.jdbc.driver.OracleDriver "                  jdbcurl= "JDBC:ORACLE:THIN:@192.168.X.X:1521:ORCL"                  Idleconnectiontestperiod= "Ten"  />   

Parameter description:

(1) Idleconnectiontestperiod

After the database has been restarted or, for some reason, the process has been killed, C3P0 does not automatically reinitialize the database connection pool, and when a new request needs to access the database, it will report an error (because the connection fails), flush the database connection pool, discard the failed connection, and return to normal when the second request arrives.

C3P0 currently does not provide a parameter that takes the number of retries after a failed connection has been made, and only the parameter (acquireretryattempts "default 30") for the number of retries after the new connection fails.

To resolve this issue, you can set the compromise resolution by setting the Idleconnectiontestperiod "default of 0, which means no check" parameter, which is to set a frequency parameter for the system to automatically check whether the connection is normal in the connection pool, and the time unit is seconds.

(2) Acquireincrement

The number of connections that are c3p0 at a time when the connection in the connection pool is depleted, that is, if the number of connections used has reached maxpoolsize,c3p0, a new connection is established immediately.

(3) MaxIdleTime

In addition, C3P0 will not close the unused connection pool by default, but will recycle it into the available connection pool, which will result in a larger number of connections, so you need to set MaxIdleTime "default 0, which means never expire", in seconds. MaxIdleTime represents the maximum time that a idle state's connection can survive.

3 The normal introduction of this data source in the project, the code does not need any modification

Two. Hibernate (spring+hibernate): It is not recommended that the first and third methods can be used instead.

1 The introduction of C3P0 in the project Jar

2 Modify the configuration in Hibernate (I am using spring+hibernate), as follows: XML code    <bean id= "Sessionfactory"            class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean ">            <property name= "DataSource"  ref= "DataSource"/>            <property name= "Mappinglocations"  value= "${ Hibernate.mapping.locations} " />           < Property name= "Hibernateproperties" >                <props>                    <prop key= "Hibernate.dialect" >                        ${hibernate.dialect}                    </prop>                    <prop key= "Hibernate.show_sql" >                        ${ hibernate.show_sql}                    </prop>                    <prop key= "Hibernate.use_sql_comments" >                        ${ hibernate.use_sql_comments}                    </prop>                                        <prop key= "Hibernate.connection.provider_class" >                         org.hibernate.connection.c3p0connectionprovider                    </prop>                    <!--Connection Pool Minimum number of connections  -->                    <prop key= " Hibernate.c3p0.min_size ">                        5                    </prop>                    <!--Maximum number of connections  -->                    <prop key= "Hibernate.c3p0.max_size" >                         50                    </prop>                    <!--Connection Timeout  -->                    <prop key= "Hibernate.c3p0.timeout" >    &NBSP;&NBSP;&Nbsp;                 120                     </prop>                    <!--statemnets Cache size  -->                    <prop key= "Hibernate.c3p0.max_statements" >                         100                    </prop>                    <!--every few seconds to detect the normal use of the connection  -->                    <prop key= "Hibernate.c3p0.idle_test_period" >                         120                    </prop>                    <!--A one-time increase in the number of connections when the connection is depleted in the pool, defaults to 3 -->                    <prop key= " Hibernate.c3p0.acquire_increment ">                        2                    </prop>                    <!--  Verify that the connection is available every time  -->                     <prop key= " Hibernate.c3p0.validate ">                        true                    </prop>                </props>            </property>       </bean>          

3 The DataSource here can be jndi.

Three. DataSource (Spring, common development)

If you are using spring and you do not use Jndi in your project, and you do not want to configure it to hibernate, you can configure c3p0 directly into DataSource, as follows: XML code    <bean id= "   DataSource " class=" Com.mchange.v2.c3p0.ComboPooledDataSource " destroy-method=" Close ">   <property name= "Driverclass" ><value>oracle.jdbc.driver.oracledriver</value></ property>   <property name= "Jdbcurl" ><value>jdbc:oracle:thin: @localhost: 1521:Test </value></property>   <property name= "User" ><value>Kay</value>< /property>   <property name= "Password" ><value>root</value></property>    <!--The minimum number of connections retained in the connection pool. -->   <property name= "Minpoolsize"  value= "ten"  />   <!-- The maximum number of connections reserved in the connection pool. default: 15 -->   <property name= "maxpoolsize"  value= " /> "   <!--maximum idle time, not used within 1800 seconds, the connection is discarded. If 0, it will never be discarded. default: 0 -->   <property name= "MaxIdleTime"  value= "1800"  />   <!-- C3P0 the number of connections that are fetched at a time when the connection in the connection pool is depleted. default: 3 -->   <property name= "Acquireincrement"  value= "3"  />    <property name= "maxstatements"  value= "1000"  />   <property  Name= "Initialpoolsize"  value= "ten"  />   <!--check for free connections in all connection pools every 60 seconds. default: 0 -->   <property name= "Idleconnectiontestperiod"  value= " " />   <!--defines the number of repeated attempts after a new connection has failed to get from the database. default: 30 -->   <property name= "acquireretryattempts"  value= " /" >   <property name= "Breakafteracquirefailure"  value= "true"  />   < Property name= "Testconnectiononcheckout"  value= "false"  />   </bean>   

Resources:

Parameter description: http://baike.baidu.com/view/920062.htm

Hibernate connection Pool configuration: http://dengjianqiang200.blog.163.com/blog/static/658119201032313017139

The combination of Jndi and c3p0: http://yakar.iteye.com/blog/356243

C3P0 fault-tolerant and automatic re-connection characteristics: HTTP://HI.BAIDU.COM/IVEXRHLAOZBDQTQ/ITEM/AD9572973A5312F2291647B1

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.