关闭 DB 连接后从 Oracle DB 读取 CLOB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4886113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a CLOB from Oracle DB after the DB connection is closed
提问by sengs
In one of the Java classes I am reviewing I see the following code
在我正在的 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;
}
The oracle.sql.CLOB that is returned by getCLOB() is read in another method:
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);
}
...
}
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?
我是这个项目的新手,这里的人说这段代码有效。我不明白我们如何在底层数据库连接关闭后读取 CLOB(在我看来,这是一个参考)。我错过了什么?
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.
编辑:另一点要注意的是,此代码在应用服务器中运行。Persistence.getConnection() 从数据源(最有可能使用连接池)获取连接。不知道数据库连接返回到连接池后是否使用。
EDIT2: Using the connection after it was returned to the pool might not be the cause. The app server is Oracle's Glassfish server Websphereand I am hoping they would guard against such usage.
EDIT2:在连接返回到池后使用连接可能不是原因。应用程序服务器是 Oracle 的 Glassfish 服务器Websphere,我希望他们能够防止这种使用。
回答by Lev Khomich
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.
JDBC 驱动程序将选定的 LOB 预取到结果集中。Read API 可以在没有连接的情况下使用预取缓冲区。oracle.jdbc.defaultLobPrefetchSize 参数指定的缓冲区大小,默认为 4000。
回答by a_horse_with_no_name
You should be able to simply use getString() on that column.
您应该能够简单地在该列上使用 getString() 。
The current drivers do not require the use of the CLOB interface anymore.
当前的驱动程序不再需要使用 CLOB 接口。
(at least it works for me with regular SELECT statements)
(至少它对我来说适用于常规的 SELECT 语句)