Skip Headers

Oracle9i Application Developer's Guide - Large Objects (LOBs)
Release 2 (9.2)

Part Number A96591-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 next page

11
Temporary LOBs

Use Case Model

In this chapter we discuss each operation on a Temporary LOB (such as "Loading a Temporary LOB with Data from a BFILE") in terms of a use case. Table 11-1, "Use Case Model Overview: Internal Temporary LOBs" lists all the use cases.

Individual Use Cases

Each Internal Persistent LOB use case is described as follows:

Use Case Model: Internal Temporary LOBs

Table 11-1, "Use Case Model Overview: Internal Temporary LOBs" indicates with + where examples are provided for specific use cases and in which programmatic environment (see Chapter 3, "LOB Support in Different Programmatic Environments" for a complete discussion and references to related manuals).

We refer to programmatic environments by means of the following abbreviations:

Table 11-1 Use Case Model Overview: Internal Temporary LOBs  
Use Case and Page P O B C V* J

Appending One Temporary LOB to Another

+

+

+

+

-

-

Checking If a LOB is Temporary

+

+

+

+

-

+

Comparing All or Part of Two Temporary LOBs

+

-

+

+

-

-

Copying a LOB Locator for a Temporary LOB

+

+

+

+

-

-

Copying All or Part of One Temporary LOB to Another

+

+

+

+

-

-

Creating a Temporary LOB

+

+

+

+

-

+

Determining if a LOB Locator for a Temporary LOB Is Initialized

-

+

-

+

-

-

Determining If a Pattern Exists in a Temporary LOB (instr)

+

-

+

+

-

-

Loading a Temporary LOB with Data from a BFILE

+

+

+

+

-

-

Loading a Temporary BLOB with Binary Data from a BFILE

+

-

-

-

-

-

Loading a Temporary CLOB/NCLOB with Character Data from a File

+

-

-

-

-

-

Disabling LOB Buffering for a Temporary LOB

-

+

+

+

-

-

Displaying Temporary LOB Data

+

+

+

+

-

-

Enabling LOB Buffering for a Temporary LOB

-

+

+

+

-

-

Erasing Part of a Temporary LOB

+

+

+

+

-

-

Finding Character Set Form of a Temporary LOB

-

+

-

-

-

-

Finding Character Set ID of a Temporary LOB

-

+

-

-

-

-

Finding the Length of a Temporary LOB

+

+

+

+

-

-

Flushing Buffer for a Temporary LOB

-

+

+

+

-

-

Freeing a Temporary LOB

+

+

+

+

-

+

Is One Temporary LOB Locator Equal to Another

-

+

-

+

-

-

Loading a Temporary LOB with Data from a BFILE

+

+

+

+

-

-

Loading a Temporary BLOB with Binary Data from a BFILE

+

-

-

-

-

-

Loading a Temporary CLOB/NCLOB with Character Data from a File

+

-

-

-

-

-

Reading Data from a Temporary LOB

+

+

+

+

-

-

Reading Portion of Temporary LOB (Substr)

+

-

+

+

-

-

Trimming Temporary LOB Data

+

+

+

+

-

-

Write-Appending to a Temporary LOB

+

+

+

+

-

-

Writing Data to a Temporary LOB

+

+

+

+

-

-

* There are no Visual Basic examples provided for internal temporary LOB use cases.

Programmatic Environments

Oracle9i supports the definition, creation, deletion, access, and update of temporary LOBs in the following programmatic environments or "interfaces":

Locators

The 'interfaces' listed earlier, operate on temporary LOBs through locators in the same way that they do for permanent LOBs. Since temporary LOBs are never part of any table, you cannot use SQL DML to operate on them. You must manipulate them using the DBMS_LOB package, OCI, or the other programmatic interfaces.

Temporary LOB Locators Can be IN Values

SQL support for temporary LOBs is available in that temporary LOB locators can be used as IN values, with values accessed through a locator. Specifically, they can be used as follows:

Can You Use the Same Functions for Temporary and Internal Persistent LOBs?

You can use the following functions for internal persistent LOBs and temporary LOBs:

In addition, you can use the ISTEMPORARY function to determine if a LOB is temporarily based on its locator.


Note:

One thing to keep in mind is that temporary LOBs do not support transactions and consistent reads.


DBMS_LOB.createtemporary() Parameter is a Hint

The DBMS_LOB.createtemporary() call takes a duration parameter. This parameter is merely a hint and is not strictly enforced.

Temporary LOB Data is Stored in Temporary Tablespace

Temporary LOBs are not stored permanently in the database like other data. The data is stored in temporary tablespaces and is not stored in any tables. This means you can CREATE an internal temporary LOB (BLOB,CLOB, NCLOB) on the server independent of any table, but you cannot store that LOB.

Since temporary LOBs are not associated with table schema, there is no meaning to the terms "inline" and "out-of-line" for temporary LOBs.


Note:

All temporary LOBs reside on the server. There is no support for client-side temporary LOBs.


Lifetime and Duration of Temporary LOBs

The default lifetime of a temporary LOB is a session.

The interface for creating temporary LOBs includes a parameter that lets you specify the default scope of the life of the temporary LOB. By default, all temporary LOBs are deleted at the end of the session in which they were created. If a process dies unexpectedly or the database instance terminates, all temporary LOBs are deleted.

OCI Can Group Temporary LOBs into Logical Buckets

OCI users can group temporary LOBs together into a logical bucket.

"OCIDuration" represents a store for temporary LOBs. There is a default duration for every session into which temporary LOBs are placed if you do not specify a specific duration. The default duration ends when your session ends. Also, you can perform an OCIDurationEnd operation that frees all OCIDuration contents.

Memory Handling

LOB Buffering and CACHE, NOCACHE, CACHE READS

Temporary LOBs are especially useful when you want to perform transformational operations on a LOB -- such as morphing an image, or changing a LOB from one format to another -- and then return it to the database.

These transformational operations can use LOB Buffering. You can specify CACHE,NOCACHE,or CACHE READS for each temporary LOB, and FREE an individual temporary LOB when you have no further need for it.

Temporary Tablespace

Your temporary tablespace is used to store temporary LOB data. Data storage resources are controlled by the DBA through control of a user's access to temporary tablespaces, and by the creation of different temporary tablespaces.

Explicitly Free Temporary LOB Space to Reuse It

Memory usage increases incrementally as the number of temporary LOBs grows. You can reuse temporary LOB space in your session by freeing temporary LOBs explicitly.

Selecting a Permanent LOB INTO a Temporary LOB Locator

We previously noted that if you perform the following:

SELECT permanent_lob INTO temporary_lob_locator FROM y_blah WHERE x_blah

the temporary_lob_locator will get overwritten with the locator for the permanent_lob. The temporary_lob_locator now points to the LOB stored in the table.


Note:

Unless you saved the locator for the temporary_lob in another variable, you will lose track of the LOB that temporary_lob_locator originally pointed to before the SELECT INTO operation.

In this case the temporary LOB will not get implicitly freed. If you do not wish to waste space, explicitly free a temporary LOB before overwriting it with a permanent LOB locator.


Since CR and rollbacks are not supported for temporary LOBs, you will have to free the temporary LOB and start over again if you run into an error.

See Also:

Chapter 7, "Modeling and Design", "LOB Storage" and Chapter 9, "LOBS: Best Practices".

Locators and Semantics

Creation of a temporary LOB instance by a user causes the engine to create, and return a locator to LOB data. Temporary LOBs do not support any operations that are not supported for persistent LOB locators, but temporary LOB locators have specific features.

Features Specific to Temporary LOBs

The following features are specific to temporary LOBs:

Security Issues with Temporary LOBs

Security is provided through the LOB locator.

NOCOPY Restrictions

See PL/SQL User's Guide and Reference, Chapter 7: "SUBPROGRAMS" -- NOCOPY COMPILER HINT, for guidelines, restrictions, and tips on using NOCOPY.

Managing Temporary LOBs

Oracle keeps track of temporary LOBs in each session, and provides a v$ view called v$temporary_lobs. From the session the application can determine that user owns the temporary LOBs. This view can be used by DBAs to monitor and guide any emergency cleanup of temporary space used by temporary LOBs.

Using JDBC and Temporary LOBs

Oracle9i JDBC drivers contain APIs to create and close temporary LOBs. These APIs should replace the previous workaround of using dbms_lob.createTemporary(), dbms_lob.isTemporary() and dbms_lob.freeTemporary() in dbms_lob PL/SQL package.

Using JDBC and Temporary BLOBs

The oracle.sql.BLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Blob interface. Each oracle.sql.BLOB instance represents a BLOB object in the database. Table 11-2 lists new Oracle extension APIs in oracle.sql.BLOB to access temporary BLOBs.

Table 11-2 JDBC: Temporary BLOB APIs
Methods Description

public static BLOB createTemporary(
    Connection conn, boolean cache,
    int duration) throws SQLException

Creates a temporary BLOB. See

public static boolean isTemporary(
    BLOB blob) throws SQLException

Checks if the specified BLOB locator refers to a temporary BLOB

public boolean isTemporary()
    throws SQLException

Checks if the current BLOB locator refers to a temporary BLOB

public static void freeTemporary(
    BLOB temp_blob) throws SQLException

Frees the specified temporary BLOB

public void freeTemporary()
    throws SQLException

Frees the temporary BLOB

Using JDBC and Temporary CLOBs

The oracle.sql.CLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Clob interface. Table 11-3 lists the new Oracle extension APIs in oracle.sql.CLOB for accessing temporary CLOBs.

Table 11-3 JDBC: Temporary CLOB APIs
Methods Description

public static CLOB createTemporary(
    Connection conn, boolean cache,
    int duration)
  throws SQLException

Creates a temporary CLOB

public static boolean isTemporary(
    CLOB clob) throws SQLException

Checks if the specified CLOB locator refers to a temporary CLOB

public boolean isTemporary()
    throws SQLException

Checks if the current CLOB locator refers to a temporary CLOB

public static void freeTemporary(
    CLOB temp_clob)
  throws SQLException

Frees the specified temporary CLOB

public void freeTemporary()
  throws SQLException

Frees the temporary CLOB


Creating a Temporary LOB

Figure 11-1 Use Case Diagram: Creating a Temporary LOB

Text description of adlob089.gif follows
Text description of the illustration adlob089.gif


See Also:

Table 11-1, "Use Case Model Overview: Internal Temporary LOBs".

Purpose

This procedure describes how to create a temporary LOB.

Usage Notes

A temporary LOB is empty when it is created.

