Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Best Practice: Closing and releasing JDBC resources

Harvey Gunther (hgunther@us.ibm.com), Senior Performance Analyst, IBM
Harvey Gunther is a Senior Performance Analyst with the IBM WebSphere Product Development team in Raleigh, North Carolina.

Summary:  This best practice recommends closing JDBC connections for quicker reuse and to improve performance.

Date:  08 Aug 2001
Level:  Intermediate

Activity:  12617 views
Comments:  

Introduction

Close and release JDBC resources when finished using them. Ensure that they are closed/released in all circumstances, including exceptions and errors. JDBC connection pooling provides a defined and finite number of JDBC connections. Shortages cause long waits. Failing to close JDBC connections causes waits for idle JDBC connections to be reaped and reused. Closing JDBC connections will allow quicker reuse and improve performance.

This best practice applies to the following product, version, and platform:

  • WebSphere Application Server Base, all platforms, versions 3.0.2.x, 3.5.x, 4.0

Recommendation

Failing to close and release JDBC connections can cause other users to experience long waits for connections. Although a JDBC connection that is left unclosed will be reaped and returned by WebSphere® Application Server after a timeout period, others may have to wait for this to occur.

Close JDBC statements when you are through with them. JDBC ResultSets can be explicitly closed as well. If not explicitly closed, ResultsSets are released when their associated statements are closed.

Ensure that your code is structured to close and release JDBC resources in all cases, even in exception and error conditions. The following shows that the acquisition and use of JDBC resources is wrapped in a "Try-Catch-Finally." Here the close of the JDBC resources is handled in the finally clause. The close will happen in all circumstances.

Proper way to close JDBC Connections and PreparedStatements

Connection conn = null;
ResultSet rs = null;
PreparedStatement pss = null;
try
{
        conn = dataSource.getConnection(USERID,PASSWORD);
        pss = conn.prepareStatement("SELECT SAVESERIALZEDDATA
            FROM SESSION.PINGSESSION3DATA WHERE SESSIONKEY = ?");
        pss.setString(1,sessionKey);
        rs = pss.executeQuery();
        pss.close();
        conn.close();
}
catch (Throwable t)
{
        // Insert Appropriate Error Handling Here
}
finally
{
        // The finally clause is always executed - even in error
        // conditions PreparedStatements and Connections will always be closed
        try
        {
                  if (pss != null)
                              pss.close();
        }
        catch(Exception e) {}

        try
        {
                  if (conn != null)
                              conn.close();
        }
        catch (Exception e){}
        }
}


Alternative

The following code shows the wrong way to close JDBC resources. They will be lost in exception scenarios. Here the application will fail to close a JDBC connection if an exception is thrown.

JDBC alternative - session data

Connection conn = null;
ResultSet rs = null;
PreparedStatement pss = null;
try
{
         conn = dataSource.getConnection(USERID,PASSWORD);
         pss = conn.prepareStatement("SELECT SAVESERIALZEDDATA
            FROM SESSION.PINGSESSION3DATA WHERE SESSIONKEY = ?");
         pss.setString(1,sessionKey);
         rs = pss.executeQuery();
         pss.close();
         conn.close();
}
catch (Throwable t)
{
          // If i reach this spot, I blew a JDBC Connection.
}


Resources

About the author

Harvey Gunther is a Senior Performance Analyst with the IBM WebSphere Product Development team in Raleigh, North Carolina.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=13512
ArticleTitle=Best Practice: Closing and releasing JDBC resources
publish-date=08082001
author1-email=hgunther@us.ibm.com
author1-email-cc=