JWiley
JWiley

Reputation: 3219

Java/Oracle connection performance issues

I am using the following code to connect to an Oracle stored procedure, with an average for the jdbcExecutionTime of 860ms, and the execution plus serialization time of 172ms.

The jdbcExecutionTime concerns me, and I know it can be done much faster, but from what I've read CallableStatement is supposed to be the fastest.

Any hints for improving the performance here?

Also: I'm using Oracle 11 with 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);

Upvotes: 0

Views: 509

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533920

The first time you do this, most of the time will be spent loading the library (which only happens when you use it for the first time) and creating the connection. Once you have an existing connection you should expect around a <10 ms delay (longer than what you are doing)

Upvotes: 1

Related Questions