Database Connection Pool--C3P0 implementation

Source: Internet
Author: User

1.C3P0 Introduction

C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. The open source projects that currently use it are hibernate,spring and so on.

The core class of the C3P0 database connection pool is Combopooleddatasource

Use C3P0 to import jar packages in the project

C3p0-0.9.2-pre5\lib There are three jar packages, if you use MySQL database, just import c3p0-0.9.2-pre5.jar,mchange-commons-java-0.2.3 two packages, Another c3p0-oracle-thin-extras-0.9.2-pre5.jar is available for use by Oracle databases.

2. Code implementation

Mode one, similar to the DBCP code implementation, configures the properties of the database connection pool using Combopooleddatasource's own API implementation.

Import Java.sql.*;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3p0utils {//1 prepare a connection pool private static Combopooleddatasource DataSource = new Combopooleddatasource () static{//Set parameter try {//1.1 Database connection Basic 4 items Datasource.setdriverclass ("Com.mysql.jdbc.Driver");d Atasource.setjdbcurl ("jdbc:mysql://localhost:3306 /day17 ");d atasource.setuser (" root ");d Atasource.setpassword (" Fujianyi ");//1.2 connection pool features//* Initialization number datasource.setinitialpoolsize (10);//* Maximum number datasource.setmaxpoolsize (20);//* Minimum number datasource.setminpoolsize (5) ;//* Each increment datasource.setacquireincrement (3);} catch (Exception e) {throw new RuntimeException (e);}} public static Connection getconnection () throws Sqlexception{return datasource.getconnection ();}}

Mode two, set the database connection pool using the C3P0 profile c3p0-config.xml, <named-config name= "XXX" in the configuration file when creating the Combopooleddatasource object > The value of the Name property is passed in as a string parameter, and the configuration file is automatically loaded, and the configuration file needs to be placed under the classpath path, the src path.

Import Java.sql.connection;import Java.sql.sqlexception;import Com.mchange.v2.c3p0.combopooleddatasource;public Class C3p0utils2 {private static Combopooleddatasource Datasource;static{datasource = new Combopooleddatasource ("XXX") ;} public static Connection getconnection () throws Sqlexception{return datasource.getconnection ();}}

About configuration Files

The first name of the configuration file must be C3p0-config.xml, and must be placed under the CLASSPATH path, the format and contents are as follows.

<c3p0-config><!--Default configuration, use this configuration if not specified--><default-config><property name= "Checkouttimeout" > 30000</property><property name= "Idleconnectiontestperiod" >30</property><property name= " Initialpoolsize ">10</property><property name=" MaxIdleTime ">30</property><property name=" Maxpoolsize ">100</property><property name=" minpoolsize ">10</property><property name=" Maxstatements ">200</property><user-overrides user=" Test-user "><property name=" MaxPoolSize "> 10</property><property name= "minpoolsize" >1</property><property name= "maxStatements" >0 </property></user-overrides></default-config> <!---named configuration--><named-config name= "XXX" ><property name= "Driverclass" >com.mysql.jdbc.driver</property><property name= "JdbcUrl" >jdbc : Mysql://127.0.0.1:3306/day17</property><property name= "user" >root</property><property name ="Password" >fujianyi</property> <!--how many--><property name= "acquireincrement" >5 if the pool has insufficient data connectivity </property><property name= "initialpoolsize" >20</property><property name= "MinPoolSize" >10 </property><property name= "maxpoolsize" >40</property><property name= "maxStatements" >0 </property><property name= "Maxstatementsperconnection" >5</property></named-config></  C3p0-config>

  

Database Connection Pool--C3P0 implementation

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.