Question

I have PL/SQL Block, it query from a Table function and I use a cursor to process it record by record, have some business logic and finally write the Qualified records into the file.

Number of records to be processed is upto 1 Million. And the total processing speed is roughly 10000 records per minute.(After testing with few chunks of data)

Now, that I need to indicate the processing status in a different environment, JSP.

DECLARE
 vSFile   utl_file.file_type;
 vNewLine VARCHAR2(200);
 my_cursor IS SELECT * FROM MYTABLE;
 my_details my_cursor%rowtype;
BEGIN
  vSFile := utl_file.fopen('ORALOAD', file_name,'r');

  IF utl_file.is_open(vSFile) THEN
        utl_file.get_line(vSFile, vNewLine);


        OPEN my_cursor;
        LOOP
               FETCH my_cursor INTO my_details;
               EXIT WHEN sll_cur%NOTFOUND;
               -- Do processing
               utl_file.putf(logfile,'%s ',my_details);
               -- A info tht record completed!
        END LOOP;
        CLOSE logfile;
        CLOSE my_cursor;
  END IF;
EXCEPTION
WHEN OTHERS THEN
    --Error handling
END;
/

The log information written here, is not available until the completion of the process. So, I am unable to track, how far it is completed. Can someone please assist me on this?

Was it helpful?

Solution

To write data to the file you should use the FFLUSH procedure. For instance:

OPEN my_cursor;
   LOOP
      FETCH my_cursor INTO my_details;
      EXIT WHEN sll_cur%NOTFOUND;
      -- Do processing
      utl_file.putf(logfile,'%s ',my_details);
      -- Call the FFLUSH proc here..And contents are available immediately.
      utl_file.FFLUSH(logfile); 
END LOOP;

From the documentation:

FFLUSH physically writes pending data to the file identified by the file handle. Normally, data being written to a file is buffered. The FFLUSH procedure forces the buffered data to be written to the file. The data must be terminated with a newline character.

Flushing is useful when the file must be read while still open. For example, debugging messages can be flushed to the file so that they can be read immediately.

There is more information on UTL_FILE in the Oracle docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top