問題文
A method opens a database connection, runs one query, and walks through the rows of the result. Which arrangement releases the three resources correctly?
選択肢
- Close the connection first and the result set last, because the outermost resource has to be released before the ones inside it
- Keep one connection in a static field and share it for the whole run of the program, because a connection is safe for use from several threads by contract
- Declare all three of them as ordinary variables and let the collector release them once the method returns, because the driver registers a hook for that
- Declare the connection, the statement, and the result set in one try-with-resources header, which closes all three of them in reverse order