請問一個有關jdbc效能的問題

chinahero發表於2003-09-14
我用二種方法從讀資料(60000條資料的表)
1.
long bTime=System.currTime()..;
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:osn";
String user = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
String sql = "SELECT * from cat" ;
ResultSet rs=stmt.execuQuery(sql);
while(rs.hastnext()){
... //組裝object
}
long eTime=System.currTime..();
System.out.println("cost time="+(eTime-bTime));


2. 用了jdbc2.0中的datasource

DataSource datasource=(Datasource)ic.lookup(...) //透過jndi取Datasource
//讀表兩次,看時間差距
for(int count=0;count<2;count++)
{
long bTime=System.currTime.();
Connection conn=(DataSource)datasource.getConnection();
.. //後面的寫法與1相同

long eTime=System.currTime..();
System.out.println("cost time="+(eTime-bTime));
}

據說第一種寫法,每次查詢前都要例項化一個類,所以效能會慢,jdbc2.0中引進了Datasource這個東東,是不是就是資料池呢?但是經過我的使用,2方法和1方法的時間沒有什麼區別,有時2快,有是1快,感覺1好象快一些.
我是多次執行的,特別是對第2種,datasource初始化好後,多次呼叫讀取表記錄的操作,時間基本一樣,差幾百ms

我的理解是不是有問題,資料池不是DataSource這個東東,是不是要把Connection這個物件包在裡面才算?


相關文章