Java uses c3p0 to manage database connection pools and c3p0 database connections

Source: Internet
Author: User

Java uses c3p0 to manage database connection pools and c3p0 database connections

Database Connection Pool class, used to obtain database connections. The Singleton mode ensures that all connections are managed only through one connection pool.

package com.mousewheel.dbcon;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBPool {        private static DBPool instance;    private ComboPooledDataSource dataSource;        static{        instance = new DBPool();    }    private DBPool() {        try {            dataSource = new ComboPooledDataSource();            Properties prop = new Properties();            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 static DBPool getInstance(){        return instance;    }        public Connection getConnection() throws SQLException {        return dataSource.getConnection();    }}

DBUtil class, packaging DBPool, simplified use

package com.mousewheel.dbcon;import java.sql.Connection;import java.sql.SQLException;public class DbUtil {    public static Connection getConnection() throws SQLException {        DBPool pool = DBPool.getInstance();        return pool.getConnection();    }}

 

Test code, compared with the case where the connection pool is not used

Package com. mousewheel. dbcon; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. util. properties; import java. io. inputStream; import com. mousewheel. dbcon. dbUtil; class App {public static void main (String [] args) {// use c3p0 connection pool try {for (int I = 0; I <100; I ++) {long beginTime = System. currentTimeMillis (); Connection con = DbUtil. getConnection (); // execute the query statement String SQL = "select * from student"; PreparedStatement ps = con. prepareStatement (SQL); ResultSet rs = ps.exe cuteQuery (); while (rs. next () {// System. out. println (rs. getString (1);} con. close (); long endTime = 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 (int I = 0; I <100; I ++) {long beginTime = System. currentTimeMillis (); Properties prop = new Properties (); 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.exe cuteQuery (); while (rs. next () {// System. out. println (rs. getString (1);} con. close (); long endTime = System. currentTimeMillis (); System. out. println (String. format ("% s times, % s", I, (endTime-beginTime) ;}} catch (Exception e) {e. printStackTrace ();}}}

 

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.