在我正在查看的 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,我希望他们能够防止此类使用。