Temporary LOBs do not support the EMPTY_BLOB() or EMPTY_CLOB() functions that are supported for permanent LOBs. The EMPTY_BLOB() function specifies the fact that the LOB is initialized, but not populated with any data.

Syntax

See Chapter 3, "LOB Support in Different Programmatic Environments" for a list of available functions in each programmatic environment. Use the following syntax references for each programmatic environment:

Scenario

These examples create a temporary LOB and copy the object contents from an ad_composite image from the Print_media table to the temporary LOB. This technique is useful, for example, if you need to convert the image from one graphic format to another. The temporary LOB is read through the CACHE, and is automatically cleaned up at the end of the user's session, if it is not explicitly freed sooner.

Examples

Examples are provided in the following programmatic environments:

PL/SQL (DBMS_LOB Package): Creating a Temporary LOB


Note:

You must set up the following data structures to use most of the examples given in this chapter:


Rem  Set up script for working with Temporary LOBs

DROP TABLE long_raw_tab;
CREATE TABLE long_raw_tab (id number, long_raw_col long raw);
INSERT INTO long_raw_tab VALUES (1, HEXTORAW('7D'));
INSERT INTO Print_media (product_id,ad_composite) SELECT 
   id,TO_LOB(long_raw_col) FROM long_raw_tab;


Note:

The DBMS_LOB.CREATETEMPORARY procedure takes an optional duration parameter. In PL/SQL, this duration parameter is used only as a hint about the duration of the LOB data. PL/SQL calculates the duration of the LOB data internally, taking into account your hint. You do not have to specify the duration of the LOB data!


DECLARE
   Dest_loc       BLOB;
   Src_loc        BLOB;
   Amount         INTEGER := 4000;
BEGIN
   SELECT ad_composite INTO Src_loc FROM Print_media 
       WHERE product_id = 3060 AND ad_id = 11001;
       
    /* Create a temporary LOB: */
   DBMS_LOB.CREATETEMPORARY(Dest_loc,TRUE);
   
   /* Copy the entire object from the Src_loc to the Temporary Lob: */
   DBMS_LOB.COPY(Dest_loc,Src_loc,DBMS_LOB.GETLENGTH(Src_loc),1,1);
   DBMS_LOB.FREETEMPORARY(Dest_loc);
END;

C (OCI): Creating a Temporary LOB

/* Creating a temporary LOB using C(OCI). [Example script: 3820.c]
  This function reads in one of the composite ads, ad_composite, 
  from table Print_media.It creates a temporary LOB so that you can use the
  temporary LOB to convert the image from one format to another, say JPG to 
GIFF.
  The Temporary LOB created is read through the CACHE, and is automatically 
cleaned 
  up after the your session, if it is not explicitly freed sooner. 
  This function returns 0 if it completes successfully, and -1 if it fails: */
  
sb4 select_and_createtemp (OCILobLocator *lob_loc, 
                           OCIError      *errhp, 
                           OCISvcCtx     *svchp,
                           OCIStmt       *stmthp,
                           OCIEnv        *envhp) 
{
  OCIDefine     *defnp1, *defnp2;
  OCIBind       *bndhp;
  text          *sqlstmt;
  int rowind =1;
  ub4 loblen = 0;
  OCILobLocator *tblob;
  printf ("in select_and_createtemp \n");
  if(OCIDescriptorAlloc((dvoid*)envhp, (dvoid **)&tblob,
                        (ub4)OCI_DTYPE_LOB, (size_t)0, (dvoid**)0))
  {
    printf("failed in OCIDescriptor Alloc in select_and_createtemp \n");
    return -1;
  }

  /* Arbitrarily select where product_id =3060: */
  sqlstmt = (text *)
     "SELECT ad_composite FROM Print_media 
         WHERE product_id = 3060 AND ad_id = 11001 FOR UPDATE";

  if (OCIStmtPrepare(stmthp, errhp, sqlstmt, 
                     (ub4) strlen((char *)sqlstmt),
                     (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT))
  {
      (void) printf("FAILED: OCIStmtPrepare() sqlstmt\n");
      return -1;
  }

  /* Define for BLOB: */
  if (OCIDefineByPos(stmthp, 
             &defnp1, errhp, (ub4) 1, (dvoid *) &lob_loc, (sb4)0, 
             (ub2) SQLT_BLOB, (dvoid *) 0, (ub2 *) 0, 
             (ub2 *) 0, (ub4) OCI_DEFAULT)
     || &defnp2, errhp, (ub4) 2, (dvoid *) &lob_loc, (sb4)0, 
             (ub2) SQLT_BLOB, (dvoid *) 0, (ub2 *) 0, 
             (ub2 *) 0, (ub4) OCI_DEFAULT))
  {
    (void) printf("FAILED: Select locator: OCIDefineByPos()\n");
    return -1;
  }
  /* Execute the select and fetch one row: */
  if (OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
                     (CONST OCISnapshot*) 0, (OCISnapshot*) 0,
                     (ub4) OCI_DEFAULT))
  {
    (void) printf("FAILED: OCIStmtExecute() sqlstmt\n");
    return -1;
  }
  if(OCILobCreateTemporary(svchp,
                            errhp, tblob, (ub2)0, SQLCS_IMPLICIT, 
                            OCI_TEMP_BLOB, OCI_ATTR_NOCACHE, 
                            OCI_DURATION_SESSION))
  {
    (void) printf("FAILED: CreateTemporary() \n");
    return -1;
  }

  if (OCILobGetLength(svchp, errhp, lob_loc, &loblen) != 0)
  {
    printf("OCILobGetLength FAILED\n");
    return -1;
  }
  if (OCILobCopy(svchp, errhp, tblob,lob_loc,(ub4)loblen, (ub4) 1, (ub4) 1))
  {
    printf( "OCILobCopy FAILED \n");
  }   
  if(OCILobFreeTemporary(svchp,errhp,tblob))
  {
    printf ("FAILED: OCILobFreeTemporary call \n");
    return -1;
  }
    
  return 0;
  }

