05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

250<br />

CHAPTER 7 ■ CONCURRENCY AND MULTI-VERSIONING<br />

ops$tkyte@ORA10G> create table t ( x int, y int );<br />

Table created.<br />

ops$tkyte@ORA10G> insert into t values ( 1, 1 );<br />

1 row created.<br />

ops$tkyte@ORA10G> commit;<br />

Commit complete.<br />

To observe the restart, all we need is a trigger to print out some information. We’ll use a<br />

BEFORE UPDATE FOR EACH ROW trigger to simply print out the before <strong>and</strong> after image of the row<br />

as the result of an update:<br />

ops$tkyte@ORA10G> create or replace trigger t_bufer<br />

2 before update on t for each row<br />

3 begin<br />

4 dbms_output.put_line<br />

5 ( 'old.x = ' || :old.x ||<br />

6 ', old.y = ' || :old.y );<br />

7 dbms_output.put_line<br />

8 ( 'new.x = ' || :new.x ||<br />

9 ', new.y = ' || :new.y );<br />

10 end;<br />

11 /<br />

Trigger created.<br />

Now we’ll update that row:<br />

ops$tkyte@ORA10G> set serveroutput on<br />

ops$tkyte@ORA10G> update t set x = x+1;<br />

old.x = 1, old.y = 1<br />

new.x = 2, new.y = 1<br />

1 row updated.<br />

So far, everything is as we expect: the trigger fired once, <strong>and</strong> we see the old <strong>and</strong> new values.<br />

Note that we have not yet committed, however—the row is still locked. In another session,<br />

we’ll execute this update:<br />

ops$tkyte@ORA10G> set serveroutput on<br />

ops$tkyte@ORA10G> update t set x = x+1 where x > 0;<br />

That will immediately block, of course, since the first session has that row locked. If we<br />

now go back to the first session <strong>and</strong> commit, we’ll see this output (the update is repeated for<br />

clarity) in the second session:<br />

ops$tkyte@ORA10G> update t set x = x+1 where x > 0;<br />

old.x = 1, old.y = 1<br />

new.x = 2, new.y = 1<br />

old.x = 2, old.y = 1<br />

new.x = 3, new.y = 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!