Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 7 ■ ■ ■ Concurrency and Multi-Versioning As stated in the last chapter, one of the key challenges in developing multiuser, databasedriven applications is to maximize concurrent access but, at the same time, ensure that each user is able to read and modify the data in a consistent fashion. In this chapter, we’re going to take a detailed look at how Oracle achieves multi-version read consistency, and what that means to you, the developer. I will also introduce a new term, write consistency, and use it to describe how Oracle works not only in a read environment with read consistency, but also in a mixed read and write environment. What Are Concurrency Controls? Concurrency controls are the collection of functions that the database provides to allow many people to access and modify data simultaneously. As noted in the previous chapter, the lock is one of the core mechanisms by which Oracle regulates concurrent access to shared database resources and prevents “interference” between concurrent database transactions. To briefly summarize, Oracle uses a variety of locks, including the following: • TX locks: These locks are acquired for the duration of a data-modifying transaction. • TM and DDL locks: These locks ensure that the structure of an object is not altered while you are modifying its contents (TM lock) or the object itself (DDL lock). • Latches: These are internal locks that Oracle employs to mediate access to its shared data structures. In each case, there is minimal overhead associated with lock acquisition. TX transaction locks are extremely scalable in terms of both performance and cardinality. TM and DDL locks are applied in the least restrictive mode whenever possible. Latches and enqueues are both very lightweight and fast (enqueues are the slightly heavier of the two, though they’re more feature-rich). Problems only arise from poorly designed applications that hold locks for longer than necessary and cause blocking in the database. If you design your code well, Oracle’s locking mechanisms will allow for scaleable, highly concurrent applications. But Oracle’s support for concurrency goes beyond efficient locking. It implements a multi-versioning architecture (introduced in Chapter 1) that provides controlled, yet highly concurrent access to data. Multi-versioning describes Oracle’s ability to simultaneously 231
232 CHAPTER 7 ■ CONCURRENCY AND MULTI-VERSIONING materialize multiple versions of the data and is the mechanism by which Oracle provides read-consistent views of data (i.e., consistent results with respect to a point in time). A rather pleasant side effect of multi-versioning is that a reader of data will never be blocked by a writer of data. In other words, writes do not block reads. This is one of the fundamental differences between Oracle and other databases. A query that only reads information in Oracle will never be blocked, it will never deadlock with another session, and it will never get an answer that didn’t exist in the database. ■Note There is a short period of time during the processing of a distributed 2PC where Oracle will prevent read access to information. As this processing is somewhat rare and exceptional (the problem applies only to queries that start between the prepare and the commit phases and try to read the data before the commit arrives), I will not cover it in detail. Oracle’s multi-versioning model for read consistency is applied by default at the statement level (for each and every query) and can also be applied at the transaction level. This means that each and every SQL statement submitted to the database sees a read-consistent view of the database at least—and if you would like this read-consistent view of the database to be at the level of a transaction (a set of SQL statements), you may do that as well. The basic purpose of a transaction in the database is to take the database from one consistent state to the next. The ISO SQL standard specifies various transaction isolation levels, which define how “sensitive” one transaction is to changes made by another. The greater the level of sensitivity, the greater the degree of isolation the database must provide between transactions executed by your application. In the following section, we’ll look at how, via its multi-versioning architecture and with absolutely minimal locking, Oracle can support each of the defined isolation levels. Transaction Isolation Levels The ANSI/ISO SQL standard defines four levels of transaction isolation, with different possible outcomes for the same transaction scenario. That is, the same work performed in the same fashion with the same inputs may result in different answers, depending on your isolation level. These isolation levels are defined in terms of three “phenomena” that are either permitted or not at a given isolation level: • Dirty read: The meaning of this term is as bad as it sounds. You are permitted to read uncommitted, or dirty, data. You would achieve this effect by just opening an OS file that someone else is writing and reading whatever data happens to be there. Data integrity is compromised, foreign keys are violated, and unique constraints are ignored. • Nonrepeatable read: This simply means that if you read a row at time T1 and attempt to reread that row at time T2, the row may have changed. It may have disappeared, it may have been updated, and so on.
- Page 226 and 227: CHAPTER 5 ■ ORACLE PROCESSES 181
- Page 228 and 229: CHAPTER 6 ■ ■ ■ Locking and L
- Page 230 and 231: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 232 and 233: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 234 and 235: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 236 and 237: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 238 and 239: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 240 and 241: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 242 and 243: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 244 and 245: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 246 and 247: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 248 and 249: CHAPTER 6 ■ LOCKING AND LATCHING
- 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 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
232<br />
CHAPTER 7 ■ CONCURRENCY AND MULTI-VERSIONING<br />
materialize multiple versions of the data <strong>and</strong> is the mechanism by which <strong>Oracle</strong> provides<br />
read-consistent views of data (i.e., consistent results with respect to a point in time). A rather<br />
pleasant side effect of multi-versioning is that a reader of data will never be blocked by a<br />
writer of data. In other words, writes do not block reads. This is one of the fundamental differences<br />
between <strong>Oracle</strong> <strong>and</strong> other databases. A query that only reads information in <strong>Oracle</strong> will<br />
never be blocked, it will never deadlock with another session, <strong>and</strong> it will never get an answer<br />
that didn’t exist in the database.<br />
■Note There is a short period of time during the processing of a distributed 2PC where <strong>Oracle</strong> will prevent<br />
read access to information. As this processing is somewhat rare <strong>and</strong> exceptional (the problem applies only<br />
to queries that start between the prepare <strong>and</strong> the commit phases <strong>and</strong> try to read the data before the commit<br />
arrives), I will not cover it in detail.<br />
<strong>Oracle</strong>’s multi-versioning model for read consistency is applied by default at the statement<br />
level (for each <strong>and</strong> every query) <strong>and</strong> can also be applied at the transaction level. This means<br />
that each <strong>and</strong> every SQL statement submitted to the database sees a read-consistent view of<br />
the database at least—<strong>and</strong> if you would like this read-consistent view of the database to be at<br />
the level of a transaction (a set of SQL statements), you may do that as well.<br />
The basic purpose of a transaction in the database is to take the database from one consistent<br />
state to the next. The ISO SQL st<strong>and</strong>ard specifies various transaction isolation levels,<br />
which define how “sensitive” one transaction is to changes made by another. The greater the<br />
level of sensitivity, the greater the degree of isolation the database must provide between<br />
transactions executed by your application. In the following section, we’ll look at how, via its<br />
multi-versioning architecture <strong>and</strong> with absolutely minimal locking, <strong>Oracle</strong> can support each<br />
of the defined isolation levels.<br />
Transaction Isolation Levels<br />
The ANSI/ISO SQL st<strong>and</strong>ard defines four levels of transaction isolation, with different possible<br />
outcomes for the same transaction scenario. That is, the same work performed in the same<br />
fashion with the same inputs may result in different answers, depending on your isolation<br />
level. These isolation levels are defined in terms of three “phenomena” that are either permitted<br />
or not at a given isolation level:<br />
• Dirty read: The meaning of this term is as bad as it sounds. You are permitted to read<br />
uncommitted, or dirty, data. You would achieve this effect by just opening an OS file<br />
that someone else is writing <strong>and</strong> reading whatever data happens to be there. Data<br />
integrity is compromised, foreign keys are violated, <strong>and</strong> unique constraints are ignored.<br />
• Nonrepeatable read: This simply means that if you read a row at time T1 <strong>and</strong> attempt to<br />
reread that row at time T2, the row may have changed. It may have disappeared, it may<br />
have been updated, <strong>and</strong> so on.