COBOL (Pro*COBOL): Creating a Temporary LOB

     * Creating a Temporary LOB  [example script: 3821.pco]
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CREATE-TEMPORARY.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  USERID   PIC X(11) VALUES "SAMP/SAMP".
       01  BLOB1          SQL-BLOB.
       01  TEMP-BLOB      SQL-BLOB.
       01  LEN            PIC S9(9) COMP.
       01  D-LEN          PIC 9(9).
       01  ORASLNRD       PIC 9(4).

           EXEC SQL INCLUDE SQLCA END-EXEC.
           EXEC ORACLE OPTION (ORACA=YES) END-EXEC.
           EXEC SQL INCLUDE ORACA END-EXEC.
       PROCEDURE DIVISION.
       CREATE-TEMPORARY.
           EXEC SQL WHENEVER SQLERROR DO PERFORM SQL-ERROR END-EXEC.
           EXEC SQL
                CONNECT :USERID
           END-EXEC.

      * Allocate and initialize the CLOB locators: 
           EXEC SQL ALLOCATE :BLOB1 END-EXEC.
           EXEC SQL ALLOCATE :TEMP-BLOB END-EXEC.
           EXEC SQL 
                LOB CREATE TEMPORARY :TEMP-BLOB
           END-EXEC.

           EXEC SQL WHENEVER NOT FOUND GOTO END-OF-BLOB END-EXEC.
           EXEC ORACLE OPTION (SELECT_ERROR=NO) END-EXEC.
           EXEC SQL 
                SELECT AD_COMPOSITE INTO :BLOB1
                FROM PRINT_MEDIA 
                WHERE PRODUCT_ID = 3060 AND AD_ID = 11001
           END-EXEC.
 
      * Get the length of the persistent BLOB: 
           EXEC SQL 
                LOB DESCRIBE :BLOB1
                GET LENGTH INTO :LEN
           END-EXEC.
     
      * Copy the entire length from persistent to temporary: 
           EXEC SQL 
                LOB COPY :LEN FROM :BLOB1 TO :TEMP-BLOB
           END-EXEC.

      * Free the temporary LOB: 
           EXEC SQL 
                LOB FREE TEMPORARY :TEMP-BLOB
           END-EXEC.

       END-OF-BLOB.
           EXEC SQL WHENEVER NOT FOUND CONTINUE END-EXEC.
           EXEC SQL FREE :BLOB1 END-EXEC.
           EXEC SQL FREE :TEMP-BLOB END-EXEC.
           STOP RUN.

       SQL-ERROR.
           EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
           MOVE ORASLNR TO ORASLNRD.
           DISPLAY " ".
           DISPLAY "ORACLE ERROR DETECTED ON LINE ", ORASLNRD, ":".
           DISPLAY " ".
           DISPLAY SQLERRMC.
           EXEC SQL ROLLBACK WORK RELEASE END-EXEC.
           STOP RUN.

C/C++ (Pro*C/C++): Creating a Temporary LOB

/* Creating a temporary LOB  [example script #: 3822.c] */
#include <oci.h>
#include <stdio.h>
#include <sqlca.h>

void Sample_Error()
{
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);
}

void createTempLOB_proc()
{
  OCIBlobLocator *Lob_loc, *Temp_loc;
  int Amount;

  EXEC SQL WHENEVER SQLERROR DO Sample_Error();
  /* Allocate the LOB Locators: */
  EXEC SQL ALLOCATE :Lob_loc;
  EXEC SQL ALLOCATE :Temp_loc;

  /* Create the Temporary LOB: */
  EXEC SQL LOB CREATE TEMPORARY :Temp_loc;
  EXEC SQL SELECT ad_composite INTO :Lob_loc FROM Print_media 
       WHERE product_ID = 3060 AND ad_id = 111001;

  /* Copy the full length of the source LOB into the Temporary LOB: */
  EXEC SQL LOB DESCRIBE :Lob_loc GET LENGTH INTO :Amount;
  EXEC SQL LOB COPY :Amount FROM :Lob_loc TO :Temp_loc;

  /* Free the Temporary LOB: */
  EXEC SQL LOB FREE TEMPORARY :Temp_loc;

  /* Release resources held by the Locators: */
  EXEC SQL FREE :Lob_loc;
  EXEC SQL FREE :Temp_loc;
}

void main()
{
  char *samp = "samp/samp";
  EXEC SQL CONNECT :samp;
  createTempLOB_proc();
  EXEC SQL ROLLBACK WORK RELEASE;
}

Java (JDBC): Creating a Temporary BLOB

To create a temporary BLOB, the JDBC application can use the createTemporary static method defined in oracle.sql.BLOB class as follows:

/** 
 * Create a temporary blob. 
 * 
 * @param cache Specifies if LOB should be read into buffer cache or not. 
 * @param duration The duration of the temporary LOB. The following are 
 *                         valid values: DURATION_SESSION, DURATION_CALL. 
 * @return A temporary blob. 
 * @since 8.2.0 
 */ 
public static BLOB createTemporary (Connection conn, boolean cache, int 
duration)          throws SQLException

Possible values of the duration parameter are:

public static final int DURATION_SESSION

public static final int DURATION_CALL

Each call to createTemporary returns a temporary BLOB. For example,

// Make a JDBC connection 
   Connection conn = ... 

// Create a temporary BLOB 
   BLOB temporaryBlob = BLOB.createTemporary (conn, true, 
        BLOB.DURATION_SESSION);

The new createTemporary API replaces previous workarounds using DBMS_LOB.createtemporary() in the DBMS_LOB PL/SQL package.

Java (JDBC): Creating a Temporary CLOB

To create a temporary CLOB, the JDBC application can use the createTemporary static method defined in oracle.sql.CLOB class as follows:

/** 
* Create a temporary clob. 
* 
* @param cache Specifies if LOB should be read into buffer cache or not. 
* @param duration The duration of the temporary LOB. The following are 
*                            valid values: DURATION_SESSION, DURATION_CALL. 
* @return A temporary clob. 
*/ 
public static CLOB createTemporary (Connection conn, boolean cache, int 
duration) 
throws SQLException

The possible values of the duration parameter are --

public static final int DURATION_SESSION 
public static final int DURATION_CALL 

Each call to createTemporary returns a temporary CLOB. For example,

// Make a JDBC connection 
   Connection conn = ... 
// Create a temporary CLOB 
   CLOB temporaryClob = CLOB.createTemporary (conn, true, CLOB.DURATION_
SESSION);

The new createTemporary API should replace the previous workaround of using dbms_lob.createtemporary() in dbms_lob PL/SQL package.

Checking If a LOB is Temporary

Figure 11-2 Use Case Diagram: Checking If a LOB is Temporary

Text description of adlob100.gif follows
Text description of the illustration adlob100.gif


See Also:

Table 11-1, "Use Case Model Overview: Internal Temporary LOBs".

Purpose

This procedure describes how to see if a LOB is temporary.

Usage Notes

Not applicable.

Syntax

See Chapter 3, "LOB Support in Different Programmatic Environments" for a list of available functions in each programmatic environment. Use the following syntax references for each programmatic environment:

Scenario

These are generic examples that query whether the locator is associated with a temporary LOB or not.

Examples

Examples are provided in the following programmatic environments:

PL/SQL (DBMS_LOB Package): Checking If a LOB is Temporary

/* Checking if a LOB is temporary. [Example script: 3828.sql]
   This is an example of freeing a temporary LOB. First test to make
   sure that the LOB locator points to a temporary LOB, then free it.
   Otherwise, issue an error: */

CREATE or REPLACE PROCEDURE freeTempLob_proc(Lob_loc IN OUT BLOB) IS
BEGIN
   /* Free the temporary LOB locator passed in. */
   /* First check to make sure that the locator is pointing to a temporary 
      LOB:*/
    IF DBMS_LOB.ISTEMPORARY(Lob_loc) = 1 THEN
       /* Free the temporary LOB locator: */
        DBMS_LOB.FREETEMPORARY(Lob_loc);
        DBMS_OUTPUT.PUT_LINE(' temporary LOB was freed');
    ELSE
        /* Print an error: */
        DBMS_OUTPUT.PUT_LINE(
           'Locator passed in was not a temporary LOB locator');
    END IF;
END;

C (OCI): Checking If a LOB is Temporary

/* Checking if a LOB is temporary.  [Example script: 3829.c]
   This function frees a temporary LOB. It takes a locator as an argument,   
   checks to see if it is a temporary LOB. If it is, the function frees 
   the temporary LOB. Otherwise, it prints out a message saying the locator 
   was not a temporary LOB locator. This function returns 0 if it 
   completes successfully, -1 otherwise: */ 

sb4 check_and_free_temp(OCILobLocator *tblob,
                        OCIError      *errhp, 
                        OCISvcCtx     *svchp,
                        OCIStmt       *stmthp, 
                        OCIEnv        *envhp)
{
  boolean is_temp;
  is_temp = FALSE;
 
  if (OCILobIsTemporary(envhp, errhp, tblob, &is_temp))
  {
    printf ("FAILED: OCILobIsTemporary call\n");
    return -1;
  }
  if(is_temp)
  {
      if(OCILobFreeTemporary(svchp, errhp, tblob))
      {
          printf ("FAILED: OCILobFreeTemporary call\n");
          return -1;
 
      }else
      {
          printf("Temporary LOB freed\n");
      }
  }else
  {
      printf("locator is not a temporary LOB locator\n");
  }
  return 0;
  }

