问题
返回大约500行的简单sql需要:
上述时间排除了获取连接实例。
问题:选项3的性能下降。为什么?如何使其更合理?
设置
测试代码
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.Hashtable; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import oracle.jdbc.OracleConnection; import oracle.jdbc.pool.OracleDataSource; import Facility; import BrowserSQL; public class JDBCTest { public static Connection conn; public static Connection getConnection(){ String url= "localhost:1521:testdb"; String usr="test"; String pswd="test"; Connection conn = null; try { OracleDataSource ods = new OracleDataSource(); ods.setURL("jdbc:oracle:thin:@" + url); ods.setUser(usr); ods.setPassword(pswd); conn = ods.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static Connection getConnectionWeblogicJndi(){ Connection conn = null; try { Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ctx = new InitialContext(ht); DataSource ods = (DataSource)ctx.lookup("jdbc/test"); conn = ods.getConnection(); } catch (SQLException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } return conn; } //get the next val from given sequence. public static void runQuery(Connection conn, String sql){ ResultSet rs=null; Statement stmt=null; try{ stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){} }catch(SQLException ex){ ex.printStackTrace(); } finally{ close(rs,stmt,conn); } } public static void close(ResultSet rs, Statement ps, Connection conn){ if (rs!=null) { try { rs.close(); } catch(SQLException ex) { ex.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException ex) { ex.printStackTrace(); } } if(conn !=null){ try{ conn.close(); }catch(SQLException ex){ ex.printStackTrace(); } } } public static void main(String[]arg) throws SQLException { //Connection conn = JDBCTest.getConnectionWeblogicJndi(); Connection conn = JDBCTest.getConnection(); ((OracleConnection)conn).setDefaultRowPrefetch(1000); long a = GregorianCalendar.getInstance().getTimeInMillis(); System.out.println("Stating testGetAllFacilityByQuery..."+a); runQuery(conn, BrowserSQL.getAllFacilities()); long b = GregorianCalendar.getInstance().getTimeInMillis(); System.out.println("End testGetAllFacilityByQuery..."+b + " -------- total time: "+(b-a)+" ms"); } }