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

External LOBs (BFILEs), 39 of 41


Closing a BFILE with CLOSE

Figure 12-35 Use Case Diagram: Closing an Open BFILE with CLOSE


Text description of adl12b13.gif follows This link takes you back to the External LOBs (BFILES) main diagram.
Text description of the illustration adl12b13.gif

Purpose

This procedure describes how to close a BFILE with CLOSE.

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

This example should be read in conjunction with the example of opening a BFILE -- in this case, closing the BFILE associated with Lincoln_photo.

Examples

PL/SQL (DBMS_LOB Package): Closing a BFile with CLOSE

/* Note that the example procedure closeBFILE_procTwo is not part of the 
   DBMS_LOB package: */
CREATE OR REPLACE PROCEDURE closeBFILE_procTwo IS
   File_loc    BFILE := BFILENAME('PHOTO_DIR', 'Lincoln_photo');
BEGIN
   DBMS_LOB.OPEN(File_loc, DBMS_LOB.LOB_READONLY);
   /* ...Do some processing. */
   DBMS_LOB.CLOSE(File_loc);
END;

C (OCI): Closing a BFile with CLOSE

void BfileClose(envhp, errhp, svchp, stmthp) 
OCIEnv       *envhp; 
OCIError     *errhp; 
OCISvcCtx    *svchp; 
OCIStmt      *stmthp; 
{ 

   OCILobLocator *bfile_loc; 
 
   /* Allocate the locator descriptors */ 
   (void) OCIDescriptorAlloc((dvoid *) envhp, (dvoid **) &bfile_loc,
                             (ub4) OCI_DTYPE_FILE,  
                             (size_t) 0, (dvoid **) 0);

   checkerr(errhp, OCILobFileSetName(envhp, errhp, &bfile_loc,
                        (OraText *) "PHOTO_DIR", (ub2) strlen("PHOTO_DIR"),
                        (OraText *) "Lincoln_photo",
                        (ub2) strlen("Lincoln_photo")));

   checkerr(errhp, OCILobOpen(svchp, errhp, bfile_loc,
                                          (ub1) OCI_LOB_READONLY));

   checkerr(errhp, OCILobClose(svchp, errhp, bfile_loc));

   /* Free the locator descriptor */ 
   OCIDescriptorFree((dvoid *)bfile_loc, (ub4)OCI_DTYPE_FILE); 
}

COBOL (Pro*COBOL): Closing a BFILE with CLOSE

This script is also provided in:

$ORACLE_HOME/rdbms/demo/lobs/cobol/fclose

       IDENTIFICATION DIVISION.
       PROGRAM-ID. BFILE-CLOSE.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  USERID   PIC X(11) VALUES "SAMP/SAMP".
       01  BFILE1         SQL-BFILE.
       01  DIR-ALIAS      PIC X(30) VARYING.
       01  FNAME          PIC X(20) VARYING.
       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.
       BFILE-CLOSE.

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

      * Allocate and initialize the BFILE locators: 
           EXEC SQL ALLOCATE :BFILE1 END-EXEC.

      * Set up the directory and file information: 
           MOVE "PHOTO_DIR" TO DIR-ALIAS-ARR.
           MOVE 9 TO DIR-ALIAS-LEN.
           MOVE "Lincoln_photo" TO FNAME-ARR.
           MOVE 13 TO FNAME-LEN.
 
           EXEC SQL 
                LOB FILE SET :BFILE1
                DIRECTORY = :DIR-ALIAS, FILENAME = :FNAME END-EXEC.

           EXEC SQL
                LOB OPEN :BFILE1 READ ONLY END-EXEC.

      * Close the LOB: 
           EXEC SQL LOB CLOSE :BFILE1 END-EXEC.

      * And free the LOB locator: 
           EXEC SQL FREE :BFILE1 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++): Closing a BFile with CLOSE

This script is also provided in:

$ORACLE_HOME/rdbms/demo/lobs/proc/fclose

/* Pro*C/C++ has only one form of CLOSE for BFILEs.  Pro*C/C++ has no
   FILE CLOSE statement.  A simple CLOSE statement is used instead: */

#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 closeBFILE_proc()
{
  OCIBFileLocator *Lob_loc;
  char *Dir = "PHOTO_DIR", *Name = "Lincoln_photo";

  EXEC SQL WHENEVER SQLERROR DO Sample_Error();
  EXEC SQL ALLOCATE :Lob_loc;
  EXEC SQL LOB FILE SET :Lob_loc DIRECTORY = :Dir, FILENAME = :Name;
  EXEC SQL LOB OPEN :Lob_loc READ ONLY;
  /* ... Do some processing */
  EXEC SQL LOB CLOSE :Lob_loc;
  EXEC SQL FREE :Lob_loc;
}

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

Visual Basic (OO4O): Closing a BFile with CLOSE

Dim MySession As OraSession
Dim OraDb As OraDatabase

Dim OraDyn As OraDynaset, OraMusic As OraBfile, amount_read%, chunksize%, chunk

Set MySession = CreateObject("OracleInProcServer.XOraSession")
Set OraDb = MySession.OpenDatabase("exampledb", "scott/tiger", 0&)

chunksize = 32767
Set OraDyn = OraDb.CreateDynaset("select * from Multimedia_tab", ORADYN_DEFAULT)
Set OraMusic = OraDyn.Fields("Music").Value
 
If OraMusic.IsOpen Then
   'Processing given that the file is already open
   OraMusic.Close
End If
 

Java (JDBC): Closing a BFile with CLOSE

import java.io.InputStream;
import java.io.OutputStream;

// Core JDBC classes: 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// Oracle Specific JDBC classes: 
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class Ex4_48
{
  public static void main (String args [])
       throws Exception
  {
    // Load the Oracle JDBC driver: 
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    // Connect to the database: 
    Connection conn =
      DriverManager.getConnection ("jdbc:oracle:oci8:@", "samp", "samp");

    conn.setAutoCommit (false);

    // Create a Statement: 
    Statement stmt = conn.createStatement ();
    try
    {
     BFILE src_lob = null;
       ResultSet rset = null;

       rset = stmt.executeQuery (
       "SELECT BFILENAME('PHOTO_DIR', 'Lincoln_photo') FROM DUAL");
       OracleCallableStatement cstmt = null;
       if (rset.next())
       {
          src_lob = ((OracleResultSet)rset).getBFILE (1);
	  cstmt = (OracleCallableStatement)conn.prepareCall 
	       ("begin dbms_lob.open (?,dbms_lob.lob_readonly); end;");
          cstmt.registerOutParameter(1,OracleTypes.BFILE);
          cstmt.setBFILE (1, src_lob);
          cstmt.execute();
          src_lob = cstmt.getBFILE(1);
          System.out.println ("the file is now open");
       }

       // Close the BFILE, statement and connection: 
       cstmt = (OracleCallableStatement)
       conn.prepareCall ("begin dbms_lob.close(?); end;");
       cstmt.setBFILE(1,src_lob);
       cstmt.execute();
       stmt.close();
       conn.commit();
       conn.close();
    }
    catch (SQLException e)
    {
       e.printStackTrace();
    }
  }
}

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