clob oracle 连接_关闭数据库连接后从Oracle DB读取CLOB
In one of the Java classes I am reviewing I see the following codeprivate oracle.sql.CLOB getCLOB() {oracle.sql.CLOB xmlDocument = null;CallableStatement cstmt = null;ResultSet resultSet = null;Connec
In one of the Java classes I am reviewing I see the following code
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;
}
The oracle.sql.CLOB that is returned by getCLOB() is read in another method:
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);
}
...
}
I am new to this project and the folks here say this code is working. I don't understand how we can read a CLOB (which, in my understanding, is a reference) after the underlying database connection is closed. What am I missing?
EDIT: Another point to note is that this code is running in an app server. Persistence.getConnection() gets the connection from a data source (most probably with a connection pool). I wonder if the database connection is used after it is returned to the connection pool.
EDIT2: Using the connection after it was returned to the pool might not be the cause. The app server is Oracle's Glassfish server Websphere and I am hoping they would guard against such usage.
解决方案
JDBC driver prefetches LOBs selected into a result set. Read API can use prefetch buffers
without connection. Buffer size specified by oracle.jdbc.defaultLobPrefetchSize parameter with default 4000.
更多推荐




所有评论(0)