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.

278<br />

CHAPTER 8 ■ TRANSACTIONS<br />

A question that application developers often pose to me is, “How can I audit every<br />

attempt to modify secure information <strong>and</strong> record the values they were trying to modify?”<br />

They want to not only prevent the attempted modification from taking place, but also create a<br />

permanent record of the attempt. Before the advent of autonomous transactions, many developers<br />

tried (<strong>and</strong> failed) to do this using st<strong>and</strong>ard triggers without an autonomous transaction.<br />

The trigger would detect the UPDATE <strong>and</strong>, upon discovering a user modifying data she should<br />

not, it would create an audit record <strong>and</strong> fail the UPDATE. Unfortunately, when the trigger<br />

failed the UPDATE, it also rolled back the audit record—it was an all-or-nothing failure. With<br />

autonomous transactions, it is now possible to securely capture the audit of an attempted<br />

operation as well as roll back that operation. In the process, we can inform the end user that<br />

she has attempted to modify data that she does not have permission to modify <strong>and</strong> that a<br />

record of the attempt has been made.<br />

It is interesting to note that the native <strong>Oracle</strong> AUDIT comm<strong>and</strong> provided the ability to capture<br />

unsuccessful attempts to modify information, using autonomous transactions, for many<br />

years. The exposure of this feature to <strong>Oracle</strong> developers allows us to create our own, more customized<br />

auditing.<br />

Here is a small example. Let’s place an autonomous transaction trigger on a table that<br />

captures an audit trail, detailing who tried to update the table <strong>and</strong> when that person tried to<br />

do it, along with a descriptive message of which data the person tried to modify. The logic<br />

behind this trigger will be that it will prevent any attempt to update the record of an employee<br />

who does not (directly or indirectly) report to you.<br />

First, we make a copy of the EMP table from the SCOTT schema to use as our example table:<br />

ops$tkyte@ORA10G> create table emp<br />

2 as<br />

3 select * from scott.emp;<br />

Table created.<br />

ops$tkyte@ORA10G> grant all on emp to scott;<br />

Grant succeeded.<br />

We also create an AUDIT_TAB table in which to store the audit information. Note that we’re<br />

using the DEFAULT attribute of the columns to have the currently logged in username <strong>and</strong> the<br />

current date/time logged into our audit trail as well:<br />

ops$tkyte@ORA10G> create table audit_tab<br />

2 ( username varchar2(30) default user,<br />

3 timestamp date default sysdate,<br />

4 msg varchar2(4000)<br />

5 )<br />

6 /<br />

Table created.<br />

Next, we create an EMP_AUDIT trigger to audit UPDATE activity on the EMP table:<br />

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

2 before update on emp<br />

3 for each row<br />

4 declare

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

Saved successfully!

Ooh no, something went wrong!