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 6102305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:35:57+00:00 2026-05-23T13:35:57+00:00

We always hear that we should close the connection once we are done with

  • 0

We always hear that we should close the connection once we are done with a transaction. My question here is: even if we don’t close the connection, the garbage collector will reclaim the memory once we are out of the method inside of which we are creating the connection. so what’s the actual issue, even if we don’t close the connection?

  • 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-05-23T13:35:57+00:00Added an answer on May 23, 2026 at 1:35 pm

    No, GC doesn’t help you here, because it’s not about memory.

    A connection is a scarce resource on the database server, not in your app client. If you clean up your memory without telling the server to do the same you’ll leak a connection that may not be reclaimed. Eventually you’ll run out.

    Same with ResultSet – it’s a cursor.

    You must always close all SQL resources – Connection, Statement, and ResultSet – in individual finally blocks, wrapped in try/catch blocks, in reverse order of acquisition.

    public class JdbcTemplate {
    
        public Object exampleSqlOperation(Connection c) throws SQLException {
            Object results = null;
    
            Statement st = null;
            ResultSet rs = null;
    
            try {
               // SQL stuff here
               // Load into results Object - data structure
            } catch (SQLException e) {
               e.printStackTrace(); // Better to log it.
            } finally {
                DatabaseUtils.close(rs);  
                DatabaseUtils.close(st);
            }
    
            return results;
        }
    }
    
    public class DatabaseUtils {
        public static void close(Connection c) {
            try {
                if (c != null) c.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // Same for Statement, ResultSet
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know so much about Test-Driven Development (TDD), but I always hear that
I always hear that programmers try to pick the right tool for the job.
You always hear that functional code is inherently easier to parallelize than non-functional code,
I always hear that Java being open-source is a big benefit, but I fail
I know this isn't strictly speaking a programming question but something I always hear
I always hear that JavaScript is single-threaded; that when JavaScript is executed, it's all
I always hear that reading a CAPTCHA is impossible and so now I want
I always hear using global variables are dangerous. Does this apply to Drupal? Take
I always believe they did, but seeing some answers here make me doubt... Can
So you always hear a bunch of stories about web sites getting popular, and

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.