Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
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.
- Page 250 and 251: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 252 and 253: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 254 and 255: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 256 and 257: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 258 and 259: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 260 and 261: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 262 and 263: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 264 and 265: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 266 and 267: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 268 and 269: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 270 and 271: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 272 and 273: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 274 and 275: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 276 and 277: CHAPTER 7 ■ ■ ■ Concurrency a
- Page 278 and 279: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 280 and 281: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 282 and 283: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 284 and 285: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 286 and 287: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 288 and 289: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 290 and 291: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 292 and 293: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 294 and 295: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 296 and 297: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 298 and 299: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 302 and 303: CHAPTER 8 ■ TRANSACTIONS 257 •
- Page 304 and 305: CHAPTER 8 ■ TRANSACTIONS 259 So,
- Page 306 and 307: CHAPTER 8 ■ TRANSACTIONS 261 X --
- Page 308 and 309: CHAPTER 8 ■ TRANSACTIONS 263 “s
- Page 310 and 311: CHAPTER 8 ■ TRANSACTIONS 265 busi
- Page 312 and 313: CHAPTER 8 ■ TRANSACTIONS 267 Many
- Page 314 and 315: CHAPTER 8 ■ TRANSACTIONS 269 ops$
- Page 316 and 317: CHAPTER 8 ■ TRANSACTIONS 271 last
- Page 318 and 319: CHAPTER 8 ■ TRANSACTIONS 273 Dist
- Page 320 and 321: CHAPTER 8 ■ TRANSACTIONS 275 Auto
- Page 322 and 323: CHAPTER 8 ■ TRANSACTIONS 277 3 Au
- Page 324 and 325: CHAPTER 8 ■ TRANSACTIONS 279 5 pr
- Page 326: CHAPTER 8 ■ TRANSACTIONS 281 scot
- Page 329 and 330: 284 CHAPTER 9 ■ REDO AND UNDO cri
- Page 331 and 332: 286 CHAPTER 9 ■ REDO AND UNDO Fir
- Page 333 and 334: 288 CHAPTER 9 ■ REDO AND UNDO The
- Page 335 and 336: 290 CHAPTER 9 ■ REDO AND UNDO We
- Page 337 and 338: 292 CHAPTER 9 ■ REDO AND UNDO Wha
- Page 339 and 340: 294 CHAPTER 9 ■ REDO AND UNDO row
- Page 341 and 342: 296 CHAPTER 9 ■ REDO AND UNDO If
- Page 343 and 344: 298 CHAPTER 9 ■ REDO AND UNDO ops
- Page 345 and 346: 300 CHAPTER 9 ■ REDO AND UNDO Inv
- Page 347 and 348: 302 CHAPTER 9 ■ REDO AND UNDO The
- Page 349 and 350: 304 CHAPTER 9 ■ REDO AND UNDO 41
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.