【发布时间】:2011-02-07 20:49:34
【问题描述】:
在我正在查看的一个 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,我希望他们能防范这种用法。
【问题讨论】: