Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreparedStatement , CallableStatement and Performance Considerations

I have a oracle stored proc that needs to be called from my Java program. I had used CallableStatement to pass parameters to the stored proc. I am using oracle thin driver (configured in web logic server against the relevant jndi entry).This stored proc does not have any OUT values. This stored proc accepts a numeric value and does a lot of updates in the db based on the value received.

I get a connection object and then call this stored proc in loop (20 times for passing 20 numbers). When I directly call this stored proc from an oracle client , the execution gets completed in 2-3 seconds. However the behaviour is not predictable from my java code. Some of the calls take even 30-40 seconds to get completed.

I tried to use PreparedStatement instead of CallableStatement and could see marginal performance improvement (though the behaviour is still inconsistent).

  1. Is it OK in my case to use PreparedStatement instead of CallableStatement given that the storedproc does not have any OUT parameters?
  2. Is there any reason why PreparedStatement has some performance gain over CallableStatement or is it something that I might have observed incorrectly?
  3. Is there a better approach for solving this performance issue?
like image 907
Punter Vicky Avatar asked Jan 05 '12 18:01

Punter Vicky


People also ask

What is PreparedStatement and CallableStatement?

The PreparedStatement is used for executing a precompiled SQL statement. The CallableStatement is an interface which is used to execute SQL stored procedures, cursors, and Functions. So PreparedStatement is faster than Statement.

Do we need to close CallableStatement?

Closing CallableStatement Object A simple call to the close() method will do the job. If you close the Connection object first, it will close the CallableStatement object as well. However, you should always explicitly close the CallableStatement object to ensure proper cleanup.

Which method is used to create a CallableStatement?

The prepareCall method is used to create new CallableStatement objects. As with the prepareStatement method, the SQL statement must be supplied at the time that the CallableStatement object is created. At that time, the SQL statement is precompiled.


1 Answers

From your comment, you have prepareCall inside your loop. An advantage of prepared statements (and callable statements) is that you can prepare it once, and then swap out the values passed in the parameters; there is overhead each time the call is prepared, so if you could bring that outside of your loop, you may find that run time decreases. You may find that turning off AutoCommit also helps, as there is overhead with each commit.

conn.setAutoCommit(false);
CallableStatement stmt = conn.prepareCall(sql);
while(true) {
    stmt.setInt(1, value);
    stmt.execute();
}
conn.commit();
conn.setAutoCommit(true);

(conn.setAutoCommit(true) does commit, but I find it clearer to be explicit).

like image 130
Matt Avatar answered Oct 12 '22 23:10

Matt