JDBC Performance Tuning

JDBC Performance Tuning

1.0 Introduction

Performance is usually considered an issue at the end of a development cycle when it should really be considered from the start. Often, a task called "performance tuning" is done after the coding is complete, and the end user of a program complains about how long it takes the program to complete a particular task.

Java Database Connectivity (JDBC) is used extensively by many Java applications. Getting database connection is very expensive. If your application requires database connections that are repeatedly opened and closed, this can become a significant performance issue. Now let us see how best we can optimize by clearing the bottlenecks and increase the performance.

When the application server accesses the database, it is also one of the bottlenecks to be considered in the performance.

I/O operations (keyboard input) on the system will slower the connection as I/O threads have maximum priority.

Whenever all the database records are to be returned, better way is put all the records in a Vector and return.

The following example method illustrates standard database interaction by accessing all the columns from every row of a specified database table and storing the data from each row into a String[] array and finally putting all the rows in a Vector.

Now start reading JDBC Performance Tuning and pass your comments at the end of this tutorial.

Adding to an array is more efficient than adding each element to a vector.

1.1 Connection Pooling

When WebLogic Application Server (or any Web server like Tomcat) starts, connections from the connection pools are opened and are available to all clients. When a client closes a connection from a connection pool, the connection is returned to the pool and becomes available for other clients; the connection itself is not closed. There is little cost to opening and closing pool connections.

How many connections should you create in the pool? A connection pool can grow and shrink according to configured parameters, between a minimum and a maximum number of connections. The best JDBC Performance Tuning will always be when the connection pool has as many connections as there are concurrent users.

2.0 Auto-Commit

If auto-commit feature is on, every SQL statement is committed as it is executed. If more than one SQL statement is executed by your program, then a small performance increase can be achieved by turning off auto-commit and by committing all transactions at a time.

This batch transaction gives good JDBC performance by reducing commit calls after each statement’s execution.

3.0 Explicitly closing JDBC objects

Oracle JDBC objects such as Connection, ResultSet, PreparedStatement and CallableStatement do not have finalizer methods. If you do not explicitly call the close() method for each of these objects as soon as you are done wit them, your programs are likely to run out of resources.

Close JDBC objects in finally block. In addition of closing, assign null value afterwards, so that it will be immediately eligible for garbage collection. Doing these things will keep your programs nice and tidy.

6 thoughts on “JDBC Performance Tuning

  1. saj

    Hi sir,

    What is the default connection pool size for tomcat and weblogic?
    what is difference between c3p0 and dbcp and proxool ? please tell me i am getting confused.

    Thanks & Regards

Leave a Reply

Your email address will not be published. Required fields are marked *