Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, Vishal Pawar > BISQL – Laymen to SQL Developer # 36 – Concurrency Control Techniques #1 – Introduction, Types of Locks: Locking Technique for Concurrency Control

BISQL – Laymen to SQL Developer # 36 – Concurrency Control Techniques #1 – Introduction, Types of Locks: Locking Technique for Concurrency Control

Hi Folks,

This post is part of Series Database Management Systems

Currently running topic for this series is listed as below :

Series of Database Management Systems

>>Chapter 1 : DBMS [Database Management Systems]

>>Chapter 2 : Database Core Concepts and  Applications

>>Chapter 3 : Record Storage and Primary File Organization

>>Chapter 4 : Index Structures of Files

>>Chapter 5 : Entity-Relationship Model

>>Chapter 6 : Relational Algebra

>>Chapter 7 : SQL

>>Chapter 8 : Functional Dependencies and  Normalization For Relational Databases

>>Chapter 9 : Transaction Processing

>>Chapter 10 : Concurrency Control Techniques<You are Here>

Continuing from my previous post on this series.

We are going to Cover the Following Points in this article

  • Introduction
  • Types of Locks: Locking Technique for Concurrency Control

Introduction

In this  you will study some of the main techniques used to control concurrent execution of transactions are based on the concept of locking data items. A lock is a restriction on access to data in a multi-user environment. It prevents multiple users from changing the same data simultaneously. If locking is not used, data within the database may become logically incorrect and may produce unexpected results.

Types of Locks: Locking Technique for Concurrency Control

Several types of locks are used in concurrency control, to introduce locking concepts gradually. We shall first discuss binary locks, which are simple but restrictive and so are not used in practice. We shall then discuss shared/exclusive locks, which provide more general locking capabilities and are used in practical database locking schemes.

Binary Locks: A binary lock can have two states or values: locked and unlocked (or 1 and 0, for simplicity). A distinct lock is associated with each database item X. If the value of the lock on X is 1, item X cannot be accessed by a database operation that requests the item. If the value of the lock on X is 0, the item can be accessed when requested.

Two operations, lock_item and unlock_item, are used with binarylocking. A transaction requests access to an item X by first issuing a lock_item(X) operation. If LOCK(X)=1, the transaction if forced to wait. If LOCK(X)=0, it is set to 1 and the transaction is allowed to access item X. When the transaction is through using the item, it issues an unlock_item(X) operation, which sets LOCK(X) to 0 (unlocks the item), so that X may be accessed by another transaction. Hence a binary lock enforces mutual-exclusion on the data item.

Lock_item(D):

B: if LOCK(X)=0 ("item is unlocked")

Then LOCK(X)¬1 ("lock the item")

else begin

wait (until lock(X)=0 and

the lock manager wakes up the transaction);

goto B

end;

unlock_item(X):

LOCK(X)¬0; (‘unlock the item") If any transactions are waiting

Then wake up one of the waiting transactions:

If the simple binary locking scheme described here is used, every transaction must obey the following rules:

1. A transaction T must issue the operation loci_item(X) before any

read_item(X) or write_item(X) operations are performed in T,

2. A transaction T must issue the operation unlock_item(X) after all read_item(X) and write_item(X) operations are completed in T.

3. A transaction T will not issue a lock_item(X) operation if it already holds

the lock on item X

4. A transaction T will not issue an unlock_item(X) operation unless it already holds the lock on item X.

Shared Locks: It is used for read only operations, i.e., used for operations that do not change or update the data.

E.G., SELECT statement:,

Shared locks allow concurrent transaction to read (SELECT) a data. No other transactions can modify the data while shared locks exist. Shared locks are released as soon as the data has been read.

Exclusive Locks: Exclusive locks are used for data modification operations, such as UPDATE, DELETE and INSERT. It ensures that multiple updates cannot be made to the same resource simultaneously. No other transaction can read or modify data when locked by an exclusive lock.

Exclusive locks are held until transaction commits or rolls back since those are used for write operations.

There are three locking operations: read_lock(X), write_lock(X), and unlock(X). A lock associated with an item X, LOCK(X), now has three possible states: "read locked", "write-locked", or "unlocked". A read-locked item is also called share-locked, because other transactions are allowed to read the item, whereas a write-locked item is called exclusive-locked, because a single transaction exclusive holds the lock on the item.

Each record on the lock table will have four fields: <data item name, LOCK, no_of_reads, locking_transaction(s)>. The value (state) of LOCK is either read-locked or write-locked.

read_lock(X):

B, if LOCK(X)=’unlocked’

Then begin LOCK(X)¬"read-locked" No_of_reads(x)¬1

end

else if LOCK(X)="read-locked"

then no_of_reads(X)¬no_of_reads(X)+1

else begin wait(until)LOCK(X)="unlocked" and the lock manager wakes up the transaction);

goto B end; write_lock(X):

B: if LOCK(X)="unlocked"

Then LOCK(X)¬"write-locked";

else begin

wait(until LOCK(X)="unlocked" and

the lock manager wkes up the transaction);

goto B

end;

unlock(X):

if LOCK(X)="write-locked"

Then begin LOCK(X)¬"un-locked";

Wakeup one of the waiting transctions, if any

end

else if LOCK(X)=read-locked" then begin

no_of_reads(X)¬no_of_reads(X)-1 if no_of_reads(X)=0

then begin LOCK(X)=unlocked";

wakeup one of the waiting transactions, if any end

end;

Hope you will like Series of Database Management Systems series !

If you have not yet subscribe this Blog , Please subscribe it from “follow me” tab !

So that you will be updated @ real time and all updated knowledge in your mail daily for free without any RSS subscription OR news reading !!

Happy Learning and Sharing !!

For More information related to BI World visit our all Mentalist networks Blog

SQL Server Mentalist … SQL Learning Blog

Business Intelligence Mentalist … Business Intelligence World

Microsoft Mentalist … MVC,ASP.NET, WCF & LinQ

MSBI Mentalist … MS BI and SQL Server

NMUG Bloggers …Navi Mumbai User Group Blog

Architectural Shack … Architectural implementation and design patterns

DBA Mentalist …Advance SQL Server Blog

MVC Mentalist … MVC Learning Blog

Link Mentalist … Daily Best link @ your email

Infographics Mentalist … Image worth explaining thousand Words

Hadoop Mentalist … Blog on Big Data

BI Tools Analysis … BI Tools

Connect With me on

| Facebook |Twitter | LinkedIn| Google+ | Word Press | RSS | About Me |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: