使用数据库链接池链接数据库

数据库链接池是什么?
java

数据库链接池(Connection pooling)是程序启动时创建足够的数据库链接,并将这些链接组成一个链接池,由程序动态地对池中的链接进行申请,使用,释放。mysql

在对数据库进行操做以前都要先获取数据库链接,而后才能向后进行操做,增删改查,获取结果集,浪费时间的地方就是在获取数据库链接上,以往每次操做的时候,先获取链接,操做完以后关掉链接,这么一次一次,时间都浪费在获取链接上了,咱们须要的就是一个数据库链接池,先建立好必定数量的链接放在池子里,当咱们要用的时候,去池子中去找可用的链接,当用完以后,再把链接放回池子中,这样咱们用的链接都是池子负责管理的,每次用完以后只要放回去,这样相比于以前的获取到链接,用完关闭速度很快,只是把链接拿来不用每次都建立,关闭,能够省不少的时间。sql

使用c3p0能够很轻松地构建数据库链接池,是一个开源的项目,https://sourceforge.net/projects/c3p0/files/?source=navbar下载网址,一个压缩包中包含jar包和例子,说明数据库

须要的jar包,导入咱们的项目中就能使用了post

使用单例模式(饿汉式)构建数据库链接池,测试

    private static ComboPooledDataSource dataSource;
    private static ConnectionManager connectionManager = new ConnectionManager();
    private ConnectionManager(){
        try {
            //ComboPooledDataSource cpds = new ComboPooledDataSource();
            // cpds.setDriverClass(“org.postgresql.Driver”);
            // 加载jdbc驱动程序cpds.setJdbcUrl(“jdbc:postgresql:localhost / testdb”);
            // cpds.setUser( “swaldman”);
            // cpds.setPassword( “测试密码”);
            // 下面的设置是可选的 -  c3p0能够使用默认值
            // cpds.setMinPoolSize(5);
            // cpds.setAcquireIncrement(5);
            // cpds.setMaxPoolSize(20); // DataSource cpds如今是一个彻底配置和可用的聚集数据源...
            dataSource = new ComboPooledDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("root");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mail");
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setInitialPoolSize(5); //初始化链接数
            dataSource.setMinPoolSize(1);//最小链接数
            dataSource.setMaxPoolSize(20);//最大链接数
            dataSource.setMaxStatements(50);//最长等待时间
            dataSource.setMaxIdleTime(60);//最大空闲时间,单位毫秒
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public static ConnectionManager getInstance(){
        return connectionManager;
    }
    public synchronized Connection getConnection() {
        Connection conn = null;
        try {
             conn=dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

在下载的说明文档中也介绍了使用方法

在main方法中测试,输出两种方法所用的时间
ui

public static void main(String[] args) throws SQLException {
        for(int i=0;i<50;i++) {
            long begin = System.currentTimeMillis();
            Connection conn = ConnectionManager.getInstance().getConnection();
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER WHERE id=2");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {

            }
            conn.close();
            ps.close();
            rs.close();
            long end = System.currentTimeMillis();
            System.out.print(end-begin+"  ");
        }
        System.out.println("-------------");
        for(int i=0;i<50;i++) {
            long begin = System.currentTimeMillis();
            Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/mail";
                String username = "root";
                String password = "root";
                conn = DriverManager.getConnection(url, username, password);
                PreparedStatement ps = conn.prepareStatement("SELECT * FROM USER WHERE id=2");
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {

                }
                conn.close();
                ps.close();
                rs.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.print(end - begin+" ");
        }
    }

输出:

利用数据库链接池第一次使用耗时时间长,之后平均用时均为1ms,不使用链接池的代码平均都在15ms左右url

参考:http://blog.csdn.net/wenwen091100304/article/details/48035003
spa

相关文章
相关标签/搜索