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 8 ■ TRANSACTIONS 257<br />

• ROLLBACK TO : This statement is used with the SAVEPOINT comm<strong>and</strong>. You<br />

may roll back your transaction to that marked point without rolling back any of the<br />

work that preceded it. So, you could issue two UPDATE statements, followed by a<br />

SAVEPOINT <strong>and</strong> then two DELETE statements. If an error or some sort of exceptional<br />

condition occurs during execution of the DELETE statements, <strong>and</strong> you catch that exception<br />

<strong>and</strong> issue the ROLLBACK TO SAVEPOINT comm<strong>and</strong>, the transaction will roll back to<br />

the named SAVEPOINT, undoing any work performed by the DELETEs, but leaving the<br />

work performed by the UPDATE statements intact.<br />

• SET TRANSACTION: This statement allows you to set various transaction attributes, such<br />

as the transaction’s isolation level <strong>and</strong> whether it is read-only or read-write. You can<br />

also use this statement to instruct the transaction to use a specific undo segment when<br />

using manual undo management, but this is not recommended. We’ll discuss manual<br />

<strong>and</strong> automatic undo management in more detail in Chapter 9.<br />

That’s it—there are no more transaction control statements. The most frequently used<br />

control statements are COMMIT <strong>and</strong> ROLLBACK. The SAVEPOINT statement has a somewhat special<br />

purpose. Internally, <strong>Oracle</strong> uses it frequently, <strong>and</strong> you may find some use for it in your application<br />

as well.<br />

Atomicity<br />

After that brief overview of the transaction control statements, we’re ready to see what’s meant<br />

by statement, procedure, <strong>and</strong> transaction atomicity.<br />

Statement-Level Atomicity<br />

Consider the following statement:<br />

Insert into t values ( 1 );<br />

It seems fairly clear that if it were to fail due to a constraint violation, the row would not be<br />

inserted. However, consider the following example, where an INSERT or DELETE on table T fires<br />

a trigger that adjusts the CNT column in table T2 appropriately:<br />

ops$tkyte@ORA10G> create table t2 ( cnt int );<br />

Table created.<br />

ops$tkyte@ORA10G> insert into t2 values ( 0 );<br />

1 row created.<br />

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

Commit complete.<br />

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

Table created.<br />

ops$tkyte@ORA10G> create trigger t_trigger<br />

2 before insert or delete on t for each row

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

Saved successfully!

Ooh no, something went wrong!