从Weblogic控制的数据源在Oracle连接下执行缓慢的sql

时间:2012-03-28 22:00:54

标签: jdbc oracle11g weblogic-10.x

问题

返回大约500行的简单sql需要:

  1. 在Toad中40毫秒(这会返回前500行)
  2. 500-600 ms在Java中,从一个实例获得连接 oracle.jdbc.pool.OracleDataSource类。
  3. 2100-2500 ms in Java,从数据中获取连接 通过JNDI
  4. 查找在Weblogic中配置的源代码

    上述时间排除了获取连接实例。

    问题:选项3的性能下降。为什么?如何使其更合理?

    设置

    1. Weblogic 10.3.5
    2. Oracle 11g RDMBS
    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");
              }
          }
      
      

0 个答案:

没有答案