Skip to content Skip to navigation

Connexions

You are here: Home » Content » Introduction to Concurrency Control

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • VOCW

    This module is included inLens: Vietnam OpenCourseWare's Lens
    By: Vietnam OpenCourseWare

    Click the "VOCW" link to see all content affiliated with them.

Recently Viewed

This feature requires Javascript to be enabled.
 

Introduction to Concurrency Control

Module by: Nguyen Kim Anh. E-mail the author

In the previous lecture, we have seen that a schedule must have two properties: leaving the database in consistent state and allowing transaction failures to be handle in a safe manner. The schedule with conflict or view serializable and cascadeless satisfy these requirement. In addition, we have known concurrent executions have some benefits in terms of improving performance of the system. There are various concurrency- control schemes is consider with the main purpose of providing a high degree of concurrency while ensuring that all schedules that can be generated are confict or view serializable and are cascadeless.

In this lecture, we discuss some of those concurrency control schemes .

Lock-based Protocols

Locking data items to prevent multiple transaction from accessing the items concurrently is the most common method used to ensure serializability. In this section, we examine the basic idea behind the locking concepts and introduce two-phase locking protocol.

Locks

Definition: A lock is a variable associated with data items that describe the status of the item with respect to possible operations that can be applied to it.

There are various types of locks are used in concurrency control. We will discuss some of them in detail in the following subsections.

Binary Locks

As we can see from the name of this type, a binary lock can have two states: locked or unlocked. A lock is associated with each database item X. If the value of the lock on X is “locked” then X cannot be accessed by an operations that request the item. If the value of the lock on X is “unlocked” then X can be accessed when requested.

The function lock(X) let the system known whether data item X is locked or not at a point of time. If lock(X) = 1 then X is being locked, if lock(X) = 0 then X is not locked.

For implementing the binary lock, we have two instructions:

  • lock_item(X)
  • unlock_item(X)

When a transaction requests access to item X, it must issuing the lock_item(X) operation. If X is currently unlocked then it is locked ( lock(X) → 1). If X is locked then the transaction need to wait.

When a transaction finish using the data item X, it issues the unlock_item(X) operation, which set lock(X) to 0 so that X be accessed by other transaction.

A transaction will not issue lock_item(X) if it is already hold the lock on X.

A transaction will not issue unlock_item(X) unless it already hold the lock on X.

Shared/Exclusive Locks

Binary locks are too restrictive to data item since at a certain point of time, at most one transaction can hold a lock on a data item X. In the database system, it should allow multiple transaction access a data item only for reading. Only when a transaction access data item for writing, the transaction can have exclusive lock.

Therefore, we have a different type of lock called shared/exclusive locks or read/write lock.

A lock associated with one data item . Instead of having only two states, now a lock can have three distict states which are read_locked, write_locked and unlock.

Read_lock is a shared lock since multiple transaction can have read_lock on the same data item in order to read the item. Write_lock is exclusive lock because a single transaction exclusively holds the lock on item.

For this type of lock, we have three instructions:

  • lock_S(Q) : used to request a shared lock on data item Q
  • lock_X(Q) used to request an exclusive lock on data item Q
  • unlock(Q) used to unlock data item Q

To access a data item, transaction T must first lock that item. If the data item is already locked by another transaction in an incompatible mode, the lock wil not be granted until all incompatible locks held by other transaction have been released. In such case, T have to wait. The table below shows us the compatibility relation between two modes of locking; the relation comp(A,B) of the matrix has the value true if and only if mode A is compatible with mode B.

Table 1
  S X
S true false
X false false

Transaction T may unlock data item that it had locked at some ealier point.

Consider two transaction T1 and T2 below:

Table 2
T1 T2
lock_X(B) lock_S(A)
read(B) read(A)
B:= B-50 lock_S(B)
write(B) read(B)
lock_X(A) display(A+B)
read(A) unlock(A)
A:= A + 50 unlock(B)
write(A)  
unlock(B)  
unlock(A)  

In the figure 1, we can see a situation called deadlock which is the state where neither transaction can proceed with its normal execution since they are waiting for each other to release a lock. T1 is currently holding a exclusive lock on B and T2­ is requesting a shared lock on B, T2 is waiting for T1 to unlock B. On the other hand, T2 is holding a shared lock on A, T1 is requesting an exclusive lock on A so it is waiting for T2 to unlock A

Table 3: Part of schedule 1 with deadlock
T1 T2
lock_X(B)  
read(B)  
B:= B-50  
write(B)  
  lock_S(A)
  read(A)
  lock_S(B)
lock_X(A)  

If we do not use locking data items as soon as possible when reading or writing them, we can get inconsistency. However, if we do not unlock data item right before requesting a lock on another item, deadlock might occur. We need a set of rules (protocol) indicating when a transaction may lock or unlock each of data items. Locking protocol limits the number of possible schedules.

Two-phases Locking protocol

This is a common locking protocol in database system.

This protocol requires each transaction issues lock and unlock requests in two phases:

  • Growing phase: A lock can be obtained but none can be released
  • Shrinking phase: existing locks can be released but no new lock can be obtained.

Transaction T1 and T2 above both are two phase

Example of transaction which is not two phase:

Table 4
T3
lock_S(A)
read(A)
unlock(A)
lock_S(B)
read(B)
unlock(B)
display(A+B)

Two –phase locking does not ensure freedom from dead lock. We can see that T1 and T2 are two phase but schedule 1 (figure 1) still has deadlock.

Two phase locking ensure conflict serializability but does not ensure cascadeless.

Cascading rollbacks can be avoided by a modification of two phase locking called strict two-phase locking protocol.

Strict two-phase locking protocol

In addition to locking being two –phase, all exclusive locks taken by a transaction must be held until that transaction commit.

This requirement ensure that no transaction can read a data item which is locking in exclusive mode by an uncommitted transaction.

Most of the database systems implement this strict two-phase locking protocol

Timestamps-based Protocols

In the locking protocols, the order between every pair of conflicting transactions is determined at execution time by the first lock that they both request that involves incompatible modes. Another method for determining the serializability order is select an ordering among transactions in advance. Timestamp – ordering scheme is the typical example of this method.

Timestamps

A timestamps is a unique variable associated with a transaction. Timestamp of transaction T is denoted by TS(T). This timestamp is assigned by the database system in the order in which the transaction submitted to the system. If Ti has been assigned timestamp TS(Ti) and a new transaction Tj enters the system then TS(Ti) < TS(Tj)

There are two simple ways for generating timestamp:

  1. Use the current value of system clock as the timestamps
  2. Use a logical counter that is incremented each time a value is assigned to a transaction as timestamp.

In order to use timestamp techniques, each data item Q is associtated with two timestamp values:

  • read_TS(Q). This is the largest timestamp of any transaction that have successfully read data item Q.

read_TS(Q) = TS(T) where T is the youngest transaction that has read Q successfully

  • write_TS(Q). This is the largest timestamp of any transaction that sucessfully written data item Q.

write_TS(Q) = TS(T) where T is the youngest transaction that has written Q successfully

Timestamp-Ordering Protocol

The timestamp ordering protocol ensures that conflicting read and write operations are executed in timestamp order. This protocol operates as follows:

  1. Transaction T issues read(Q) operation
    • If TS(T) < write_TS(Q) , T is trying to read a value of Q that was already overwritten, then abort read operation and T is rolled back.
    • If TS(T) >= write_TS(Q) then execute read operation, read_TS(Q) = max(read_TS(Q), TS(T))
  2. Transaction T issues write(Q) operation
    • If TS(T) < read_TS(Q) or TS(T) < write_TS(Q), then abort write opertation and roll back T because there is an younger transaction that already read or written the value of Q before T had a chance to write Q
    • Otherwise, execute write operation and write_TS(Q) = TS(T)

Transaction which is rolled back by the above scheme is assigned a new timestamp and is restarted.

Table 5: Schedule 2 – a schedule under timestamp protocol
T4 T5 Timestamp values
read(B)   read_TS(B) = TS(T4)
  read(B) read_TS(B) = TS(T5)
  B:= B-50  
  write(B) write_TS(B) = TS(T5)
read(A)   read_TS(A) = TS(T4)
  read(A) read_TS(A) = TS(T5)
display(A+B)    
  A:=A+50  
  write(A) write_TS(A) = TS(T5)
  display(A+B)  

The timestamp-ordering protocol ensures conflict serializability since the conflict operations are processed in timestamp order. This also ensures freedom from deadlock because no lock is used. Howerver, it can generate schedules that are not recoveable.

Strict timestamp-ordering protocol

In this strict variation of timestamp ordering, a transaction T that issues read(Q) or write(Q) such that TS(T) > write_TS(Q) has its read or write opertations delayed until the transaction T’ that wrote the value of Q has committed or aborted.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks