Beginning SQL
Beginning SQL Beginning SQL
Chapter 11 When discussing locking, the phrases optimistic and pessimistic inevitably come up. Optimistic locking generally presumes that transactions do not interfere with each other and thus prefer less restrictive locking strategies. Pessimistic locking assumes that transactions definitely interfere with each other and thus presumes more restrictive locking strategies. In general, locks work very well as long as the locks are of short duration, usually milliseconds or seconds. If locks last hours or days, they can tie a database up in knots, which is where versioning (discussed shortly) comes into play. Isolation Levels With some DBMSs, you can set the isolation level, which simply means the level of isolation between users of the database and their transactions and how much they are allowed to interfere with each other. To this point, this chapter has discussed transactions as if they were absolutely isolated from each other, but this may not be necessary or even desirable. Remember that transactions and locks have costs associated with them, and to force absolute isolation at all times would cause maximum cost. However, insufficient isolation between transactions can cause serious problems, displaying inconsistent views of the data and even causing data corruption. It is important to understand the issues that insufficient isolation can cause, as well as issues that unnecessary isolation causes. SET TRANSACTION The SQL2 standard provides an actual SET TRANSACTION statement that tells the DBMS which isolation level the transaction that follows uses. You can use this statement only as the first statement of a transaction, and thus you cannot change it in the middle of a transaction. It is possible to change levels at the beginning of any transaction, allowing different processes the freedom to select the isolation level necessary for the transaction that follows. Each level has its uses and should be used where applicable. After all, the lower the isolation level, the lower the impact on the database, on other users and processes, and on the hardware and software that implement the database platform. Isolation levels are set using SQL code as shown below. Only one of the isolation levels can be set at any given time, and it remains in effect until explicitly changed. Generally, if no SET TRANSACTION statement is issued, the SERIALIZABLE level is in effect. SET TRANSACTION ISOLATION LEVEL ISOLATION LEVEL BEGIN TRANSACTION The four isolation levels are discussed in the following sections. SERIALIZABLE 318 A SERIALIZABLE isolation is essentially the maximum isolation possible. At this level, the DBMS guarantees that concurrently processing transactions operate as if they occurred in sequence (serially). SERI- ALIZABLE isolation is the default level in the ANSI standard, and this is how most database users expect transactions to perform. If a transaction sequence needs to be able to read a specific set of data twice during the transaction and return exactly the same set of data, SERIALIZABLE is the level that you need to use. It is implemented by using the following statement: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION
- Page 624: Chapter 10 Perhaps you need to view
- Page 628: Chapter 10 Figure 10-7 Summary view
- Page 632: Chapter 10 296 This statement gives
- Page 636: Chapter 10 The CHECK OPTION plays a
- Page 642: 11 Transactions When you move out o
- Page 646: Example Data This section creates e
- Page 650: 3. Build a record in the Rentals ta
- Page 654: The other statement that the ANSI S
- Page 658: SAVE TRANSACTION Select Update Begi
- Page 662: Figure 11-5 Begin Transaction Delet
- Page 666: In spite of all of these performanc
- Page 670: Shared A shared lock essentially me
- Page 674: of record locks, the DBMS may decid
- Page 680: Chapter 11 Usually, the default iso
- Page 684: Chapter 11 From Sue’s perspective
- Page 688: Chapter 11 5. At this point, you ne
- Page 694: 12 SQL Security In today’s world,
- Page 698: Setting up security begins with cre
- Page 702: ALTER USER username options ALTER U
- Page 706: In Figure 12-3, three different gro
- Page 710: How It Works After you run this sta
- Page 714: check constraints can be used in a
- Page 718: You could then give SELECT privileg
- Page 722: Limitations on Views Views are by t
Chapter 11<br />
When discussing locking, the phrases optimistic and pessimistic inevitably come up. Optimistic locking<br />
generally presumes that transactions do not interfere with each other and thus prefer less restrictive<br />
locking strategies. Pessimistic locking assumes that transactions definitely interfere with each other and<br />
thus presumes more restrictive locking strategies. In general, locks work very well as long as the locks<br />
are of short duration, usually milliseconds or seconds. If locks last hours or days, they can tie a database<br />
up in knots, which is where versioning (discussed shortly) comes into play.<br />
Isolation Levels<br />
With some DBMSs, you can set the isolation level, which simply means the level of isolation between users<br />
of the database and their transactions and how much they are allowed to interfere with each other. To this<br />
point, this chapter has discussed transactions as if they were absolutely isolated from each other, but this<br />
may not be necessary or even desirable. Remember that transactions and locks have costs associated with<br />
them, and to force absolute isolation at all times would cause maximum cost. However, insufficient isolation<br />
between transactions can cause serious problems, displaying inconsistent views of the data and even<br />
causing data corruption. It is important to understand the issues that insufficient isolation can cause, as<br />
well as issues that unnecessary isolation causes.<br />
SET TRANSACTION<br />
The <strong>SQL</strong>2 standard provides an actual SET TRANSACTION statement that tells the DBMS which isolation<br />
level the transaction that follows uses. You can use this statement only as the first statement of a transaction,<br />
and thus you cannot change it in the middle of a transaction. It is possible to change levels at the<br />
beginning of any transaction, allowing different processes the freedom to select the isolation level necessary<br />
for the transaction that follows. Each level has its uses and should be used where applicable. After<br />
all, the lower the isolation level, the lower the impact on the database, on other users and processes, and<br />
on the hardware and software that implement the database platform.<br />
Isolation levels are set using <strong>SQL</strong> code as shown below. Only one of the isolation levels can be set at any<br />
given time, and it remains in effect until explicitly changed. Generally, if no SET TRANSACTION statement<br />
is issued, the SERIALIZABLE level is in effect.<br />
SET TRANSACTION ISOLATION LEVEL ISOLATION LEVEL<br />
BEGIN TRANSACTION<br />
The four isolation levels are discussed in the following sections.<br />
SERIALIZABLE<br />
318<br />
A SERIALIZABLE isolation is essentially the maximum isolation possible. At this level, the DBMS guarantees<br />
that concurrently processing transactions operate as if they occurred in sequence (serially). SERI-<br />
ALIZABLE isolation is the default level in the ANSI standard, and this is how most database users expect<br />
transactions to perform. If a transaction sequence needs to be able to read a specific set of data twice during<br />
the transaction and return exactly the same set of data, SERIALIZABLE is the level that you need to<br />
use. It is implemented by using the following statement:<br />
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE<br />
BEGIN TRANSACTION