Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
Using "try with resources" for resources created without any reference

Consider the following situation :

try (ResultSet resultSet = DriverManager.getConnection("jdbc:...", "user", "pass")
                                      .createStatement().executeQuery(sql)) {    
                    .
                    .
                    .
}

This is a situation where three resources have been created inside try resources: a Connection, a Statement, and a ResultSet.

What will happen to these three resources after the try block ends? Will they all be closed, even if they haven't any reference to them, or will only the resultSet be closed?

Is this safe to declare AutoCloseable resources without any reference to them in try with resource blocks?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Only the ResultSet will be closed. If you want multiple resources to be closed, you need to declare them separately:

try (Connection conn  = DriverManager.getConnection("jdbc:...", "user", "pass");
     Statement stmt = conn.createStatement();
     ResultSet resultSet = stmt.executeQuery(sql)) {    

     // do stuff...
}
over 4 years ago · Santiago Trujillo Report

0

Is this safe to declare AutoCloseable resources without any reference to them in try with resource blocks?

Yes and no. Those resources that were not assigned to resource variables won't be auto-closed by this code. Therefore:

  • "Yes" those resources will still be "safe" to use via operations on the ResultSet within the try block.
  • "No" those resources will leak, and that is liable to cause problems later on.

Since I interpret "safe" to mean both of those things, my considered answer is "No" it is not safe.

The only resources that will be auto-closed by the try-with-resources are the ones that are assigned to the resource variables. So the correct way to write that would be:

try (Connection connection = DriverManager.getConnection(...);
     Statement statement = connection.createStatement();
     ResultSet resultSet = statement.executeQuery(sql)) {
    // ...   
}

Note that the resource variables are auto-closed in the reverse order that they are declared.


Note that you can often get away with it. For example:

try (BufferedWriter bw = new BufferedWriter(new FileWriter(...)) {
    // ...   
}

is likely to be OK except under extremely unusual circumstances. When bw is auto-closed, its close() method calls close() on the FileWriter. The only case where this might leak a resource is when the BufferedWriter creation / construction fails. I think that is only possible if you are unlucky enough to get an OOME at that point. (And the OOME may well trigger a full garbage collection which will find and close the dropped resource anyway ...)

over 4 years ago · Santiago Trujillo Report

0

You can do that with multiple resources too:

try (Connection c = DriverManager.getConnection("jdbc:...", "user", "pass");
     Statement s = c.createStatement();
     ResultSet resultSet = s.executeQuery(sql)) {
   //...
}

All variables that implement the AutoClosable interface get closed after the execution of the try block.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!