在我正在审查的 Java 类之一中,我看到以下代码
private oracle.sql.CLOB getCLOB() {
oracle.sql.CLOB xmlDocument = null;
CallableStatement cstmt = null;
ResultSet resultSet = null;
Connection connection = null;
try {
connection = Persistence.getConnection();
cstmt = connection.prepareCall("{call pkg.proc(?,?)}");
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.setString(2, id);
cstmt.execute();
resultSet = (ResultSet)cstmt.getObject(1);
if (resultSet.next()) {
xmlDocument = ((OracleResultSet) resultSet).getCLOB(1);
}
} finally {
Persistence.closeAll(resultSet, cstmt, connection);
}
return xmlDocument;
}
getCLOB() 返回的 oracle.sql.CLOB 在另一种方法中读取:
private void anotherMethod() {
...
oracle.sql.CLOB xmlDocument = getCLOB();
clobLength = xmlDocument.length();
chunkSize = xmlDocument.getChunkSize();
textBuffer = new char[chunkSize];
for (int position = 1; position <= clobLength; position += chunkSize) {
charsRead = xmlDocument.getChars(position, chunkSize, textBuffer);
outputBufferedWriter.write(textBuffer, 0, charsRead);
}
...
}
我是这个项目的新手,这里的人说这段代码有效。我不明白在底层数据库连接关闭后我们如何读取 CLOB(在我看来,这是一个引用)。我错过了什么?
编辑:另一点要注意的是,此代码在应用程序服务器中运行。 Persistence.getConnection() 从数据源(最有可能使用连接池)获取连接。不知道数据库连接返回到连接池后是否使用。
EDIT2:在连接返回到池后使用连接可能不是原因。应用程序服务器是 Oracle 的 Glassfish 服务器 Websphere,我希望他们能够防止这种使用。
最佳答案
JDBC 驱动程序将选定的 LOB 预取到结果集中。读取 API 可以使用预取缓冲区
没有连接。 oracle.jdbc.defaultLobPrefetchSize 参数指定的缓冲区大小,默认为 4000。
关于java - 关闭数据库连接后从 Oracle DB 读取 CLOB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4886113/