试用资源关闭订单
Try-with-resources close order
我正在查看 Java 中的 try-with-resources 示例,我了解以下内容:
try (Connection conn = DriverManager.getConnection(url, user, pwd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);) {
...
}
所以,关闭的顺序是:
rs.close();
stmt.close();
conn.close();
这是完美的,因为一个连接有一个语句,一个语句有一个结果集。
然而,在下面的例子中,关闭的顺序我认为是相反的:
示例 1:
try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
...
}
关闭顺序为:
br.close();
fr.close();
示例 2:
try (FileOutputStream fos = new FileOutputStream("testSer.ser");
ObjectOutputStream oos = new ObjectOutputStream(fs);) {
...
}
关闭顺序为:
oos.close();
fos.close();
这些例子正确吗?我认为这些示例中的收盘价应该有所不同,因为:
- 在示例 1 中,一个 BufferedReader 有一个 FileReader。
- 在示例 2 中,一个 ObjectOutputStream 有一个 FileOutputStream。
顺序是相同的:总是与指定资源的顺序相反。来自 JLS:
Resources are closed in the reverse order from that in which they were initialized.
但是,如果后面指定的资源本身调用前面指定的资源的close()
方法(如BufferedReader
和ObjectOutputStream
的情况),它可能看起来好像它们没有按预期顺序发生(并且 close()
将被多次调用)。
我觉得我说的 "a connection has a statement and a statement has a result set" 不对。也许是相反的 "a result set has a statement and a statement has a connection" 或至少 "a result set was created by a statement and a statement was created by a connection".
所以我认为:
try (Parent parent = new Parent();
Child child = parent.createChild();) {
...
}
相当于:
try (Parent parent = new Parent();
Child child = new Child(parent);) {
...
}
我正在查看 Java 中的 try-with-resources 示例,我了解以下内容:
try (Connection conn = DriverManager.getConnection(url, user, pwd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);) {
...
}
所以,关闭的顺序是:
rs.close();
stmt.close();
conn.close();
这是完美的,因为一个连接有一个语句,一个语句有一个结果集。
然而,在下面的例子中,关闭的顺序我认为是相反的:
示例 1:
try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
...
}
关闭顺序为:
br.close();
fr.close();
示例 2:
try (FileOutputStream fos = new FileOutputStream("testSer.ser");
ObjectOutputStream oos = new ObjectOutputStream(fs);) {
...
}
关闭顺序为:
oos.close();
fos.close();
这些例子正确吗?我认为这些示例中的收盘价应该有所不同,因为:
- 在示例 1 中,一个 BufferedReader 有一个 FileReader。
- 在示例 2 中,一个 ObjectOutputStream 有一个 FileOutputStream。
顺序是相同的:总是与指定资源的顺序相反。来自 JLS:
Resources are closed in the reverse order from that in which they were initialized.
但是,如果后面指定的资源本身调用前面指定的资源的close()
方法(如BufferedReader
和ObjectOutputStream
的情况),它可能看起来好像它们没有按预期顺序发生(并且 close()
将被多次调用)。
我觉得我说的 "a connection has a statement and a statement has a result set" 不对。也许是相反的 "a result set has a statement and a statement has a connection" 或至少 "a result set was created by a statement and a statement was created by a connection".
所以我认为:
try (Parent parent = new Parent();
Child child = parent.createChild();) {
...
}
相当于:
try (Parent parent = new Parent();
Child child = new Child(parent);) {
...
}