处理大数据对象,应该释放 Clob 吗?
Handle large data object, should release Clob?
我正在使用带有 ojdbc7
驱动程序的 Oracle 数据库
我使用 (Clob)rs.getObject("DATA")
从 ResultSet 获取 Clob,然后在方法中将其转换为 String:
private String clobToString(Clob data) {
StringBuilder sb = new StringBuilder();
try (Reader reader = data.getCharacterStream(); BufferedReader br = new BufferedReader(reader)){
String line;
while(null != (line = br.readLine())) {
sb.append(line);
}
} catch (Exception e) {
logger.error("Failed to read CLOB", e);
}
return sb.toString();
}
根据 Oracle 的 Using Large Objects
Blob, Clob, and NClob Java objects remain valid for at least the duration of the transaction in which they are created. This could potentially result in an application running out of resources during a long running transaction. Applications may release Blob, Clob, and NClob resources by invoking their free method.
In the following excerpt, the method Clob.free is called to release the resources held for a previously created Clob object
我是否应该添加更多代码以在 finally
中发布:
finally {
try {
data.free();
} catch (SQLException e) {
logger.error("Failed to release Clob", e);
}
}
还是因为 Clob
没有在方法外部使用,或者是否有 simple/native 从数据库中的大对象获取字符串的方法?
在该方法之外不使用 clob 的事实并不意味着调用 free()
没有用:如果你不调用它,你就是在延迟资源的释放 至少直到交易结束(如 Oracle 的文档所说)。
这也在 Do java.sql.Array/Blob/Clob types need to be "free()"ed? and Should JDBC Blob (not) be free()'d after use? and JDBC 4's java.sql.Clob.free() method and backwards compatibility
中讨论
关于 simple/native 从 clob 获取字符串的方法,参见 Most efficient solution for reading CLOB to String, and String to CLOB in Java?
我正在使用带有 ojdbc7
驱动程序的 Oracle 数据库
我使用 (Clob)rs.getObject("DATA")
从 ResultSet 获取 Clob,然后在方法中将其转换为 String:
private String clobToString(Clob data) {
StringBuilder sb = new StringBuilder();
try (Reader reader = data.getCharacterStream(); BufferedReader br = new BufferedReader(reader)){
String line;
while(null != (line = br.readLine())) {
sb.append(line);
}
} catch (Exception e) {
logger.error("Failed to read CLOB", e);
}
return sb.toString();
}
根据 Oracle 的 Using Large Objects
Blob, Clob, and NClob Java objects remain valid for at least the duration of the transaction in which they are created. This could potentially result in an application running out of resources during a long running transaction. Applications may release Blob, Clob, and NClob resources by invoking their free method.
In the following excerpt, the method Clob.free is called to release the resources held for a previously created Clob object
我是否应该添加更多代码以在 finally
中发布:
finally {
try {
data.free();
} catch (SQLException e) {
logger.error("Failed to release Clob", e);
}
}
还是因为 Clob
没有在方法外部使用,或者是否有 simple/native 从数据库中的大对象获取字符串的方法?
在该方法之外不使用 clob 的事实并不意味着调用 free()
没有用:如果你不调用它,你就是在延迟资源的释放 至少直到交易结束(如 Oracle 的文档所说)。
这也在 Do java.sql.Array/Blob/Clob types need to be "free()"ed? and Should JDBC Blob (not) be free()'d after use? and JDBC 4's java.sql.Clob.free() method and backwards compatibility
关于 simple/native 从 clob 获取字符串的方法,参见 Most efficient solution for reading CLOB to String, and String to CLOB in Java?