Application Express User's Guide > Advanced Programming Techni... > Running Background PL/SQL > Using a Process to Implemen...
Using a Process to Implement Background PL/SQL |
Previous |
Next |
The simplest way to implement the HTMLDB_PLSQL_JOB
package is to create a page process that specifies the process type PLSQL DBMS JOB
. By selecting this process type, Application Builder will submit the PL/SQL code you specify as a job. Because you are not calling the function directly, you can use the APP_JOB
built-in substitution item to determine the job number of any jobs you submit.
The following example runs a PL/SQL job in the background for testing and explanation:
001 BEGIN 002 FOR i IN 1 .. 100 LOOP 003 INSERT INTO emp(a,b) VALUES (:APP_JOB,i); 004 IF MOD(i,10) = 0 THEN 005 HTMLDB_PLSQL_JOB.UPDATE_JOB_STATUS( 006 P_JOB => :APP_JOB, 007 P_STATUS => i || 'rows inserted'); 008 END IF; 009 HTMLDB_UTIL.PAUSE(2); 010 END LOOP; 011 END;
In this example, note that:
Lines 002 to 010 run a loop that inserts 100 records into the emp
table.
APP_JOB
is referenced as a bind variable inside the VALUES clause of the INSERT, and specified as the P_JOB
parameter value in the call to UPDATE_JOB_STATUS
.
APP_JOB
represents the job number which will be assigned to this process as it is submitted to HTMLDB_PLSQL_JOB
. By specifying this reserved item inside your process code, it will be replaced for you at execution time with the actual job number.
Note that this example calls to UPDATE_JOB_STATUS
every ten records, INSIDE the block of code. Normally, Oracle transaction rules dictate updates made inside code blocks will not be seen until the entire transaction is committed. The HTMLDB_PLSQL_JOB.UPDATE_JOB_STATUS
procedure, however, has been implemented in such a way that the update will happen regardless of whether or not the job succeeds or fails. This last point is important for two reasons:
Even if your status shows "100 rows inserted," it does not mean the entire operation was successful. If an error occurred at the time the block of code tried to commit, the user_status
column of HTMLDB_PLSQL_JOBS
would not be affected because status updates are committed separately.
Updates are performed autonomously. You can view the job status before the job has completed. This gives you the ability to display status text about ongoing operations in the background as they are happening.