05.11.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 6 ■ LOCKING AND LATCHING 195<br />

ops$tkyte@ORA10G> begin<br />

2 for x in ( select deptno, dname, loc<br />

3 from dept<br />

4 where deptno = 10<br />

5 for update nowait )<br />

6 loop<br />

7 if ( hextoraw( 'C44F7052661CE945D385D5C3F911E70FA99407A6' ) <br />

8 dbms_crypto.hash<br />

9 ( utl_raw.cast_to_raw(x.deptno||'/'||x.dname||'/'||x.loc),<br />

10 dbms_crypto.hash_sh1 ) )<br />

11 then<br />

12 raise_application_error(-20001, 'Row was modified' );<br />

13 end if;<br />

14 end loop;<br />

15 update dept<br />

16 set dname = lower(dname)<br />

17 where deptno = 10;<br />

18 commit;<br />

19 end;<br />

20 /<br />

PL/SQL procedure successfully completed.<br />

Upon requerying that data <strong>and</strong> computing the hash again after the update, we can see<br />

that the hash value is very different. If someone had modified the row before we did, our hash<br />

values would not have compared:<br />

ops$tkyte@ORA10G> begin<br />

2 for x in ( select deptno, dname, loc<br />

3 from dept<br />

4 where deptno = 10 )<br />

5 loop<br />

6 dbms_output.put_line( 'Dname: ' || x.dname );<br />

7 dbms_output.put_line( 'Loc: ' || x.loc );<br />

8 dbms_output.put_line( 'Hash: ' ||<br />

9 dbms_crypto.hash<br />

10 ( utl_raw.cast_to_raw(x.deptno||'/'||x.dname||'/'||x.loc),<br />

11 dbms_crypto.hash_sh1 ) );<br />

12 end loop;<br />

13 end;<br />

14 /<br />

Dname: accounting<br />

Loc: NEW YORK<br />

Hash: F3DE485922D44DF598C2CEBC34C27DD2216FB90F<br />

PL/SQL procedure successfully completed.

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

Saved successfully!

Ooh no, something went wrong!