Beginning SQL

Beginning SQL Beginning SQL

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

No updates, additions, or deletions to the record set by other transactions are allowed while your transaction is processing. SERIALIZABLE transactions are absolutely required where multiple transactions occur on the same table, with updates and deletes that could cause data corruption if not handled properly. This scenario is covered in greater detail in the lost update and uncommitted data examples later in this chapter. REPEATABLE READ REPEATABLE READ is the second most rigorous level of isolation and essentially states that updates by other transactions to the existing records involved in your transaction must not be seen. However, insertions are allowed. In other words, existing data rows that your transaction reads may not be modified or deleted, but additional rows may be added. You set REPEATABLE READ by using the following statement: SET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRANSACTION A multirow query run by your transaction at the beginning of your transaction may return additional records late — the phantom insert problem discussed a little bit later. READ COMMITTED READ COMMITTED is the third isolation level. READ COMMITTED guarantees that your transaction cannot see uncommitted updates from other transactions. This prevents the lost updates and uncommitted data problems discussed later. Use the following statement to specify the READ COMMITTED isolation level: SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRANSACTION However, any transactions committed between reads of a data set may be seen. A process reading a row of data twice could see changed data in the second read since another process could commit a change to that data which would be seen the second time the data is read. If your process attempts to write updates to data updated by another process, your update is automatically rolled back to prevent the lost update problem, which is discussed later. READ UNCOMMITTED READ UNCOMMITTED is the last isolation level specified by the ANSI standard. In READ UNCOMMITTED, any updates performed by other transactions affect your transaction, either committed or uncommitted. This allows the uncommitted data, inconsistent data, and phantom data problems, which are all discussed in detail later in this chapter. However, the DBMS prevents the lost update problem (also discussed later) by causing a rollback of your changes if it would cause lost updates. The following statement shows you how to set the isolation level to READ UNCOMMITTED: SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED BEGIN TRANSACTION Transactions The SET TRANSACTION statement also allows you to specify READ ONLY, which means that the transaction only reads data. You can also specify READ WRITE, in which case, the transaction expects to update, add, or delete records. Specifying READ WRITE allows the DBMS to optimize the transaction since it knows in advance how the transaction may impact other processes and transactions. 319

No updates, additions, or deletions to the record set by other transactions are allowed while your transaction<br />

is processing. SERIALIZABLE transactions are absolutely required where multiple transactions<br />

occur on the same table, with updates and deletes that could cause data corruption if not handled properly.<br />

This scenario is covered in greater detail in the lost update and uncommitted data examples later in<br />

this chapter.<br />

REPEATABLE READ<br />

REPEATABLE READ is the second most rigorous level of isolation and essentially states that updates by<br />

other transactions to the existing records involved in your transaction must not be seen. However, insertions<br />

are allowed. In other words, existing data rows that your transaction reads may not be modified or<br />

deleted, but additional rows may be added. You set REPEATABLE READ by using the following statement:<br />

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ<br />

BEGIN TRANSACTION<br />

A multirow query run by your transaction at the beginning of your transaction may return additional<br />

records late — the phantom insert problem discussed a little bit later.<br />

READ COMMITTED<br />

READ COMMITTED is the third isolation level. READ COMMITTED guarantees that your transaction cannot<br />

see uncommitted updates from other transactions. This prevents the lost updates and uncommitted data<br />

problems discussed later. Use the following statement to specify the READ COMMITTED isolation level:<br />

SET TRANSACTION ISOLATION LEVEL READ COMMITTED<br />

BEGIN TRANSACTION<br />

However, any transactions committed between reads of a data set may be seen. A process reading a row<br />

of data twice could see changed data in the second read since another process could commit a change to<br />

that data which would be seen the second time the data is read. If your process attempts to write updates<br />

to data updated by another process, your update is automatically rolled back to prevent the lost update<br />

problem, which is discussed later.<br />

READ UNCOMMITTED<br />

READ UNCOMMITTED is the last isolation level specified by the ANSI standard. In READ UNCOMMITTED,<br />

any updates performed by other transactions affect your transaction, either committed or uncommitted.<br />

This allows the uncommitted data, inconsistent data, and phantom data problems, which are all discussed<br />

in detail later in this chapter. However, the DBMS prevents the lost update problem (also discussed<br />

later) by causing a rollback of your changes if it would cause lost updates. The following<br />

statement shows you how to set the isolation level to READ UNCOMMITTED:<br />

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED<br />

BEGIN TRANSACTION<br />

Transactions<br />

The SET TRANSACTION statement also allows you to specify READ ONLY, which means that the transaction<br />

only reads data. You can also specify READ WRITE, in which case, the transaction expects to update,<br />

add, or delete records. Specifying READ WRITE allows the DBMS to optimize the transaction since it<br />

knows in advance how the transaction may impact other processes and transactions.<br />

319

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

Saved successfully!

Ooh no, something went wrong!