Siva V
Siva V

Reputation: 59

Will Open jdbc connections to a database server slow down the performance of an application?

I have an automation script to test database which creates connection to db server and verifies table values. Meanwhile the application has got very slow. Is there any relation between open connections and performance of application?

Upvotes: 2

Views: 2186

Answers (3)

SarthAk
SarthAk

Reputation: 1776

When you are done with using your Connection, you need to explicitly close it by calling its close() method in order to release any other database resources (cursors, handles, etc) the connection may be holding on to.

   Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        // Do stuff
        ...

    } catch (SQLException ex) {
        // Exception handling stuff
        ...
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) { /* ignored */}
        }
    }

It is always better to close the database/resource objects after usage. Better to close connection, resultset and statement objects in the finally block.

Until Java7, all these resources needs to be closed using a finally block. If you are using Java 7, then for closing the resources you can use a try-with-resources as follows.

try(Connection con = getConnection(url, username, password, "org.postgresql.Driver");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
) {

//statements
}catch(....){}

Upvotes: 2

Little Santi
Little Santi

Reputation: 8813

Not necessarily. In fact, it is not expected that open connections will slow down the application's performance, unless it is specifically justified.

Performance may be affected by three factors:

  • CPU usage.
  • Memory occupation.
  • I/O traffic.

Should a JDBC connection, just for being opened and not for being used, increase the CPU usage? In principle no, unless the driver is running in background some thread of its own.

Should it occupy a lot of memory? In principle no, because JDBC APIs are designed to recover data thorugh cursors (It should not occupy more than a simple working buffer) - unless the driver is not doing a correct garbage collection of used udata.

Should it do a lot of I/O traffic? In principle no, unless the driver is doing some polling or something in background.

So, as you can see, the answer is that it depends on the JCBC driver implementation. Closing a connection as soon as it is not used is a good practice for releasing resources at the server side, but usually it is not critical at the client side.

Upvotes: 1

The Impaler
The Impaler

Reputation: 48865

If you open and close connections very frequently I would suggest using a JDBC connection pool, any JDBC connection pool will do.

The pool keeps track of the use/reuse/multiplexing of the connections without opening anc closing them frequently. This way the database load becomes lighter.

Upvotes: 0

Related Questions