【问题标题】:Reading a CLOB from Oracle DB after the DB connection is closedDB 连接关闭后从 Oracle DB 读取 CLOB
【发布时间】: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,我希望他们能防范这种用法。

【问题讨论】:

    标签: java oracle jdbc


    【解决方案1】:

    JDBC 驱动程序将选择的 LOB 预取到结果集中。读取 API 可以使用预取缓冲区 没有连接。 oracle.jdbc.defaultLobPrefetchSize 参数指定的缓冲区大小,默认为 4000。

    【讨论】:

    【解决方案2】:

    您应该能够在该列上简单地使用 getString()。

    当前的驱动程序不再需要使用 CLOB 接口。

    (至少它适用于我的常规 SELECT 语句)

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2013-10-22
      • 2019-12-26
      • 2020-09-28
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      相关资源
      最近更新 更多