Database connection pools DBCP and c3p0

Source: Internet
Author: User
Tags connection pooling

Direct code:

   Public classdbcputils {Private StaticDbcputils dbcputils=NULL;PrivateBasicdatasource bds=NULL;PrivateDatasourceconnectionfactory dscf=NULL;Private dbcputils(){if(bds==NULL) bds=NewBasicdatasource ();        Bds.seturl (Dbconsts.url);        Bds.setusername (Dbconsts.username);        Bds.setpassword (Dbconsts.password);        Bds.setdriverclassname (Dbconsts.driverclass); Bds.setmaxactive ( -); Bds.setinitialsize ( -); Bds.setmaxidle ( -); Bds.setminidle (Ten); dscf=NewDatasourceconnectionfactory (BDS); } PublicSynchronizedStaticDbcputilsgetinstance(){if(dbcputils==NULL) dbcputils=NewDbcputils ();returnDbcputils; } PublicConnectiongetconnection() {Connection con=NULL;Try{con= (Connection) dscf.createconnection (); }Catch(SQLException e) {//TODO auto-generated catch blockE.printstacktrace (); }returnCon } Public Static void Main(string[] args) throws SQLException {Connection con=NULL;LongBegin=system.currenttimemillis (); for(intI=0;i<1000000; i++) {con=dbcputils.getinstance (). getconnection ();        Con.close (); }LongEnd=system.currenttimemillis (); System. out. println ("Time-consuming:"+ (End-begin) +"MS"); }}
 Public classc3p0utils {Private StaticC3p0utils dbcputils=NULL;PrivateCombopooleddatasource cpds=NULL;Private c3p0utils(){if(cpds==NULL) {cpds=NewCombopooleddatasource ();        } cpds.setuser (Dbconsts.username);        Cpds.setpassword (Dbconsts.password); Cpds.setjdbcurl (Dbconsts.url);Try{Cpds.setdriverclass (Dbconsts.driverclass); }Catch(Propertyvetoexception e) {//TODO auto-generated catch blockE.printstacktrace (); } cpds.setinitialpoolsize ( -); Cpds.setmaxidletime ( -); Cpds.setmaxpoolsize ( -); Cpds.setminpoolsize (Ten); } PublicSynchronizedStaticC3p0utilsgetinstance(){if(dbcputils==NULL) dbcputils=NewC3p0utils ();returnDbcputils; } PublicConnectiongetconnection() {Connection con=NULL;Try{con=cpds.getconnection (); }Catch(SQLException e) {//TODO auto-generated catch blockE.printstacktrace (); }returnCon } Public Static void Main(string[] args) throws SQLException {Connection con=NULL;LongBegin=system.currenttimemillis (); for(intI=0;i<1000000; i++) {con=c3p0utils.getinstance (). getconnection ();        Con.close (); }LongEnd=system.currenttimemillis (); System. out. println ("Time-consuming:"+ (End-begin) +"MS"); }}

In spring:

    <!--configuring DBCP data Sources --      <bean id="DataSource2" destroy-method="Close" class= "Org.apache.commons.dbcp.BasicDataSource">        < property name="Driverclassname" value="${ Jdbc.driverclassname} "/>        < property name="url" value="${jdbc.url}"/>         < property name="username" value="${jdbc.username}"/>        < property name="password" value="${jdbc.password}"/>         <!--The number of connections created when the pool is started-        < property name="InitialSize" value="5"/>        <!--The maximum number of connections that can be allocated from the pool at the same time. When set to 0, it means no limit. -        < property name="maxactive" value="/> "        <!--The maximum number of idle connections that will not be released in the pool. When set to 0, it means no limit. -        < property name="Maxidle" value="/> "        <!--The minimum number of connections that remain idle in the pool without creating a new connection. -        < property name="Minidle" value="3"/>        <!--Set auto-recycle timeout connection --          < property name="removeabandoned" value="true" />        <!--auto-reclaim time-out (in seconds)--          <property Name="Removeabandonedtimeout" value="$"/ >        <!--Set the time-out error for print connection when auto-recycle timeout connection         < property name="logabandoned" value="true"/>        <!--wait timeout in milliseconds, the maximum time the pool waits for a connection to be reclaimed (when no connection is available) before throwing an exception.  Set to 1 to indicate an infinite wait. -          < property name="maxwait" value="/> "        </Bean>    <!--configuring C3P0 data Sources --    <bean id= "dataSource" class=" Com.mchange.v2.c3p0.ComboPooledDataSource " destroy-method=" Close ">        < property name="Jdbcurl" value="${jdbc.url}" />         < property name="Driverclass" value="${ Jdbc.driverclassname} " />        < property name="user" value="${jdbc.username}" />        < property name="password" value="${jdbc.password}" />         <!--The maximum number of connections that are kept in the connection pool. Default:15 --        < property name="Maxpoolsize" value=" />"         <!--The minimum number of connections that are kept in the connection pool. -        < property name="Minpoolsize" value="1" />        <!--The number of connections obtained when initializing, the value should be between Minpoolsize and Maxpoolsize. Default:3 --        < property name="Initialpoolsize" value="Ten" />        <!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. default:0 --        < property name="MaxIdleTime" value=" />"         <!--c3p0 The number of connections that are fetched at the same time when the connection in the connection pool is exhausted. Default:3 --        < property name="Acquireincrement" value="5" />        <!--The standard parameters of JDBC to control the number of preparedstatements loaded within the data source. However, because the pre-cached statements belong to a single connection instead of the entire connection pool.          So setting this parameter takes into account a variety of factors. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. Default:0-->        < property name="maxstatements" value="0" />        <!--Check for idle connections in all connection pools every 60 seconds. default:0 --        < property name="Idleconnectiontestperiod" value=" />"         <!--defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30 --        < property name="acquireretryattempts" value=" />"         <!--getting a connection failure will cause any thread that waits for the connection pool to get the connection to throw an exception. However, the data source is still valid and continues to try to get the connection the next time you call Getconnection (). If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. Default:false-->        < property name="Breakafteracquirefailure" value="true" />         <!--use it only when you need it, due to its high performance consumption. If set to true then the validity of each connection submission is officer. We recommend using methods such as Idleconnectiontestperiod or automatictesttable to improve the performance of your connectivity tests. Default:false --        < property name="Testconnectiononcheckout"value="false" />              </Bean>

Related website:
DBCP Baidu Encyclopedia
DBCP Official Website Tutorial
C3P0 Baidu Encyclopedia
C3P0 Official Website Tutorial

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Database connection pooling dbcp and c3p0

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.