请看下面的DbUtil
类示例。以下是它的三种方法
public static void closeResults(ResultSet rs) {
if (rs != null) {
try {
if (!rs.isClosed()){
rs.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public static void closeStatement(Statement stmt) {
if(stmt != null) {
try {
if (!stmt.isClosed()) {
stmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
if(!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
如您所见,所有 3 种方法都有相同的逻辑,我想让这段代码干燥。一个新的常用方法可以这样写
public static void closeStatement(AutoCloseable ob) {
if(ob != null) {
try {
ob.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
AutoCloseable
接口不包含isClosed
方法。但是在尝试关闭资源之前执行该检查仍然是一个好习惯(甚至必须做)。我对吗?代码可以以某种方式简化并且仍然执行isClosed
检查吗?
笔记。这只是问题的一个例子。我知道该AutoCloseable
界面是为尝试资源技术而设计的,因此可以使用该样式重写代码,并且DbUtil
不再需要。但我想为自己澄清一下,在类似的情况下甚至可以做些什么。例如,我会考虑创建一些接口,例如,MyAutoCloseable
扩展AutoCloseable
一个并拥有isClosed
方法,但肯定不会奏效,因为不可能强制转换ResultSet
或Statement
转换为MyAutoCloseable
.