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

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

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

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.