Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:22:15+00:00 2026-06-09T21:22:15+00:00

It is strongly suggested to close JDBC objects (connections, statements, result sets) when done

  • 0

It is strongly suggested to close JDBC objects (connections, statements, result sets) when done using them. However, that produces loads of code like that:

Connection conn = null;
Statement stm = null;
ResultSet res = null;
try {
  // Obtain connection / statement, get results, whatever...
} catch (SQLException e) {
  // ...
} finally {
  if (res != null) { try { res.close(); } catch (SQLException ignore) {}}
  if (stm != null) { try { stm.close(); } catch (SQLException ignore) {}}
  if (conn != null) { try { conn.close(); } catch (SQLException ignore) {}}
}

Now I thought about reducing the amount of (repeating) code for closing the objects by implementing a helper function. It takes the objects as arguments and tries to invoke the method close() of each object (if the object does have such a method), using reflection.

public void close(Object... objects) {
  for (Object object : objects) {
    for (Method method : object.getClass().getMethods()) {
      if (method.getName().equals("close")) {
        try {
          method.invoke(object);
        } catch (Exception e) {
          e.printStackTrace();
        }
        break; // break on the methods, go for the next object
      }
    }
  }
}

The finally block can be reduced to this:

} finally {
  close(res, stm, conn);
}

Is that a good thing to do? If no, what are the reasons? Is there a “better” way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:22:16+00:00Added an answer on June 9, 2026 at 9:22 pm

    Personally, I wouldn’t use reflection in this case, when there are plenty of good ways to do this without needing it. Here are a couple things you can do.

    1. Use Spring. Spring has a JdbcTemplate object that helps to alleviate the redundancy of Jdbc coding. The boilerplate code is hidden in the implementation of JdbcTemplate, so you are free to do what matters for your app.
    2. Use Java7. Java7 provides a new language construct that makes it easier to close objects that implement the AutoClosable interface.
    3. Create your own library to handle the boilerplate code. If Spring is too heavy for your needs, you can do all of the closing yourself easily in one base class, from which your Jdbc interaction classes can extend. You will have to write it once, but it can, for the most part, be out of sight and out of mind.

    The Java7 way looks something like this:

    try (
        Connection conn = getConnectionSomehow();
        Statement statement = getStatementFromConnSomehow(conn);
    ) {
        //use connection
        //use statement
    } catch(SomeException ex) {
        //do something with exception
    }//hey, check it out, conn and statement will be closed automatically! :)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apple strongly recommends using the binary plist format when reading large XML-based data sets
i have an strongly typed object that represents all of the textbox properties of
I am using a Strongly Typed Dataset as DAL. I want to return a
I'm strongly considering adding unit testing to an existing project that is in production.
I strongly believe that, reading code and reading good code is key to great
I use some strongly-typed expressions that get serialized to allow my UI code to
I have a winforms app that uses a strongly typed custom DataSet for holding
Firstly I appreciate that this question could be seen as subjective but I strongly
After reading books like The Pragmatic Programmer, one thing it strongly suggested was to
I have been using WPF for a new project which is far from done.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.