2011-02-03 38 views
2

后的Java类,我检讨我看到下面的代码阅读从Oracle数据库CLOB一个数据库连接被关闭

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()从数据源(很可能是连接池)获取连接。我想知道数据库连接在返回到连接池后是否被使用。

编辑2:它返回到池后使用连接可能不是原因。应用服务器是Oracle的Glassfish服务器 Websphere ,我希望他们能够防范这种用法。

回答

1

您应该可以简单地在该列上使用getString()。

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

(至少它为我的作品与普通的SELECT语句)

2

JDBC驱动程序选择预取到结果集的LOB。 Read API可以使用预取缓冲区 而无需连接。由oracle.jdbc.defaultLobPrefetchSize参数指定的缓冲区大小,默认值为4000.

+1

相关链接 - http://download.oracle.com/docs/cd/E16338_01/appdev.112/e13995/oracle/jdbc/OracleStatement.html #setLobPrefetchSize_int_ – sengs 2011-02-03 13:54:11