Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 8 ■ TRANSACTIONS 281 scott@ORA10G> connect / Connected. ops$tkyte@ORA10G> set echo on ops$tkyte@ORA10G> select * from audit_tab; USERNAME TIMESTAMP MSG --------- --------- ---------------------------------------- OPS$TKYTE 27-APR-05 Attempt to update 7369 SCOTT 27-APR-05 Attempt to update 7788 The attempt by SCOTT to perform that UPDATE has been recorded. Summary In this chapter, we looked at many aspects of transaction management in Oracle. Transactions are one of the major features that set a database apart from a file system. Understanding how they work and how to use them is necessary to implement applications correctly in any database. Understanding that, in Oracle, all statements are atomic (including their side effects) and that this atomicity is extended to stored procedures is crucial. We saw how the placement of a WHEN OTHERS exception handler in a PL/SQL block could radically affect what changes took place in the database. As database developers, having a good understanding of how transactions work is crucial. We took a look at the somewhat complex interaction between integrity constraints (unique keys, check constraints, and the like) and transactions in Oracle. We discussed how Oracle typically processes integrity constraints immediately after a statement executes, but that we can defer this constraint validation until the end of the transaction if we wish. This feature is key in implementing complex multitable updates when the tables being modified are all dependent on each other—the cascading update was an example of that. We moved on to consider some of the bad transaction habits that people tend to pick up from working with databases that “support” rather than “promote” the use of transactions. We looked at the cardinal rule of transactions: they should be as short as they can be but as long as they need to be. Data integrity drives the transaction size—that is a key concept to take away from this chapter. The only things that should drive the size of your transactions are the business rules that govern your system. Not undo space, not locks—business rules. We covered distributed transactions and how they differ from single database transactions. We explored the limitations imposed upon us in a distributed transaction and discussed why they are there. Before you build a distributed system, you need to understand these limitations. What works in a single instance might not work in a distributed database. The chapter closed with a look at autonomous transactions and covered what they are and, more important, when they should and should not be used. I would like to emphasize once again that the legitimate real-world use of autonomous transactions is exceedingly rare. If you find them to be a feature you are using constantly, you’ll want to take a long, hard look at why.
- 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 300 and 301: CHAPTER 8 ■ ■ ■ Transactions
- 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 328 and 329: CHAPTER 9 ■ ■ ■ Redo and Undo
- Page 330 and 331: CHAPTER 9 ■ REDO AND UNDO 285 ■
- Page 332 and 333: CHAPTER 9 ■ REDO AND UNDO 287 ops
- Page 334 and 335: CHAPTER 9 ■ REDO AND UNDO 289 At
- Page 336 and 337: CHAPTER 9 ■ REDO AND UNDO 291 The
- Page 338 and 339: CHAPTER 9 ■ REDO AND UNDO 293 imp
- Page 340 and 341: CHAPTER 9 ■ REDO AND UNDO 295 •
- Page 342 and 343: CHAPTER 9 ■ REDO AND UNDO 297 •
- Page 344 and 345: CHAPTER 9 ■ REDO AND UNDO 299 Tab
- Page 346 and 347: CHAPTER 9 ■ REDO AND UNDO 301 @my
- Page 348 and 349: CHAPTER 9 ■ REDO AND UNDO 303 ops
- Page 350 and 351: CHAPTER 9 ■ REDO AND UNDO 305 ops
- Page 352 and 353: CHAPTER 9 ■ REDO AND UNDO 307 I d
- Page 354 and 355: CHAPTER 9 ■ REDO AND UNDO 309 dat
- Page 356 and 357: CHAPTER 9 ■ REDO AND UNDO 311 •
- Page 358 and 359: CHAPTER 9 ■ REDO AND UNDO 313 Why
- Page 360 and 361: CHAPTER 9 ■ REDO AND UNDO 315 blo
- Page 362 and 363: CHAPTER 9 ■ REDO AND UNDO 317 In
- Page 364 and 365: CHAPTER 9 ■ REDO AND UNDO 319 So,
- Page 366 and 367: CHAPTER 9 ■ REDO AND UNDO 321 13
- Page 368 and 369: CHAPTER 9 ■ REDO AND UNDO 323 Inv
- Page 370 and 371: CHAPTER 9 ■ REDO AND UNDO 325 ops
- Page 372 and 373: CHAPTER 9 ■ REDO AND UNDO 327 •
- Page 374 and 375: CHAPTER 9 ■ REDO AND UNDO 329 man
CHAPTER 8 ■ TRANSACTIONS 281<br />
scott@ORA10G> connect /<br />
Connected.<br />
ops$tkyte@ORA10G> set echo on<br />
ops$tkyte@ORA10G> select * from audit_tab;<br />
USERNAME TIMESTAMP MSG<br />
--------- --------- ----------------------------------------<br />
OPS$TKYTE 27-APR-05 Attempt to update 7369<br />
SCOTT 27-APR-05 Attempt to update 7788<br />
The attempt by SCOTT to perform that UPDATE has been recorded.<br />
Summary<br />
In this chapter, we looked at many aspects of transaction management in <strong>Oracle</strong>. Transactions<br />
are one of the major features that set a database apart from a file system. Underst<strong>and</strong>ing how<br />
they work <strong>and</strong> how to use them is necessary to implement applications correctly in any database.<br />
Underst<strong>and</strong>ing that, in <strong>Oracle</strong>, all statements are atomic (including their side effects)<br />
<strong>and</strong> that this atomicity is extended to stored procedures is crucial. We saw how the placement<br />
of a WHEN OTHERS exception h<strong>and</strong>ler in a PL/SQL block could radically affect what changes took<br />
place in the database. As database developers, having a good underst<strong>and</strong>ing of how transactions<br />
work is crucial.<br />
We took a look at the somewhat complex interaction between integrity constraints<br />
(unique keys, check constraints, <strong>and</strong> the like) <strong>and</strong> transactions in <strong>Oracle</strong>. We discussed how<br />
<strong>Oracle</strong> typically processes integrity constraints immediately after a statement executes, but<br />
that we can defer this constraint validation until the end of the transaction if we wish. This<br />
feature is key in implementing complex multitable updates when the tables being modified<br />
are all dependent on each other—the cascading update was an example of that.<br />
We moved on to consider some of the bad transaction habits that people tend to pick up<br />
from working with databases that “support” rather than “promote” the use of transactions. We<br />
looked at the cardinal rule of transactions: they should be as short as they can be but as long<br />
as they need to be. Data integrity drives the transaction size—that is a key concept to take<br />
away from this chapter. The only things that should drive the size of your transactions are the<br />
business rules that govern your system. Not undo space, not locks—business rules.<br />
We covered distributed transactions <strong>and</strong> how they differ from single database transactions.<br />
We explored the limitations imposed upon us in a distributed transaction <strong>and</strong> discussed<br />
why they are there. Before you build a distributed system, you need to underst<strong>and</strong> these limitations.<br />
What works in a single instance might not work in a distributed database.<br />
The chapter closed with a look at autonomous transactions <strong>and</strong> covered what they are<br />
<strong>and</strong>, more important, when they should <strong>and</strong> should not be used. I would like to emphasize<br />
once again that the legitimate real-world use of autonomous transactions is exceedingly rare.<br />
If you find them to be a feature you are using constantly, you’ll want to take a long, hard look<br />
at why.