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 9 ■ REDO AND UNDO 291 The DELETE Again, undo is generated as a result of the DELETE, blocks are modified, and redo is sent over to the redo log buffer. This is not very different from before. In fact, it is so similar to the UPDATE that we are going to move right on to the COMMIT. The COMMIT We’ve looked at various failure scenarios and different paths, and now we’ve finally made it to the COMMIT. Here, Oracle will flush the redo log buffer to disk, and the picture will look like Figure 9-4. Figure 9-4. State of the system after a COMMIT The modified blocks are in the buffer cache; maybe some of them have been flushed to disk. All of the redo necessary to replay this transaction is safely on disk and the changes are now permanent. If we were to read the data directly from the data files, we probably would see the blocks as they existed before the transaction took place, as DBWR most likely has not written them yet. That is OK—the redo log files can be used to bring up to date those blocks in the event of a failure. The undo information will hang around until the undo segment wraps around and reuses those blocks. Oracle will use that undo to provide for consistent reads of the affected objects for any session that needs them. Commit and Rollback Processing It is important for us to understand how redo log files might impact us as developers. We will look at how the different ways we can write our code affect redo log utilization. We’ve already seen the mechanics of redo earlier in the chapter, and now we’ll look at some specific issues. Many of these scenarios might be detected by you, but would be fixed by the DBA as they affect the database instance as a whole. We’ll start with what happens during a COMMIT, and then get into commonly asked questions and issues surrounding the online redo logs.

292 CHAPTER 9 ■ REDO AND UNDO What Does a COMMIT Do? As a developer, you should have a good understanding of exactly what goes on during a COMMIT. In this section, we’ll investigate what happens during the processing of the COMMIT statement in Oracle. A COMMIT is generally a very fast operation, regardless of the transaction size. You might think that the bigger a transaction (in other words, the more data it affects), the longer a COMMIT will take. This is not true. The response time of a COMMIT is generally “flat,” regardless of the transaction size. This is because a COMMIT does not really have too much work to do, but what it does do is vital. One of the reasons this is an important fact to understand and embrace is that it will lead you down the path of letting your transactions be as big as they should be. As we discussed in the previous chapter, many developers artificially constrain the size of their transactions, committing every so many rows, instead of committing when a logical unit of work has been performed. They do this in the mistaken belief that they are preserving scarce system resources, when in fact they are increasing them. If a COMMIT of one row takes X units of time, and the COMMIT of 1,000 rows takes the same X units of time, then performing work in a manner that does 1,000 one-row COMMITs will take an additional 1,000*X units of time to perform. By committing only when you have to (when the logical unit of work is complete), you will not only increase performance, but also reduce contention for shared resources (log files, various internal latches, and the like). A simple example demonstrates that it necessarily takes longer. We’ll use a Java application, although you should expect similar results from most any client— except, in this case, PL/SQL (we’ll discuss why that is after the example). To start, here is the sample table we’ll be inserting into: scott@ORA10G> desc test Name Null? Type ----------------- -------- ------------ ID NUMBER CODE VARCHAR2(20) DESCR VARCHAR2(20) INSERT_USER VARCHAR2(30) INSERT_DATE DATE Our Java program will accept two inputs: the number of rows to INSERT (iters) and how many rows between commits (commitCnt). It starts by connecting to the database, setting autocommit off (which should be done in all Java code), and then calling a doInserts() method a total of three times: • Once just to warm up the routine (make sure all of the classes are loaded) • A second time, specifying the number of rows to INSERT along with how many rows to commit at a time (i.e., commit every N rows) • A final time with the number of rows and number of rows to commit set to the same value (i.e., commit after all rows have been inserted) It then closes the connection and exits. The main method is as follows:

CHAPTER 9 ■ REDO AND UNDO 291<br />

The DELETE<br />

Again, undo is generated as a result of the DELETE, blocks are modified, <strong>and</strong> redo is sent over to<br />

the redo log buffer. This is not very different from before. In fact, it is so similar to the UPDATE<br />

that we are going to move right on to the COMMIT.<br />

The COMMIT<br />

We’ve looked at various failure scenarios <strong>and</strong> different paths, <strong>and</strong> now we’ve finally made it<br />

to the COMMIT. Here, <strong>Oracle</strong> will flush the redo log buffer to disk, <strong>and</strong> the picture will look like<br />

Figure 9-4.<br />

Figure 9-4. State of the system after a COMMIT<br />

The modified blocks are in the buffer cache; maybe some of them have been flushed to<br />

disk. All of the redo necessary to replay this transaction is safely on disk <strong>and</strong> the changes are<br />

now permanent. If we were to read the data directly from the data files, we probably would<br />

see the blocks as they existed before the transaction took place, as DBWR most likely has not<br />

written them yet. That is OK—the redo log files can be used to bring up to date those blocks in<br />

the event of a failure. The undo information will hang around until the undo segment wraps<br />

around <strong>and</strong> reuses those blocks. <strong>Oracle</strong> will use that undo to provide for consistent reads of<br />

the affected objects for any session that needs them.<br />

Commit <strong>and</strong> Rollback Processing<br />

It is important for us to underst<strong>and</strong> how redo log files might impact us as developers. We will<br />

look at how the different ways we can write our code affect redo log utilization. We’ve already<br />

seen the mechanics of redo earlier in the chapter, <strong>and</strong> now we’ll look at some specific issues.<br />

Many of these scenarios might be detected by you, but would be fixed by the DBA as they<br />

affect the database instance as a whole. We’ll start with what happens during a COMMIT, <strong>and</strong><br />

then get into commonly asked questions <strong>and</strong> issues surrounding the online redo logs.

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

Saved successfully!

Ooh no, something went wrong!