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

  • Home
  • SEARCH
  • 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 8320827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:47:45+00:00 2026-06-08T22:47:45+00:00

In JDBC literatur I often encounter snippets like void databaseCall() { Connection con =

  • 0

In JDBC literatur I often encounter snippets like

void databaseCall() {
    Connection con = null;
    Statement statement = null        
    try {      
          con = getConnection(...);    
          statement = con.createStatement(...);        
          // do some query
    } catch (SQLException e) {
         // bla bla
    } finally {
         try {con.close();} catch (Exception e1) {}
         try {statement.close();} catch (Exception e1) {}
    }
}

I know it’s best practice to close connections and statements explicity, but, in this case, apparently the resources con and statement will be closed when the method is finished, i.e., when the try block is finished. Is the close statements in the finally block really necessary? Even if we did not release the resources explicitly, wouldn’t they be closed any way when the method is finished?

  • 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-08T22:47:47+00:00Added an answer on June 8, 2026 at 10:47 pm

    No, they wouldn’t be closed.

    If getConnection() creates a new Connection, then the only thing that will happen at the end of the method is that the Connection could be garbage collected. But the GC won’t call the close() method for you. And, anyway, you want to close as soon as possible.

    Most of the time, getConnection() will simply get a connection from a pool of connections, that stay open for a very long time. If you don’t close() the connection, it will not be put back into the pool of available connections, and after a few seconds or minutes, you won’t have any connection available anymore.

    The title asks about System.exit(), but the question body doesn’t. If you call System.exit(), then the connection will end up being closed because the database will notice that the communication is broken. But it’s extremely rare for a program to startup, execute a query, and exit. Most of the applications start and stay running for days or even months. So you really want to release the connection once you have finished using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Jdbc,the statement was created after successfully created connection then we are called st=conn.createStatement()
The JDBC 3.0 spec talks about Connection (and Prepared Statement) pooling. We have several
When using JDBC, I often come across constructs like ResultSet rs = ps.executeQuery(); while
Oracle JDBC connection with Weblogic 10 datasource mapping, giving problem java.sql.SQLException: Closed Connection I
I noticed that JDBC has different Drivers like: Type 1 Driver Type 2 Driver
The spring JDBC template returns me a list which looks like below: [{CODE_C=CSC, DESC_C=CSC},
When using an Oracle JDBC connection pool, is there a way to control how
I have set up jdbc connection pooling in a java-ee environment by doing the
I have setup the JDBC connection pooling and its showing the following error when
The JDBC java.sql.Statement class has a cancel() method. This can be called in another

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.