Java manages database connection pooling with C3P0

Source: Internet
Author: User
Tags connection pooling

Database connection pool class for getting a database connection. Use the singleton mode to ensure that all connections are managed through only one connection pool.

 PackageCom.mousewheel.dbcon;ImportJava.io.InputStream;Importjava.sql.Connection;Importjava.sql.SQLException;Importjava.util.Properties;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classDbpool {Private StaticDbpool instance; PrivateCombopooleddatasource DataSource; Static{instance=NewDbpool (); }    PrivateDbpool () {Try{DataSource=NewCombopooleddatasource (); Properties prop=NewProperties (); InputStream in= Dbpool.class. getClassLoader (). getResourceAsStream ("Db.properties");            Prop.load (in); Datasource.setdriverclass (Prop.getproperty ("Jdbcdriver")); Datasource.setjdbcurl (Prop.getproperty ("url")); Datasource.setuser (Prop.getproperty ("Username")); Datasource.setpassword (Prop.getproperty ("Password")); } Catch(Exception e) {e.printstacktrace (); }    }         Public StaticDbpool getinstance () {returninstance; }         PublicConnection getconnection ()throwsSQLException {returndatasource.getconnection (); }}

Dbutil class, Packaging dbpool, simplified use

 Package Com.mousewheel.dbcon; Import java.sql.Connection; Import java.sql.SQLException;  Public class Dbutil {    publicstaticthrows  SQLException {        =  Dbpool.getinstance ();         return pool.getconnection ();    }}

Test the code and compare it to the case where the connection pool is not used

 PackageCom.mousewheel.dbcon;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.util.Properties;ImportJava.io.InputStream;ImportCom.mousewheel.dbcon.DbUtil;classApp { Public Static voidMain (string[] args) {//Use c3p0 Connection pool        Try {             for(inti = 0; I < 100; i++) {                LongBeginTime =System.currenttimemillis (); Connection Con=dbutil.getconnection (); //Execute Query StatementString sql = "SELECT * FROM Student"; PreparedStatement PS=con.preparestatement (SQL); ResultSet RS=Ps.executequery ();  while(Rs.next ()) {//System.out.println (rs.getstring (1));} con.close (); LongEndTime =System.currenttimemillis (); System.out.println (String.Format ("%s times,%s", I, (EndTime-( beginTime))); }        } Catch(Exception e) {e.printstacktrace (); }        //jdbc without using conection pool        Try {             for(inti = 0; I < 100; i++) {                LongBeginTime =System.currenttimemillis (); Properties prop=NewProperties (); InputStream in= App.class. getClassLoader (). getResourceAsStream ("Db.properties");                Prop.load (in); Class.forName (Prop.getproperty ("Jdbcdriver")); Connection Con= Drivermanager.getconnection (prop.getproperty ("url"), Prop.getproperty ("username"), Prop.getproperty ("Password")); String SQL= "SELECT * FROM Student"; PreparedStatement PS=con.preparestatement (SQL); ResultSet RS=Ps.executequery ();  while(Rs.next ()) {//System.out.println (rs.getstring (1));} con.close (); LongEndTime =System.currenttimemillis (); System.out.println (String.Format ("%s times,%s", I, (EndTime-( beginTime))); }        } Catch(Exception e) {e.printstacktrace (); }    }}

Java manages database connection pooling with 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.