Simple use of the C3P0 connection pool

Source: Internet
Author: User

First, C3P0 is a connection pooling plugin

Jar Package Required:

  

Use manual configuration:

/*** Manual configuration using C3P0 *@throwspropertyvetoexception *@throwsSQLException*/@Test Public voidFUN1 ()throwspropertyvetoexception, sqlexception{//To create a connection pool objectCombopooleddatasource datasource=NewCombopooleddatasource (); //four-parameter configuration of the poolDatasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setjdbcurl ("Jdbc:mysql://127.0.0.1:3306/demo"); Datasource.setuser ("Guodaxia"); Datasource.setpassword ("961012gz"); //Pool ConfigurationDatasource.setacquireincrement (5); Datasource.setinitialpoolsize (20); Datasource.setminpoolsize (2); Datasource.setmaxpoolsize (50); Connection Con=datasource.getconnection ();        System.out.println (con);    Con.close (); }    

C3P0 is allowed through XML configuration, similar to Hibernate.cfg.xml:

C3P0 Configuration requirements:
File name: Must be called C3p0-config.xml
File location: Must be under SRC

C3p0-config.xml:

<?XML version= "1.0" encoding= "UTF-8"?><C3p0-config>    <!--Default Connection Configuration -    <Default-config>         <!--connecting four parameter configurations -        < Propertyname= "Jdbcurl">Jdbc:mysql://127.0.0.1:3306/demo</ Property>        < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "User">Guodaxia</ Property>        < Propertyname= "Password">961012gz</ Property>        <!--Pool parameter Configuration -        < Propertyname= "Acquireincrement">3</ Property>        < Propertyname= "Initialpoolsize">10</ Property>        < Propertyname= "Minpoolsize">2</ Property>        < Propertyname= "Maxpoolsize">10</ Property>    </Default-config>        <!--specifically connected to Oracle, the analog -    <Name-configname= "Oracle-config">         <!--connecting four parameter configurations -        < Propertyname= "Jdbcurl">Jdbc:mysql://127.0.0.1:3306/demo</ Property>        < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "User">Guodaxia</ Property>        < Propertyname= "Password">961012gz</ Property>        <!--Pool parameter Configuration -        < Propertyname= "Acquireincrement">3</ Property>        < Propertyname= "Initialpoolsize">10</ Property>        < Propertyname= "Minpoolsize">2</ Property>        < Propertyname= "Maxpoolsize">10</ Property>    </Name-config></C3p0-config>

Use the default configuration in the configuration file:

 /**    @throws   SQLException  */  @Test  public  void  fun2 () throws   sqlexception{ /**   */  Combopooleddatasource dataSource  =new          Combopooleddatasource ();        Connection con  =datasource.getconnection ();        System.out.println (con);    Con.close (); }    

The configuration file can be configured with multi-song configuration information, can be manually selected:

Use the specified configuration:

/*** Use named configuration *@throwsSQLException*/@Test Public voidFUN3 ()throwssqlexception{/*** constructor Parameters specify the name of the configuration element of the E-<name-config name= "Oracle-config" >*/Combopooleddatasource DataSource=NewCombopooleddatasource ("Oracle-config"); Connection Con=datasource.getconnection ();        System.out.println (con);    Con.close (); }

JDBC Connection pool after c3p0:

 PackageCn.itcast.jdbc;Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {/** configuration file for Demon person configuration! Ask you to give c3p0-config.     xnl! */    Private StaticCombopooleddatasource datasource=NewCombopooleddatasource (); /*** Use connection pooling to return a connection object *@return     * @throwsSQLException*/     Public StaticConnection getconnection ()throwssqlexception{returndatasource.getconnection (); }        /*** Return Connection pool Object *@return     */     Public StaticDataSource Getdatasource () {returnDataSource; }}

Simple use of the C3P0 connection pool

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.