COBOL (Pro*COBOL): Checking If a LOB is Temporary


     * Checking if a LOB is temporary  [Example script: 3830.pco]
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEMP-LOB-ISTEMP.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  USERID   PIC X(11) VALUES "SAMP/SAMP".
       01  TEMP-BLOB      SQL-BLOB.
       01  IS-TEMP        PIC S9(9) COMP.
       01  ORASLNRD       PIC 9(4).

           EXEC SQL INCLUDE SQLCA END-EXEC.
           EXEC ORACLE OPTION (ORACA=YES) END-EXEC.
           EXEC SQL INCLUDE ORACA END-EXEC.

       PROCEDURE DIVISION.
       CREATE-TEMPORARY.
           EXEC SQL WHENEVER SQLERROR DO PERFORM SQL-ERROR END-EXEC.
           EXEC SQL
                CONNECT :USERID
           END-EXEC.

      * Allocate and initialize the BLOB locators: 
           EXEC SQL ALLOCATE :TEMP-BLOB END-EXEC.
           EXEC SQL 
                LOB CREATE TEMPORARY :TEMP-BLOB
           END-EXEC.

      * Check if the LOB is temporary: 
           EXEC SQL 
                LOB DESCRIBE :TEMP-BLOB
                GET ISTEMPORARY INTO :IS-TEMP
           END-EXEC.
     
           IF IS-TEMP = 1
      *      Logic for a temporary LOB goes here
             DISPLAY "LOB is temporary."
           ELSE
      *      Logic for a persistent LOB goes here.
             DISPLAY "LOB is persistent."
           END-IF.

           EXEC SQL 
                LOB FREE TEMPORARY :TEMP-BLOB 
           END-EXEC.
           EXEC SQL FREE :TEMP-BLOB END-EXEC.
           STOP RUN.

       SQL-ERROR.
           EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
           MOVE ORASLNR TO ORASLNRD.
           DISPLAY " ".
           DISPLAY "ORACLE ERROR DETECTED ON LINE ", ORASLNRD, ":".
           DISPLAY " ".
           DISPLAY SQLERRMC.
           EXEC SQL ROLLBACK WORK RELEASE END-EXEC.
           STOP RUN.

C/C++ (Pro*C/C++): Checking If a LOB is Temporary

/* Checking if a LOB is temporary  [Example script: 3831.pc]
#include <oci.h>
#include <stdio.h>
#include <sqlca.h>

void Sample_Error()
{
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);
}

void lobIsTemp_proc()
{
  OCIBlobLocator *Temp_loc;
  int isTemporary = 0;

  EXEC SQL WHENEVER SQLERROR DO Sample_Error();
  /* Allocate and Create the Temporary LOB: */
  EXEC SQL ALLOCATE :Temp_loc;
  EXEC SQL LOB CREATE TEMPORARY :Temp_loc;
  /* Determine if the Locator is a Temporary LOB Locator: */
  EXEC SQL LOB DESCRIBE :Temp_loc GET ISTEMPORARY INTO :isTemporary;
   
  /* Note that in this example, isTemporary should be 1 (TRUE) */
  if (isTemporary)
    printf("Locator is a Temporary LOB locator\n");
  /* Free the Temporary LOB: */
  EXEC SQL LOB FREE TEMPORARY :Temp_loc;
  /* Release resources held by the Locator: */
  EXEC SQL FREE :Temp_loc;
else
    printf("Locator is not a Temporary LOB locator \n");
   }

void main()
{
  char *samp = "samp/samp";
  EXEC SQL CONNECT :samp;
  lobIsTemp_proc();
  EXEC SQL ROLLBACK WORK RELEASE;
}

Java (JDBC): Checking if a BLOB is Temporary

To see if a BLOB is temporary, the JDBC application can either use the isTemporary instance method to determine whether the current BLOB object is temporary, or pass the BLOB object to the static isTemporary method to determine whether the specified BLOB object is temporary. These two methods are defined as follows:

/** 
* Checking if a BLOB is temporary. [Example script: 3833.java]
* Returns true if LOB locator points to a temporary BLOB, False if not. 
* @param lob the BLOB to test. 
* @returns true if LOB locator points to a temporary BLOB, False if not. 
*/ 
  public static boolean isTemporary (BLOB lob) throws SQLException 

/** 
 * Returns true if LOB locator points to a temporary BLOB, False if not. 
 * @returns true if LOB locator points to a temporary BLOB, False if not. 
 */ 
   public boolean isTemporary () throws SQLException

  //The usage example is--

  BLOB blob = ... 

  // See if the BLOB is temporary 
  boolean isTemporary = blob.isTemporary (); 

  // See if the specified BLOB is temporary 
  boolean isTemporary2 = BLOB.isTemporary(blob);

This JDBC API replaces previous workarounds that use
DBMS_LOB.isTemporary().

Java (JDBC): Checking if a CLOB is Temporary

To see if a CLOB is temporary, the JDBC application can either use the isTemporary instance method to determine whether the current CLOB object is temporary, or pass the CLOB object to the static isTemporary method to determine whether the specified CLOB object is temporary. These two methods are defined as follows:

/** 
 * Checking if LOB is temporary  [Example script: 3834.java]
 * Return true if the LOB locator points to a temporary CLOB, False if it 
 * does not. 
 * 
 * @param lob the BLOB to test. 
 * @return true if the LOB locator points to a temporary CLOB, False if it 
 *         does not. 
 */ 
  public static boolean isTemporary (CLOB lob) throws SQLException 

  /** 
   * Return true if the LOB locator points to a temporary CLOB, False if it 
   * does not. 
   * 
   * @return true if the LOB locator points to a temporary CLOB, False if it 
   *         does not. 
   */ 
    public boolean isTemporary () throws SQLException 

  //The usage example is--

  CLOB clob = ... 

  // See if the CLOB is temporary 
  boolean isTemporary = clob.isTemporary (); 

  // See if the specified CLOB is temporary 
  boolean isTemporary2 = CLOB.isTemporary(clob);

