Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!