JAVAWEB-C3P0 Connection Pool

Source: Internet
Author: User

The most common connection pooling technology, spring supports C3P0 connection pooling by default.

Core class

1. Combopooleddatasource ds;

Introduced, C3P0 jar package

2. Use a link pool to create a link

A) hard-coded mode

     Public voidTestcode ()throwsexception{//Create connection Pooling tool classCombopooleddatasource DataSource =NewCombopooleddatasource (); //Setting Connection ParametersDatasource.setjdbcurl ("Jdbc:mysql://localhost:3306/learnstruts"); Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setuser ("Root"); Datasource.setpassword ("33269456.cx"); Datasource.setinitialpoolsize (3); Datasource.setmaxpoolsize (6); //get linked objectJava.sql.Connection Connection =datasource.getconnection (); Connection.preparestatement ("DELETE from user WHERE userId = ' 1 '"). Executeupdate (); //CloseConnection.close (); }

b) How XML is configured

C3p0-config.xml

<C3p0-config>  <Default-config>     < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>     < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/learnstruts</ Property>     < Propertyname= "User">Root</ Property>      < Propertyname= "Password">33269456.cx</ Property>     < Propertyname= "Initialpoolsize">5</ Property>      < Propertyname= "Maxpoolsize">10</ Property>   </Default-config><!--<named-config name= "Oracleconfig" > <property name= "Driverclass" >com.mysql.jdbc.driver</ property> <property name= "Jdbcurl" >jdbc:mysql:///day17</property> <property name= "user" >roo t</property> <property name= "password" >root</property> <property name= "Initialpoolsize" > 5</property> <property name= "maxpoolsize" >10</property> </named-config> -</C3p0-config>
@Test      Public void throws exception{        // in this class will automatically load c3p0 config file under src        new  Combopooleddatasource ();         = datasource.getconnection ();        Connection.preparestatement ("DELETE from user WHERE userId = ' 1 '");        Connection.close ();    }

Jdbcutil.jav package com.cx.utils;

ImportJavax.sql.DataSource;ImportOrg.apache.commons.dbutils.QueryRunner;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classJdbcutils {//Initializing the connection pool    Private StaticDataSource DataSource;

Static code blocks load connection pools, load only once
Static { //get the data source from the connection poolDataSource =NewCombopooleddatasource (); } //encapsulate Common Objects Public StaticDataSource Getdatasource () {returnDataSource; } Public StaticQueryrunner Getqueryrunner () {return NewQueryrunner (DataSource); }
}

JAVAWEB-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.