【问题标题】:Use Oracle createTemporary to update Clob使用 Oracle createTemporary 更新 Clob
【发布时间】:2012-01-17 17:07:32
【问题描述】:

我使用以下代码更新 Oracle Clob:

CLOB tempClob = null;
try {
  Connection conn = getConnection();
  PreparedStatement = = conn.prepareStatement("UPDATE PROGRAM_HISTORY SET DETAILS = ? WHERE ID = 12");
  tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
  tempClob.open(CLOB.MODE_READWRITE);
  Writer tempClobWriter = tempClob.getCharacterOutputStream();
  tempClobWriter.write(clobData);
  tempClobWriter.flush();
  tempClobWriter.close();
  tempClob.close();

 pStmt.setClob(1, tempClob);
 pStmt.execute();

} catch (Exception ex) { // Trap errors
  System.out.println(" Error Inserting Clob : "+ex.toString());
  ex.printStackTrace();
}finally{
  if(tempClob != null ) tempClob.freeTemporary();
  opstmt.close();
  conn.close();

}

如您所见,在创建临时 clob 后,我使用了 tempClob.open(CLOB.MODE_READWRITE); 打开并使用 tempClob.close() 稍后关闭;所以我的问题是这有必要吗?如果是,为什么?因为我从谷歌搜索的一些示例代码没有这个过程。

我的第二个问题是,在 finally 语句中是否需要 tempClob.close() ;用完后我们必须像连接一样关闭临时clob吗?或者不需要这样做它会自动释放?

【问题讨论】:

    标签: java sql oracle jdbc oracle11g


    【解决方案1】:

    Oracle 会话对象将保留对 CLOB 的引用,因此垃圾收集器不会触及它。会话关闭时将自动释放。

    请注意,实际的临时 CLOB 内存不会存在于 Java VM 中的某个位置,而是存在于 Oracle 服务器进程 (PGA) 或临时表空间(磁盘)中,具体取决于您的数据库配置和当前工作负载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多