This API replaces previous workarounds that used DBMS_LOB.istemporary().

Freeing a Temporary LOB

Figure 11-3 Use Case Diagram: Freeing a Temporary LOB

Text description of adlob024.gif follows
Text description of the illustration adlob024.gif


See Also:

Table 11-1, "Use Case Model Overview: Internal Temporary LOBs".

Purpose

This procedure describes how to free a temporary LOB.

Usage Notes

A temporary LOB instance can only be destroyed for example, in OCI or the DBMS_LOB package by using the appropriate FREETEMPORARY or OCIDurationEnd or OCILOBFreeTemporary statements.

To make a temporary LOB permanent, the user must explicitly use the OCI or DBMS_LOB copy() command and copy the temporary LOB into a permanent one.

Syntax

See Chapter 3, "LOB Support in Different Programmatic Environments" for a list of available functions in each programmatic environment.

Scenario

Not applicable.

Examples

Examples are provided in the following programmatic environments:

PL/SQL (DBMS_LOB Package): Freeing a Temporary LOB

/*  Freeing a temporary LOB  [Example script: 3836.sql]
DECLARE
   Dest_loc       BLOB;
   Src_loc        BFILE := BFILENAME('ADPHOTO_DIR', 'monitor_3060_11001');
   Amount         INTEGER := 4000;
BEGIN
   DBMS_LOB.CREATETEMPORARY(Dest_loc,TRUE);
   /* Opening the BFILE is mandatory: */
   DBMS_LOB.OPEN(Src_loc, DBMS_LOB.LOB_READONLY);
   
   /* Opening the LOB is optional: */
   DBMS_LOB.OPEN(Dest_loc,DBMS_LOB.LOB_READWRITE);
   DBMS_LOB.LOADFROMFILE(Dest_loc, Src_loc, Amount);
   
   /* Closing the LOB is mandatory if you have opened it: */
   DBMS_LOB.CLOSE(Src_loc);
   DBMS_LOB.CLOSE(Dest_loc);
   /* Free the temporary LOB: */
   DBMS_LOB.FREETEMPORARY(Dest_loc);
END;

C (OCI): Freeing a Temporary LOB

/* Freeing a temporary LOB.  [Example script: 3837.c]
   This function creates a temporary LOB and then frees it: 
   This function returns 0 if it completes successfully, -1 otherwise:  */ 

sb4 freeTempLob(OCIError      *errhp, 
                OCISvcCtx     *svchp,
                OCIStmt       *stmthp, 
                OCIEnv        *envhp)
{
  OCILobLocator *tblob;
  checkerr (errhp,OCIDescriptorAlloc((dvoid*)envhp, (dvoid **)&tblob,
                                     (ub4)OCI_DTYPE_LOB, (size_t)0,
                                     (dvoid**)0));
  if(OCILobCreateTemporary(svchp,errhp,tblob,(ub2)0,SQLCS_IMPLICIT,
                           OCI_TEMP_BLOB, OCI_ATTR_NOCACHE,
                           OCI_DURATION_SESSION))
  {
      (void) printf("FAILED:CreateTemporary():freeTempLob\n");
      return -1;
  }

  if(OCILobFreeTemporary(svchp,errhp,tblob))
  {
      printf ("FAILED: OCILobFreeTemporary call in freeTempLob\n");
      return -1;

  }else
  {
   printf("Temporary LOB freed in freeTempLob\n");
  }
  return 0;
 }

COBOL (Pro*COBOL): Freeing a Temporary LOB

     * Freeing a temporary LOB  [Example script: 3838.pco]
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FREE-TEMPORARY.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  USERID   PIC X(11) VALUES "SAMP/SAMP".
        
       01  TEMP-BLOB      SQL-BLOB.
       01  IS-TEMP        PIC S9(9) COMP.
       01  ORASLNRD        PIC 9(4).
           EXEC SQL INCLUDE SQLCA END-EXEC.
           EXEC ORACLE OPTION (ORACA=YES) END-EXEC.
           EXEC SQL INCLUDE ORACA END-EXEC.

       PROCEDURE DIVISION.
       FREE-TEMPORARY.

           EXEC SQL WHENEVER SQLERROR DO PERFORM SQL-ERROR END-EXEC.
           EXEC SQL
                CONNECT :USERID
           END-EXEC.

      * Allocate and initialize the BLOB locators: 
           EXEC SQL ALLOCATE :TEMP-BLOB END-EXEC.
           EXEC SQL 
                LOB CREATE TEMPORARY :TEMP-BLOB
           END-EXEC.

      * Do something with the temporary LOB here: 

      * Free the temporary LOB:
           EXEC SQL 
                LOB FREE TEMPORARY :TEMP-BLOB 
           END-EXEC.
           EXEC SQL FREE :TEMP-BLOB END-EXEC.
           STOP RUN.

       SQL-ERROR.
           EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
           MOVE ORASLNR TO ORASLNRD.
           DISPLAY " ".
           DISPLAY "ORACLE ERROR DETECTED ON LINE ", ORASLNRD, ":".
           DISPLAY " ".
           DISPLAY SQLERRMC.
           EXEC SQL ROLLBACK WORK RELEASE END-EXEC.
           STOP RUN.

