Oracle9i Application Developer's Guide - Large Objects (LOBs)
Release 1 (9.0.1)

Part Number A88879-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to beginning of chapter Go to next page

Modeling and Design, 8 of 21


OPEN, CLOSE, and ISOPEN Interfaces for Internal LOBs

The OPEN, CLOSE, and ISOPEN interfaces let you open and close an internal LOB and test whether an internal LOB is already open.

It is not mandatory that you wrap all LOB operations inside the OPEN/CLOSE APIs. The addition of this feature does not impact already-existing applications that write to LOBs without first opening them, since these calls did not exist in 8.0.


Note:

Openness is associated with the LOB, not the locator. The locator does not save any information as to whether the LOB to which it refers is open. 


Wrap LOB Operations Inside an OPEN / CLOSE Call

Close All Opened LOBs Before Committing the Transaction

It is an error to commit the transaction before closing all opened LOBs that were opened by the transaction. When the error is returned, the openness of the open LOBs is discarded, but the transaction is successfully committed. Hence, all the changes made to the LOB and non-LOB data in the transaction are committed but but the domain and functional indexes are not updated. If this happens, please rebuild your functional and domain indexes on the LOB column.


Note:

Changes to the LOB are not discarded if the COMMIT returns an error. 


At transaction rollback, the openness of all open LOBs still open for that transaction are discarded. Discarding the openness means the following for LOBs:

What is a 'Transaction' Where an Open LOB Value is Closed?

A 'transaction' where an open LOB value must be closed meets one of the following:

A LOB opened when there is no transaction, must be closed before the end of the session. If there are still open LOBs at the end of the session, the openness will be discarded and no triggers on domain indexes will be fired.

Do Not Open or Close Same LOB Twice!

It is also an error to open/close the same LOB twice either with different locators or with the same locator.

Example 1: Correct Use of OPEN/CLOSE Calls to LOBs in a Transaction

This example shows the correct use of open and close calls to LOBs inside and outside a transaction.

DECLARE 
   Lob_loc1 CLOB; 
   Lob_loc2 CLOB;
   Buffer   VARCHAR2(32767); 
   Amount   BINARY_INTEGER := 32767; 
   Position INTEGER := 1; 
BEGIN 
   /* Select a LOB: */ 
   SELECT Story INTO Lob_loc1 FROM Multimedia_tab WHERE Clip_ID = 1; 

   /* The following statement opens the LOB outside of a transaction 
      so it must be closed before the session ends: */ 
   DBMS_LOB.OPEN(Lob_loc1, DBMS_LOB.LOB_READONLY); 
   /* The following statement begins a transaction. Note that Lob_loc1 and 
      Lob_loc2 point to the same LOB:  */ 
   SELECT Story INTO Lob_loc2 FROM Multimedia_tab WHERE Clip_ID = 1 for update;
   /* The following LOB open operation is allowed since this lob has  
      not been opened in this transaction:  */ 
   DBMS_LOB.OPEN(Lob_loc2, DBMS_LOB.LOB_READWRITE); 
   /* Fill the buffer with data to write to the LOB */
   buffer := 'A good story';
   Amount := 12;
   /* Write the buffer to the LOB: */ 
   DBMS_LOB.WRITE(Lob_loc2, Amount, Position, Buffer); 
   /* Closing the LOB is mandatory if you have opened it: */ 
   DBMS_LOB.CLOSE(Lob_loc2); 
   /* The COMMIT ends the transaction. It is allowed because all LOBs 
      opened in the transaction were closed. */ 
   COMMIT; 
   /* The the following statement closes the LOB that was opened  
       before the transaction started: */ 
   DBMS_LOB.CLOSE(Lob_loc1); 
END; 

Example 2: Incorrect Use of OPEN/CLOSE Calls to a LOB in a Transaction

This example the incorrect use of OPEN and CLOSE calls to a LOB and illustrates how committing a transaction which has open LOBs returns an error.

DECLARE
   Lob_loc CLOB;
BEGIN
   /* Note that the FOR UPDATE clause starts a transaction: */
   SELECT Story INTO Lob_loc FROM Multimedia_tab WHERE Clip_ID = 1 for update;
   DBMS_LOB.OPEN(Lob_loc, DBMS_LOB.LOB_READONLY);
   /* COMMIT returns an error because there is still an open LOB associated
   with this transaction: */
   COMMIT;
END;


Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback