DMC unit 3 notes
DMC unit 3 notes
* What is a Transaction?
= any action that reads from and/or writes to a database.
= logical unit of work that must be entirely completed or entirely aborted;
no intermediate states are acceptable.
= may consist of a simple statement or a series of related statements.
= A successful transaction changes the database from one consistent state to
another consistent state.
- A consistent database state is one in which all data integrity
constraints are satisfied.
* Concurrency Control
- Meaning = The coordination of the simultaneous execution of transactions in a
multiuser database system
- Objective = to ensure the serializability of transactions in a multiuser
database environment.
- data integrity and consistency problems:
1) Lost updates
= occurs when two concurrent transactions, T1 and T2, are updating the
same data element and one of the updates is lost (overwritten by the other
transaction).
2) Uncommitted data
= occurs when two transactions, T1 and T2, are executed concurrently and
the first transaction (T1) is rolled back after the second transaction (T2) has
already accessed the uncommitted datathus violating the isolation property of
transactions.
3) Inconsistent retrievals
= occurs when a transaction accesses data before and after another
transaction(s) finish working with such data.
* The Schedular:
- The scheduler is a special DBMS process that establishes the order in which
the operations within concurrent transactions are executed.
- aim is to ensure serializability and isolation of transactions.
- The schedulers main job is to create a serializable schedule of a
transactions operations.
- A serializable schedule = a schedule of a transactions operations in
which the interleaved execution of the transactions (T1,
T2, T3, etc.) yields the same results as if the transactions were executed
in serial order (one after another).
- The scheduler also makes sure that the computers central processing unit
(CPU) and storage systems are used efficiently.
- Additionally, the scheduler facilitates data isolation to ensure that two
transactions do not update the same data
element at the same time. (e.g. table 10.11)
- following algorithms are used by DBMS to determine proper order:
1) locking
2) time stamping
3) optimistic
- Generally, transactions that are not serializable are executed on a first-
come, first-served basis by the DBMS. Otherwise, the schedulers main job is to
create a serializable schedule of a transactions operations.
* Lock Granularity
- Lock granularity indicates the level of lock use.
- Locking can take place at the following levels:
1) database level
2) table level
3) page level
4) row level
5) field (attribute) level
- In a page-level lock, the DBMS will lock an entire diskpage. (figure 10.5)
- A diskpage, or page, is the equivalent of a diskblock, which can be
described as a directly addressable section of a disk.
- A page has a fixed size, such as 4K, 8K, or 16K.
- A table can span several pages, and a page can contain several rows of one
or more tables.
- Page-level locks are currently the most frequently used multiuser DBMS
locking method.
- A row-level lock (figure 10.6) is much less restrictive than the locks
discussed earlier.
- The DBMS allows concurrent transactions to access different rows of the same
table even when the rows are located on the same page.
- it improves the availability of data.
- its management requires high overhead because a lock exists for each row in
a table of the database involved in a conflicting transaction.
- Modern DBMSs automatically escalate a lock from a row-level to a page-level
lock when the application session requests multiple locks
on the same page.
- The field-level lock allows concurrent transactions to access the same row as
long as they require the use of different fields (attributes)
within that row.
- Although field-level locking clearly yields the most flexible multiuser data
access, it is rarely implemented in a DBMS.
- because it requires an extremely high level of computer overhead and because
the row-level lock is much more useful in practice.
* Lock types
1) Binary Locks
- it has only two states
- Locked (1)
- Unlocked (0)
- If an objectthat is, a database, table, page, or rowis locked by a
transaction, no other transaction can use that object.
- If an object is unlocked, any transaction can lock the object for its use.
- As a rule, a transaction must unlock the object after its termination.
- Such operations of locking and unlocking are automatically managed and
scheduled by the DBMS.
- binary locks are now considered too restrictive to yield optimal
concurrency conditions.
2) Shared/Exclusive Locks
- An exclusive lock exists when access is reserved specifically for the
transaction that locked the object.
- A shared lock exists when concurrent transactions are granted read access
on the basis of a common lock.
- a lock can have three states: unlocked, shared (read), and exclusive
(write).
- mutual exclusive rule: only one transaction at a time can own an exclusive
lock on the same object.
- locks prevent serious data inconsistencies but they can lead to two major
problems:
1) The resulting transaction schedule might not be serializable.
2) The schedule might create deadlocks.
- A deadlock occurs when two transactions wait indefinitely for each
other to unlock data.
- A database deadlock, which is equivalent to traffic gridlock in a big
city, is caused when two or more transactions wait for
each other to unlock data.
* Two-phase locking
- It guarantees serializability, but it does not prevent deadlocks.
- The two phases are: (figure 10.7)
1. A growing phase
- in which a transaction acquires all required locks without unlocking any
data. Once all locks have been acquired, the transaction
is in its locked point.
2. A shrinking phase
- in which a transaction releases all locks and cannot obtain any new lock.
- rules:
# 1. Two transactions cannot have conflicting locks.
# 2. No unlock operation can precede a lock operation in the same transaction.
# 3. No data are affected until all locks are obtainedthat is, until the
transaction is in its locked point.
- It increases the transaction processing cost.
- It might cause additional undesirable effects. e.g. the possibility of
creating deadlocks
* Deadlocks
- A deadlock occurs when two transactions wait indefinitely for each other to
unlock data.
- e.g. Two transactions, T1 and T2, exist in the following mode:
T1 = access data items X and Y
T2 = access data items Y and X
If T1 has not unlocked data item Y, T2 cannot begin; if T2 has not
unlocked data item X, T1 cannot continue.
Consequently, T1 and T2 each wait for the other to unlock the required
data item.
- A deadlock is also known as a deadly embrace.
- Note:
1) Deadlocks are possible only when one of the transactions wants to obtain an
exclusive lock on a data item.
2) No deadlock condition can exist among shared locks.
- The three basic techniques to control deadlocks are:
1) Deadlock prevention
2) Deadlock detection
3) Deadlock Avoidance
- The choice of the best deadlock control method to use depends on the
database environment.
1) if the probability of deadlocks is low = deadlock detection is
recommended.
2) if the probability of deadlocks is high = deadlock prevention is
recommended.
3) if response time is not high on the systems priority list = deadlock
avoidance might be employed.
2) Wound/wait scheme
= Rule 1 : If the transaction requesting the lock is the older of the two
transactions, it will preempt (wound) the younger
transaction (by rolling it back). T1 preempts T2 when T1 rolls back T2. The
younger, preempted transaction is rescheduled using the same time stamp.
= Rule 2 : If the transaction requesting the lock is the younger of the
two transactions, it will wait until the other transaction is completed and the
locks are released.
= Here, in summary, the older transaction rolls back the younger
transaction and reschedules it.
3) the write phase = upon positive result of validation test, changes (that
are in temporary files) are applied into database permanently.
***** * *****