Open source database connection pool

Source: Internet
Author: User

 

Open source database connection pool

Generally, the implementation of datasource is called a data source in English. The data source includes the implementation of the database connection pool.

1. DBCP database connection pool

Public classDbmanager {

Private StaticDatasourceDS=Null;

Static{

Inputstream is = dbmanager.Class. Getclassloader (). getresourceasstream ("DBCP. properties ");

Properties P =NewProperties ();

Try{

P. Load (is );

DS= Basicdatasourcefactory.Createdatasource(P); // DBCP Data Source

}Catch(Ioexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}Catch(Exception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}

}

 

Public staticConnection getconnection (){

Connection con =Null;

Try{

Con =DS. Getconnection ();

}Catch(Sqlexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}

ReturnCon;

}

Public static voidRelease (connection con, statement St, resultset RS ){

If(RS! =Null){

Try{

Rs. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Rs =Null;

}

If(Con! =Null){

Try{

Con. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Con =Null;

}

}

}

DBCP. properties File

# \ U8fde \ u63a5 \ u8bbe \ u7f6e

Driverclassname = com. MySQL. JDBC. Driver

Url =JDBC\:MySQL\://Localhost\: 3306/JDBC

Username = root

Password = 1234

 

# <! -- \ U530d \ u59cb \ u5316 \ u8fde \ u63a5 -->

Initialsize = 5

 

# \ U6700 \ u5927 \ u8fde \ u63a5 \ u6570 \ u91cf

Maxactive = 5

 

# <! -- \ U6700 \ u5927 \ u7a7a \ u95f2 \ u8fde \ u63a5 -->

Maxidle = 20

 

# <! -- \ U6700 \ u5c0f \ u7a7a \ u95f2 \ u8fde \ u63a5 -->

Minidle = 5

 

# <! -- \ Users \ u65f6 \ u7b49 \ u5f85 \ u65f6 \ u95f4 \ u4ee5 \ u6beb \ u79d2 \ Users 60000 \ u6beb \ u79d2/1000 \ u7b49 \ Users \ u79d2

Maxwait = 3000

 

# JDBC \ Alibaba \ u52a8 \ Alibaba \ u7acb \ u8fde \ u63a5 \ u65f6 \ u9644 \ Alibaba \ u7684 \ Alibaba \ u63a5 \ Alibaba \ u6027 \ u5c5e \ u6027 \ u7684 \ u683c \ birthday \ u5fc5 \ u987b \ u4e3a \ u8fd9 \ u6837 \ uff1a [\ u5c5e \ u6027 \ u540d = property;]

# \ Users \ uff1a "user" \ Users "password" \ u4e24 \ u4e2a \ u5c5e \ u6027 \ Users \ u88ab \ u660e \ u786e \ u5730 \ u4f20 \ u9012 \ Users \ u56e0 \ u6b64 \ u8fd9 \ u91cc \ u4e0d \ u9700 \ u8981 \ u5305 \ u542b \ u4ed6 \ u4eec \ u3002

Connectionproperties = useunicode = true; characterencoding = UTF-8

 

# \ Users \ u7531 \ Users \ u63a5 \ u660\ u6240 \ u521b \ Users \ u7684 \ Users \ u63a5 \ u7684 \ u81ea \ u52a8 \ u63d0 \ u4ea4 \ uFF08auto-commit \ uff09 \ u72b6 \ u6001 \ u3002

Defaultautocommit = true

 

# Driver Default \ Users \ u7531 \ u8fde \ u63a5 \ u660\ u6240 \ u521b \ Users \ u7684 \ Users \ u63a5 \ u7684 \ u53ea \ u8bfb \ uFF08read-only \ uff09 \ u72b6 \ u6001 \ u3002

# \ U5982 \ u679c \ u6ca1 \ u6709 \ u8bbe \ Users \ u503c \ Users \ u5219 \ Users \ u201d \ Users \ u6cd5 \ Users \ u88ab \ u8c03 \ u7528 \ u3002 \ uff08 \ u67d0 \ Users \ u9a71 \ u52a8 \ Users \ u652f \ u6301 \ u53ea \ u8bfb \ u6a21 \ u5f0f \ uff0c \ u5982 \ Users \ uff09

Defaultreadonly =

 

# Driver Default \ Users \ u7531 \ u8fde \ u63a5 \ u660\ u6240 \ u521b \ u5efa \ u7684 \ Users \ u63a5 \ u7684 \ u4e8b \ u52a1 \ Users \ uff09 \ u3002

# \ U53ef \ u7528 \ u503c \ u4e3a \ signature \ u5217 \ u4e4b \ u4e00 \ uff1a \ uff08 \ signature \ u60c5 \ u53ef \ signature \ u3002 \ uff09none, expires, expires, serializable

Defaulttransactionisolation = read_uncommitted

 

2. c3p0 database connection pool

Public classDbmanager_c3p0 {

Private StaticCombopooleddatasourceDS=Null;

// Construct the database connection pool object

Static{

DS=NewCombopooleddatasource ("MySQL ");

/* Ds. setdriverclass ("com. MySQL. JDBC. Driver ");

DS. setjdbcurl ("JDBC: mysql ://Localhost: 3306/JDBC");

DS. setuser ("root ");

DS. setpaeeword ("1234 ");

DS. setinitialpoolsize (10 );

DS. setmaxpoolsize (50 );

DS. setminpoolsize (10); // c3p0 data source */

}

Public staticConnection getconnection (){

Connection con =Null;

Try{

Con =DS. Getconnection ();

}Catch(Sqlexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}

ReturnCon;

}

Public static voidRelease (connection con, statement St, resultset RS ){

If(RS! =Null){

Try{

Rs. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Rs =Null;

}

If(Con! =Null){

Try{

Con. Close ();

}Catch(Exception e) {e. printstacktrace ();}

Con =Null;

}

}

}

C3p0_config.xml File

<C3p0-config>

<Default-config>

<Property name ="Driverclass"> Com. MySQL. JDBC. Driver </property>

<Property name ="Jdbcurl"> JDBC: mysql ://Localhost: 3306/JDBC</Property>

<Property name ="User"> Root </property>

<Property name ="Password"& Gt; 1234 </property>

<Property name ="Automatictesttable"> Con_test </property>

<Property name ="Checkouttimeout"& Gt; 30000 </property>

<Property name ="Idleconnectiontestperiod"> 30 </property>

<Property name ="Initialpoolsize"> 10 </property>

<Property name ="Maxidletime"> 30 </property>

<Property name ="Maxpoolsize"& Gt; 100 </property>

<Property name ="Minpoolsize"> 10 </property>

<Property name ="Maxstatements"& Gt; 200 </property>

</Default-config>

<! -- ThisAppIs massive! -->

<Named-config name ="MySQL">

<Property name ="Driverclass"> Com. MySQL. JDBC. Driver </property>

<Property name ="Jdbcurl"> JDBC: mysql ://Localhost: 3306/JDBC</Property>

<Property name ="User"> Root </property>

<Property name ="Password"& Gt; 1234 </property>

<Property name ="Acquireincrement"> 50 </property>

<Property name ="Initialpoolsize"& Gt; 100 </property>

<Property name ="Minpoolsize"> 50 </property>

<Property name ="Maxpoolsize"& Gt; 1000 </property>

</Named-config>

C3p0-config>

 

 

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.