gpt4 book ai didi

java - 处理大数据对象,应该释放Clob吗?

转载 作者:行者123 更新时间:2023-11-30 05:43:23 25 4
gpt4 key购买 nike

我正在使用带有 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 没有在外部方法中使用,或者是否有一种简单/ native 的方法从数据库的大对象中获取字符串?

最佳答案

在该方法之外不使用 clob 的事实并不意味着调用 free() 没有用:如果不调用它,就会延迟资源的释放至少直到交易结束(正如 Oracle 的文档所说)。Do java.sql.Array/Blob/Clob types need to be "free()"ed? 中也对此进行了讨论。和 Should JDBC Blob (not) be free()'d after use?JDBC 4's java.sql.Clob.free() method and backwards compatibility

关于从 clob 获取字符串的简单/ native 方法,请参阅 Most efficient solution for reading CLOB to String, and String to CLOB in Java?

关于java - 处理大数据对象,应该释放Clob吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55275388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com