当前位置:操作系统 > Unix/Linux >>

数据库连接池与非连接池效率的直观对比

如果网络状况复杂(每次连接耗时更长)的情况下,使用连接池的优势将更加明显。
运行结果:
 
运行100次,共耗费109毫秒
非池运行100次,共耗费4422毫秒
 
运行200次,共耗费141毫秒
非池运行200次,共耗费8031毫秒
 
运行300次,共耗费219毫秒
非池运行300次,共耗费11812毫秒
 
 
 
测试代码:
 
 
public class Test { 
 
    /** 
     * @param args 
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        try{ 
            Test.poolConnection(300); 
            Test.connectionTest(300); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
         
    } 
     
    public static void poolConnection(int count)throws Exception{ 
        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource(); 
        ds.setDriverType("thin"); 
        ds.setServerName("127.0.0.1"); 
        ds.setPortNumber(1521); 
        ds.setNetworkProtocol("tcp"); 
        ds.setDatabaseName("DEVELOPER"); 
        ds.setUser("ysf"); 
        ds.setPassword("ysf123"); 
         
        PooledConnection pc = ds.getPooledConnection(); 
        long bt = System.currentTimeMillis(); 
        for(int i=0; i < count; i++){ 
            Connection con = pc.getConnection(); 
             
            Statement st = con.createStatement(); 
            ResultSet rs = st.executeQuery("SELECT sysdate from dual"); 
             
            /*while(rs.next()){ 
                System.out.println(rs.getString("sysdate")); 
            }*/ 
             
            rs.close(); 
            st.close(); 
            con.close(); 
        } 
        long et = System.currentTimeMillis(); 
        System.out.println("运行"+Integer.toString(count)+"次,共耗费"+Long.toString(et-bt)+"毫秒"); 
 
        pc.close(); 
        ds.close(); 
    } 
     
    public static void connectionTest(int count)throws Exception{ 
         
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
        long bt = System.currentTimeMillis(); 
        for(int i=0; i < count; i++ ){ 
            Connection con = DriverManager.getConnection( 
                    "jdbc:oracle:thin:@localhost:1521:DEVELOPER","ysf","ysf123"); 
             
            Statement st = con.createStatement(); 
            ResultSet rs = st.executeQuery("SELECT sysdate from dual"); 
             
            /*while(rs.next()){ 
                System.out.println(rs.getString("sysdate")); 
            }*/ 
             
            rs.close(); 
            st.close(); 
            con.close(); 
        } 
        long et = System.currentTimeMillis(); 
        System.out.println("非池运行"+Integer.toString(count)+"次,共耗费"+Long.toString(et-bt)+"毫秒"); 
    } 

 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,