P. 1
phpyoracle

phpyoracle

|Views: 623|Likes:
Published by Dunkel Kruspe

More info:

Categories:Types, Research
Published by: Dunkel Kruspe on Apr 05, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

08/03/2011

pdf

text

original

Sometimes a web page starts a database operation that can run in the background while the user continues
other work.

For example, there might be some database cleanup to be run periodically. Another example is when a
user of a photo site decides to change the name of a tag associated with images. The photo site application
might initiate the name change, but return the user an HTML page saying Your request is being
processed and will soon complete
. The user can continue viewing photos without having to wait
for the renaming process to complete. This technique can improve user satisfaction. It can also free up an
Apache server that would otherwise be blocked, allowing it to be used by another page request.
The DBMS_SCHEDULER package can be used to start background database tasks. It has a lot of
functionality, including allowing tasks to be repeated at intervals, or started when events are received. It
can also be used to invoke operating system programs. In Oracle 9i, the DBMS_JOB package can be used
instead of DBMS_SCHEDULER.
For the photo site example, create some data with the incorrect tag weeding:

Script 62: dschedinit.sql

connect system/systempwd

grant create job to hr;

connect hr/hrpwd

drop table tag_table;

create table tag_table (tag varchar2(20), photo_id number);
insert into tag_table values ('
weeding', 2034);

188

Scheduling Background or Long Running Operations

insert into tag_table values ('weeding', 2035);
insert into tag_table values ('sanfrancisco', 4540);
commit;

To change the tag weeding to wedding, a procedure changetagname() can be created:

Script 63: dbsched.sql

create or replace procedure changetagname(old in varchar2, new in varchar2) as

b number;

begin

for i in 1..100000000 loop b := 1; end loop; -- simulate slow transaction
update tag_table set tag = new where tag = old;
commit;

end;
/
show errors

This script creates a sample table and the procedure to update tags. The procedure is artificially slowed
down to simulate a big, long running database operation.
The following PHP script uses an anonymous block to create a job calling changetagname().

Script 64: dsched.php

$c = oci_connect("hr", "hrpwd", "localhost/XE");

function doquery($c)
{

$s = oci_parse($c, "select tag from tag_table");
oci_execute($s);
oci_fetch_all($s, $res);
var_dump($res);

}

// Schedule a task to change a tag name from 'weeding' to 'wedding'

$stmt =
"begin

dbms_scheduler.create_job(
job_name

=> :jobname,

job_type

=> 'STORED_PROCEDURE',

job_action

=> 'changetagname', // procedure to call

number_of_arguments => 2);
dbms_scheduler.set_job_argument_value (
job_name

=> :jobname,

argument_position

=> 1,

argument_value

=> :oldval);
dbms_scheduler.set_job_argument_value (
job_name

=> :jobname,

argument_position

=> 2,

argument_value

=> :newval);
dbms_scheduler.enable(:jobname);

189

Using PL/SQL With OCI8

end;";

$s = oci_parse($c, $stmt);

$jobname = uniqid('ut');
$oldval = '
weeding';
$newval = '
wedding';
oci_bind_by_name($s, ":jobname", $jobname);
oci_bind_by_name($s, ":oldval", $oldval);
oci_bind_by_name($s, ":newval", $newval);

oci_execute($s);

echo "

Your request is being processed and will soon complete\n";
doquery($c); // gives old results
sleep(10);
echo "Your request has probably completed\n";
doquery($c); // gives new results

?>

The PHP call to the anonymous PL/SQL block returns quickly. The background PL/SQL call to
changetagname() will take several more seconds to complete (because of its for loop), so the first
doquery() output shows the original, incorrect tag values. Then, after PHP has given the job time to
conclude, the second doquery() call shows the updated values:
Your request is being processed and will soon complete
array(1) {
["TAG"]=>
array(3) {
[0]=>
string(7) "
weeding"
[1]=>
string(7) "
weeding"
[2]=>
string(12) "sanfrancisco"

}

}
Your request has probably completed
array(1) {
["TAG"]=>
array(3) {
[0]=>
string(7) "
wedding"
[1]=>
string(7) "
wedding"
[2]=>
string(12) "sanfrancisco"

}

}

190

Reusing Procedures Written for MOD_PLSQL

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->