• java 数据库连接池和普通连接 对比


    自由自在,闲话省略

    包:c3p0-***.jar

    mchange-commons-java-**.jar

    ojdba.jar

    mysql-connector.jaava-**bin.jar

    ConnectionManager

    package com.adao.c3p0;
    
    import java.beans.PropertyVetoException;
    import java.sql.Connection;
    import java.sql.SQLException;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public final class ConnectionManager {
        //使用单利模式创建数据库连接池
        private static ConnectionManager instance;
        private static ComboPooledDataSource dataSource;
      
        private ConnectionManager() throws SQLException, PropertyVetoException {
            dataSource = new ComboPooledDataSource();
      
            dataSource.setUser("coll_platform");     //用户名
            dataSource.setPassword("coll_platform"); //密码
            dataSource.setJdbcUrl("jdbc:oracle:thin:@192.168.127.156:1521:ORCL");//数据库地址
            dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource.setInitialPoolSize(5); //初始化连接数
            dataSource.setMinPoolSize(1);//最小连接数
            dataSource.setMaxPoolSize(10);//最大连接数
            dataSource.setMaxStatements(50);//最长等待时间
            dataSource.setMaxIdleTime(60);//最大空闲时间,单位毫秒
        }
      
        public static final ConnectionManager getInstance() {
            if (instance == null) {
                try {
                    instance = new ConnectionManager();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return instance;
        }
      
        public synchronized final Connection getConnection() {
            Connection conn = null;
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    }

    ConnectionDemo

    package com.adao.c3p0;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
      
    public class ConnectionDemo {
      
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            System.out.println("使用连接池................................");
            for (int i = 0; i < 20; i++) {
                long beginTime = System.currentTimeMillis();
                Connection conn = ConnectionManager.getInstance().getConnection();
                try {
                    PreparedStatement pstmt = conn.prepareStatement("select * from T_TERMINAL");
                    ResultSet rs = pstmt.executeQuery();
                    while (rs.next()) {
                         // do nothing...
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
      
                long endTime = System.currentTimeMillis();
                System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
            }
            Connection con = null;// 创建一个数据库连接
            System.out.println("不使用连接池................................");
            for (int i = 0; i < 20; i++) {
                long beginTime = System.currentTimeMillis();
                //oracle
                Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
                String url = "jdbc:oracle:thin:@192.168.127.156:1521:ORCL";
                String user = "coll_platform";
                String password = "coll_platform";
                con = DriverManager.getConnection(url, user, password);// 获取连接
    
    
                // mysql
                // MysqlDataSource mds = new MysqlDataSource();
                // mds.setURL("jdbc:mysql://localhost:3306/zww");
                // mds.setUser("root");
                // mds.setPassword("123456");
                // Connection conn = mds.getConnection();
                try {
                    PreparedStatement pstmt = con.prepareStatement("select * from T_TERMINAL");
                    ResultSet rs = pstmt.executeQuery();
                    while (rs.next()) {
                                        // do nothing...
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                long endTime = System.currentTimeMillis();
                System.out.println("第" + (i + 1) + "次执行花费时间为:"
                                    + (endTime - beginTime));
            }
      
        }
    }

    执行结果

    flawless

  • 相关阅读:
    单精度和双精度
    @Transactional注解用法
    JPA No EntityManager with actual transaction available for current thread
    上传文件Request Entity Too Large解决办法
    PG数据库查看当前会话和结束会话
    Chrome浏览器记不住密码也不提示保存密码win10
    ARM平台VMP保护开发入门
    关于我
    HDU7072:Boring data structure problem——题解
    HDU7067:Just another board game——题解
  • 原文地址:https://www.cnblogs.com/adao21/p/13223396.html
Copyright © 2020-2023  润新知