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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 8 ■ ■ ■ Transactions Transactions are one of the features that set a database apart from a file system. In a file system, if you are in the middle of writing a file and the operating system crashes, this file is likely to be corrupted. It is true there are “journaled” file systems and the like, which may be able to recover your file to some point in time. However, if you need to keep two files synchronized, such as system won’t help you there—if you update one file, and the system fails before you finish updating the second, then you will have out-of-sync files. This is the main purpose of transactions in the database; they take the database from one consistent state to the next. That is their job. When you commit work in the database, you are assured that either all of your changes are saved or none of them is saved. Furthermore, you are assured that the various rules and checks that protect data integrity are implemented. In the previous chapter, we discussed transactions in terms of concurrency control and how, as a result of Oracle’s multi-versioning read-consistent model, Oracle transactions can provide consistent data every time, under highly concurrent data access conditions. Transactions in Oracle exhibit all of the required ACID characteristics. ACID is an acronym for • Atomicity: Either all of a transaction happens or none of it happens. • Consistency: A transaction takes the database from one consistent state to the next. • Isolation: The effects of a transaction may not be visible to other transactions until the transaction has committed. • Durability: Once the transaction is committed, it is permanent. We discussed how Oracle obtains Consistency and Isolation in the previous chapter. Here we’ll focus most of our attention on concept of Atomicity and how that is applied in Oracle. In this chapter, we’ll discuss the implications of atomicity and how it affects statements in Oracle. We’ll cover transaction control statements such as COMMIT, SAVEPOINT, and ROLLBACK, and we’ll discuss how integrity constraints are enforced in a transaction. We’ll also look at why you may have some bad transaction habits if you’ve been developing in other databases. We’ll look at distributed transactions and the two-phase commit (2PC). Lastly, we’ll examine autonomous transactions, what they are, and the role they play. 255

256 CHAPTER 8 ■ TRANSACTIONS Transaction Control Statements There is no “begin transaction” statement needed in Oracle. A transaction implicitly begins with the first statement that modifies data (the first statement that gets a TX lock). You may explicitly begin a transaction using SET TRANSACTION or the DBMS_TRANSACTION package, but it is not a necessary step, unlike in various other databases. Issuing either a COMMIT or ROLLBACK statement explicitly ends a transaction. ■Note A ROLLBACK TO SAVEPOINT command will not end a transaction! Only a full, proper ROLLBACK will. You should always explicitly terminate your transactions with a COMMIT or ROLLBACK; otherwise, the tool/environment you are using will pick one or the other for you. If you exit your SQL*Plus session normally, without committing or rolling back, SQL*Plus will assume you wish you commit your work and will do so for you. If you just exit from a Pro*C program, on the other hand, an implicit rollback will take place. Never rely on implicit behavior, as it could change in the future. Always explicitly COMMIT or ROLLBACK your transactions. Transactions are atomic in Oracle, meaning that either every statement that comprises the transaction is committed (made permanent) or all of the statements are rolled back. This protection is extended to individual statements as well. Either a statement entirely succeeds or the statement is entirely rolled back. Note that I said the “statement” is rolled back. The failure of one statement does not cause previously executed statements to be automatically rolled back. Their work is preserved and must be either committed or rolled back by you. Before we get into the details of exactly what it means for a statement and transaction to be atomic, let’s take a look at the various transaction control statements available to us: • COMMIT: To use this statement’s simplest form, you would just issue COMMIT. You could be more verbose and say COMMIT WORK, but the two are equivalent. A COMMIT ends your transaction and makes any changes permanent (durable). There are extensions to the COMMIT statement used in distributed transactions. These extensions allow you to label a COMMIT (label a transaction) with some meaningful comment and force the commit of an in-doubt distributed transaction. • ROLLBACK: To use this statement’s simplest form, you would just issue ROLLBACK. Again, you could be more verbose and say ROLLBACK WORK, but the two are equivalent. A rollback ends your transaction and undoes any uncommitted changes you have outstanding. It does this by reading information stored in the rollback/undo segments (going forward I’ll refer to these exclusively as undo segments, the favored terminology for Oracle 10g) and restoring the database blocks to the state they were in prior to your transaction beginning. • SAVEPOINT: A SAVEPOINT allows you to create a “marked point” within a transaction. You may have multiple SAVEPOINTs within a single transaction.

256<br />

CHAPTER 8 ■ TRANSACTIONS<br />

Transaction Control Statements<br />

There is no “begin transaction” statement needed in <strong>Oracle</strong>. A transaction implicitly begins<br />

with the first statement that modifies data (the first statement that gets a TX lock). You may<br />

explicitly begin a transaction using SET TRANSACTION or the DBMS_TRANSACTION package, but it is<br />

not a necessary step, unlike in various other databases. Issuing either a COMMIT or ROLLBACK<br />

statement explicitly ends a transaction.<br />

■Note A ROLLBACK TO SAVEPOINT comm<strong>and</strong> will not end a transaction! Only a full, proper ROLLBACK<br />

will.<br />

You should always explicitly terminate your transactions with a COMMIT or ROLLBACK; otherwise,<br />

the tool/environment you are using will pick one or the other for you. If you exit your<br />

SQL*Plus session normally, without committing or rolling back, SQL*Plus will assume you<br />

wish you commit your work <strong>and</strong> will do so for you. If you just exit from a Pro*C program, on<br />

the other h<strong>and</strong>, an implicit rollback will take place. Never rely on implicit behavior, as it could<br />

change in the future. Always explicitly COMMIT or ROLLBACK your transactions.<br />

Transactions are atomic in <strong>Oracle</strong>, meaning that either every statement that comprises<br />

the transaction is committed (made permanent) or all of the statements are rolled back. This<br />

protection is extended to individual statements as well. Either a statement entirely succeeds<br />

or the statement is entirely rolled back. Note that I said the “statement” is rolled back. The<br />

failure of one statement does not cause previously executed statements to be automatically<br />

rolled back. Their work is preserved <strong>and</strong> must be either committed or rolled back by you.<br />

Before we get into the details of exactly what it means for a statement <strong>and</strong> transaction to be<br />

atomic, let’s take a look at the various transaction control statements available to us:<br />

• COMMIT: To use this statement’s simplest form, you would just issue COMMIT. You could<br />

be more verbose <strong>and</strong> say COMMIT WORK, but the two are equivalent. A COMMIT ends your<br />

transaction <strong>and</strong> makes any changes permanent (durable). There are extensions to the<br />

COMMIT statement used in distributed transactions. These extensions allow you to label a<br />

COMMIT (label a transaction) with some meaningful comment <strong>and</strong> force the commit of<br />

an in-doubt distributed transaction.<br />

• ROLLBACK: To use this statement’s simplest form, you would just issue ROLLBACK. Again,<br />

you could be more verbose <strong>and</strong> say ROLLBACK WORK, but the two are equivalent. A rollback<br />

ends your transaction <strong>and</strong> undoes any uncommitted changes you have outst<strong>and</strong>ing. It<br />

does this by reading information stored in the rollback/undo segments (going forward<br />

I’ll refer to these exclusively as undo segments, the favored terminology for <strong>Oracle</strong> 10g)<br />

<strong>and</strong> restoring the database blocks to the state they were in prior to your transaction<br />

beginning.<br />

• SAVEPOINT: A SAVEPOINT allows you to create a “marked point” within a transaction.<br />

You may have multiple SAVEPOINTs within a single transaction.

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

Saved successfully!

Ooh no, something went wrong!