频道栏目
首页 > 资讯 > 其他综合 > 正文

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

12-02-25        来源:[db:作者]  
收藏   我要投稿
如果网络状况复杂(每次连接耗时更长)的情况下,使用连接池的优势将更加明显。
运行结果:
 
运行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)+"毫秒"); 
    } 

 
相关TAG标签
上一篇:Oracle学习动态性能表
下一篇: CentOS minimal 安装
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站