C/C++ (Pro*C/C++): Freeing a Temporary LOB

/* Freeing a temporary LOB. [Example script: 3839.pc]
#include <oci.h>
#include <stdio.h>
#include <sqlca.h>

void Sample_Error()
{
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);
}

void freeTempLob_proc()
{
  OCIBlobLocator *Temp_loc;

  EXEC SQL WHENEVER SQLERROR DO Sample_Error();
  EXEC SQL ALLOCATE :Temp_loc;
  EXEC SQL LOB CREATE TEMPORARY :Temp_loc;
  /* Do something with the Temporary LOB: */
  EXEC SQL LOB FREE TEMPORARY :Temp_loc;
  EXEC SQL FREE :Temp_loc;
}

void main()
{
  char *samp = "samp/samp";
  EXEC SQL CONNECT :samp;
  freeTempLob_proc();
  EXEC SQL ROLLBACK WORK RELEASE;
}

Java (JDBC): Freeing a Temporary BLOB

To free a temporary BLOB, the JDBC application can either use the freeTemporary instance method to free the current BLOB object, or pass the temporary BLOB to be freed to the static freeTemporary method to free the specified temporary BLOB. These two methods are defined as follows:

/** 
* Freeing a temporary BLOB.  [Example script:  3840.java]
* This example frees the contents and locator of a temporary BLOB. 
* @param temp_lob A temporary BLOB to be freed. 
* @exception SQLException if temp_lob is a permanent LOB or temp_lob has already 
* been freed. 
*/ 

public static void freeTemporary (BLOB temp_lob) throws SQLException 

/** 
* Free the contents and the locator of the temporary BLOB. 
* @exception SQLException if self is a permanent LOB or self has already been 
freed. 
*/ 

public void freeTemporary() throws SQLException

/**
* The usage example example is -- 
* BLOB tempBlob1 = ... 
* BLOB tempBlob2 = ... 
* // free the temporary BLOB 
* tempBlob1.freeTemporary (); 
* // free the specified temporary BLOB 
* BLOB.freeTemporary(tempBlob2);
*
* The newer freeTemporary APIs should replace previous workaround of 
* using dbms_lob.freeTemporary() in dbms_lob PL/SQL package. 
*/

The freeTemporary APIs should replace the previous workaround of using dbms_lob.freeTemporary() in dbms_lob PL/SQL package.

Java (JDBC): Freeing a Temporary CLOB

To free a temporary CLOB, the JDBC application can either use the freeTemporary instance method to free the current CLOB object, or pass the temporary CLOB to be freed to the static freeTemporary method to free the specified temporary CLOB. These two methods are defined as follows:

/** 
* Freeing a temporary CLOB.  [Example script: 3841.java]
* Free the contents and the locator of the temporary blob. 
* @param temp_lob A temporary CLOB to be freed. 
* @since 8.2.0 
* @exception SQLException if temp_lob is a permanent LOB or temp_lob has 
*            already been freed. 
*/ 

public static void freeTemporary (CLOB temp_lob) throws SQLException 

/** 
* Free the contents and the locator of the temporary CLOB. 
* 
* @since 8.2.0 
* @exception SQLException if self is a permanent lob or self has 
*            already been freed. 
*/ 
public void freeTemporary() throws SQLException


/**
* Use the free temporary CLOB API as follows:
* CLOB tempClob1 = ... 
* CLOB tempClob2 = ... 
* // free the temporary CLOB 
* tempClob1.freeTemporary (); 
* // free the specified temporary CLOB 
* CLOB.freeTemporary(tempClob2);
*
*The freeTemporary API replaces previous workarounds that use DBMS_
LOB.freetemporary().
*/

The freeTemporary API replaces previous workarounds that use DBMS_LOB.freetemporary().

Java (JDBC): Creating and Freeing a Temporary CLOB Using TemporaryClob.java


Note:

temporaryClob.java and temporaryBlob.java classes have been deprecated. These classes are no longer needed or used in the JDBC standard.


Loading a Temporary LOB with Data from a BFILE

Figure 11-4 Use Case Diagram: Loading a LOB with Data from a BFILE

Text description of adlob083.gif follows
Text description of the illustration adlob083.gif


See Also:

Purpose

This procedure describes how to load a temporary LOB with data from a BFILE.

Usage Notes


Note:

The LOADBLOBFROMFILE and LOADCLOBFROMFILE procedures implement the functionality of this procedure and provide improved features for loading binary data and character data. The improved procedures are available in the PL/SQL environment only. When possible, using one of the improved procedures is recommended. See "Loading a Temporary BLOB with Binary Data from a BFILE" and "Loading a Temporary CLOB/NCLOB with Character Data from a File" for more information.


Binary Data to Character Set Conversion is Needed on BFILE Data

In using OCI, or any of the programmatic environments that access OCI functionality, character set conversions are implicitly performed when translating from one character set to another. When you use the DBMS_LOB.LOADFROMFILE procedure to populate a CLOB or NCLOB, you are populating the LOB with binary data from the BFILE. No implicit translation is performed from binary data to a character set. For this reason, you should use the LOADCLOBFROMFILE procedure when loading text (see Loading a Temporary CLOB/NCLOB with Character Data from a File on).

Syntax

See Chapter 3, "LOB Support in Different Programmatic Environments" for a list of available functions in each programmatic environment. Use the following syntax references for each programmatic environment: