更快地从数据库读取和写入大型 CLOB 文件

Read and write large CLOB files from database Faster

我的任务是从 Oracle 9i 数据库读取 CLOB 文件并将它们写入文件。我所拥有的适用于小文件,但在大文件的情况下,大约 200MB,一个文件需要大约 5 个小时,这是完全不能接受的。我相信这可以更快地完成,但我不确定如何。这是我的:

//get the Clob record from the database and convert it to String
 public String getClobRecord() {
        Connection conn;
        Clob xmlCont;
        ResultSet rset;
        PreparedStatement stmt;
        try {
            conn = db.prepareConn();
            String sql = "select a.clob_clm from xml_statement a where period_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, pid);
            rset = stmt.executeQuery();
            while (rset.next()) {
                xmlCont = rset.getClob(1);
                xml = ClobStringConversion(xmlCont);
            }

            stmt.close();
            DBConnect.closeConn(conn);
        } catch (SQLException asd) {
            log.fatal(asd.getMessage());
        } catch (IOException asd) {
            log.fatal(asd.getMessage());
        }
        return xml;
    }
//My CLOB to String Conversion Method
  public static String ClobStringConversion(Clob clb) throws SQLException, IOException {
        if (clb == null) {
            return "";
        }
        StringBuilder str = new StringBuilder();
        String strng;
        BufferedReader br = new BufferedReader(clb.getCharacterStream());
        while ((strng = br.readLine()) != null) {
            str.append(strng);
        }

        return str.toString();
    }
//Write the String to File
public void writeXmlToFile(String fileDir, String xmlFileName) {
        File xmlfile = new File(fileDir + "/" + xmlFileName);
        xml = this.getClobRecord();
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(xmlfile));
            bw.write(formatXmlFile());
            bw.close();
        } catch (IOException asd) {
            log.fatal(asd.getMessage());
        }
    }

我究竟应该更改什么才能容纳非常大的 CLOB 文件?

如果您坚持将 clob 数据存储在内存中,您可以改进的一件事是使用 oracle 特定的功能。

public static String toString(final Clob clob)
    throws SQLException, IOException {

    if (clob == null) {
        return "";
    }

    Long length = null;

    // try to get the oracle specific CLOB length
    // no vendor-specific code here.
    try {
        final Class<?> oracleClobClass = Class.forName("oracle.sql.CLOB");
        if (oracleClobClass.isInstance(clob)) {
            length = (Long) oracleClobClass.getMethod("getLength", null)
                     .invoke(clob, null);
        }
    } catch (final Exception e) {
    }

    // we can set initial capacity if we got the length.
    final StringBuilder builder
        = length == null
        ? new StringBuilder() : new StringBuilder(length.intValue());

    final BufferedReader reader
        = new BufferedReader(clob.getCharacterStream());
    for (String line = null; (line = reader.readLine()) != null; ) {
        builder.append(line);
    }

    return builder.toString();
}