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

3.5k views Asked by At

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?

5

There are 5 answers

0
Mureinik On BEST ANSWER

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...
}
0
f1sh On

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.

0
spongebob On

Your code will only close the ResultSet, leaving the Connection and Statement open.

In your case, if you do not reuse the Connection nor the Statement, to close them all, note the try-with-resources supports multiple statements:

try (Connection connection = DriverManager.getConnection(...);
     Statement statement = connection.createStatement();
     ResultSet resultSet = statement.executeQuery(...)) {
    ...
}
4
Stephen C On

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 ...)

0
Arun Prasat On

It wont close all the three resources since you have not created each resource separately followed by semicolon. If you create each resource separately it will be closed. ex:

 try (
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "password");
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(sql)
            ){}

this will work because try-with-resources statement closes the resources in the reverse order from which they were opened. We can use objects which implements AutoCloseable inside try with resource if it doesnt implement then it needs to be closed inside the final black. you can check more details here https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html