On Thu, 6 May 2004 22:00:24 +0000 (UTC), JGH <jo******@nospam.tds.net> wrote:
I want to create a web page that allows a user to run an oracle stored
procedure. That's easy enough but the problem is that the procedure
might take several seconds to complete.
I don't want the user to be able to cancel the process. So,
essentially, I want to call the procedure as a background task and have
the web page that the user sees display something like, "Please wait
while I update the database." And then every 10 seconds or so
automatically refresh the page.
So the tricky part, as far as I can tell, is to get it to NOT wait
while the stored procedure is executing.
<?php
$stmt = OCIparse ("BEGIN UPDATEDATABASE(); END;");
OCIexecute ($stmt);
?>
But this just sits there while the procedure executes.
One option would be to submit a job using DBMS_JOB, set to run once
immediately, and have it set some flag that you can query for in the subsequent
refreshes, or query USER_JOBS with the job ID. Something like:
async.php:
----------
<pre>
<?php
function check_oci_error($status, $handle = null) {
if (!$status) {
$error = $handle ? OCIError($handle) : OCIError();
die("error: " . $error['message']);
}
}
check_oci_error($conn = OCILogon('test', 'test', 'dev101'));
check_oci_error($stmt = OCIParse($conn, "
begin
dbms_job.submit(:job_id,
'begin dbms_lock.sleep(25); end;',
sysdate);
end;"), $conn);
OCIBindByName($stmt, ':job_id', &$job_id, 38);
check_oci_error(OCIExecute($stmt, OCI_DEFAULT), $stmt);
check_oci_error(OCICommit($conn));
echo "<a href='waitforjob.php?job_id=$job_id'>Wait for job $job_id</a>";
?>
</pre>
waitforjob.php:
---------------
<pre>
<?php
function check_oci_error($status, $handle = null) {
if (!$status) {
$error = $handle ? OCIError($handle) : OCIError();
die("error: " . $error['message']);
}
}
check_oci_error($conn = OCILogon('test', 'test', 'dev101'));
check_oci_error($stmt = OCIParse($conn, 'select broken from user_jobs where job
= :job_id'));
OCIBindByName($stmt, ':job_id', $_GET['job_id'], -1);
check_oci_error(OCIExecute($stmt, OCI_DEFAULT), $stmt);
if (OCIFetchInto($stmt, $row, OCI_ASSOC)) {
var_dump($row);
if ($row['BROKEN'] == 'Y') {
echo "Job broken.";
} else {
echo "Job still running. (<a
href='waitforjob.php?job_id={$_GET['job_id']}>refresh</a>)";
}
} else {
echo "Job complete.";
echo "<a href='async.php'>submit another</a>";
}
?>
</pre>
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk /
http://www.andyhsoftware.co.uk/space