C.c
C.c

Reputation: 1945

Use Oracle createTemporary to update Clob

I used the following codes to update 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();

}

As you can see, after creating temporary clob, I used tempClob.open(CLOB.MODE_READWRITE); to open and use tempClob.close() to colse later; so My question is that is this necessary? if yes why? because some example codes I searched from google don't have this procedure.

My second question is that is this required to tempClob.close() in finally statement; we must close temporary clob just like connection after used out? or don't need do this it will be automatically released?

Upvotes: 0

Views: 3822

Answers (1)

HAL 9000
HAL 9000

Reputation: 3985

The Oracle session object will keep a reference to the CLOB, so the garbage collector won't touch it. It will be freed automaticly when the session is closed.

Note that the actual temp CLOB memory will not exist somewhere in the Java VM, but either in the Oracle server process (PGA) or in the temp tablespace (disk), depending on your database configuration and the current workload.

Upvotes: 1

Related Questions