Java / Oracle连接性能问题

时间:2012-07-05 20:08:01

标签: java performance oracle

我使用以下代码连接到Oracle存储过程,jdbcExecutionTime的平均值为860毫秒,执行加序列化时间为172毫秒。

jdbcExecutionTime关注我,我知道它可以更快地完成,但从我读过的CallableStatement应该是最快的。

这里有什么改善性能的提示?

另外:我正在Oracle 11使用ojdbc14.jar

final long jdbcStartTime1 = System.currentTimeMillis(); 
    final long jdbcEndTime1;
    final long jdbcEndTime2;
    Connection con = null;

    logger.info("Loading properties...SYS TIME:"+System.currentTimeMillis());

    InputStream inputStream = blah.class.getClassLoader().getResourceAsStream("properties/jdbc.properties");
    logger.info("Done loading properties...SYS TIME:"+System.currentTimeMillis());
    try {

        PROPERTIES.load(inputStream); //Load the jdbc properties
        Class.forName(PROPERTIES.getProperty("jdbc.driverClassName")).newInstance(); //Load the oracle driver
        //Set the connection 
        logger.info("Starting JDBC connection...SYS TIME:"+System.currentTimeMillis());
        con = DriverManager.getConnection(PROPERTIES.getProperty("jdbc.url"), PROPERTIES.getProperty("jdbc.username"), PROPERTIES.getProperty("jdbc.password"));
        ((OracleConnection)con).setDefaultRowPrefetch(80);

        logger.info("JDBC connection established...SYS TIME:"+System.currentTimeMillis());

        String jobquery =   "{call PKG_TEST.MY_PACKAGE(?,?,?,?,?,?)}";  
        CallableStatement callStmt = con.prepareCall(jobquery);

        callStmt.setString(1,blah);                     
        callStmt.setBoolean(2, blah2);
        callStmt.registerOutParameter(3, OracleTypes.CURSOR);   
        callStmt.registerOutParameter(4, OracleTypes.CURSOR);   
        callStmt.registerOutParameter(5, OracleTypes.CURSOR);   
        callStmt.registerOutParameter(6, OracleTypes.CURSOR);   

        jdbcEndTime1 = System.currentTimeMillis();

        logger.info("Executing stored procedure...SYS TIME:"+System.currentTimeMillis());
        callStmt.execute();
        logger.info("Stored procedure complete...SYS TIME:"+System.currentTimeMillis());

        logger.info("Storing db cursor objects as result sets...SYS TIME:"+System.currentTimeMillis());
        ResultSet cur1 = (ResultSet)callStmt.getObject(3);
        ResultSet cur2 = (ResultSet)callStmt.getObject(4);
        ResultSet cur3 = (ResultSet)callStmt.getObject(5);
        ResultSet cur4 = (ResultSet)callStmt.getObject(6);
        logger.info("Completed storing cursors...SYS TIME:"+System.currentTimeMillis());

        jdbcEndTime2 = System.currentTimeMillis();

        final long jdbcExecutionTime =  (jdbcEndTime1-jdbcStartTime1);
        final long spEx_ResultSetTime = (jdbcEndTime2-jdbcEndTime1);

1 个答案:

答案 0 :(得分:1)

第一次执行此操作时,大部分时间将用于加载库(仅在首次使用时才会发生)并创建连接。一旦你有一个现有的连接,你应该期望大约10毫秒的延迟(比你正在做的更长)