Oops
Oops
1.0 INTRODUCTION
Object orientation is based on real life philosophy. In Unit 1 and 2 of Block 1 you are
introduced to basic concepts of Object Oriented technology. It is emphasized that “in
Object Oriented Programming (OOP) more emphasis is given on data” .To achieve
this objective concept of classification is used which results into classes, that are the
major component of object-oriented programming. Object Oriented Programming
forces to think in terms of objects and interaction between objects them.
In this unit you will study how Java supports class, objects, and how objects are used
in problem solving. You will also study how to use methods for communication
between objects. We will use a special kind of methods known as static method in
programs writing .To initialize objects at the time of creation we use constructors.
Also in this unit we will learn the concept of constructor overloading. Further you will
study use of this key word in programs. We will discuss the memory de–allocation
technique used in Java.
Finally we will discuss various arguments (parameters) passing schemes, how an
object is passed as parameter and object return from a function.
1.1 OBJECTIVES
After going through this unit you will be able to explain:
The class construct is what makes Java an object-oriented language. A Java class is a
group of values with a set of operations to manipulate this values. Classes facilitate
modularity and information hiding. Classes are used to define a new data type. Once a
new data type is defined, variables of this data type can be created in program for
solving problems. Variables of a class are known as objects of that class, and carry the
properties of the class with values. Thus it can be said that, “a class is a template for
an object of the properties and object is an instance of a class”.
Before defining a class it must be clear to you for what purpose you are going to
create class. i.e. “ the nature and exact form of the class” should be reflected in class
definition.
You can see that class is declared by the use of the class keyword. Data and methods
are defined within the class. Being the part of the class, data (variables) and methods
are called member of the class. Data are called member data or instance variable of
the class and methods are called member functions of the class. Member functions
are defined within a class and act on the data member of the class.
As we have discussed before defining a class, the data members and member
functions of a class should be decided. If we take Student class and want to display
basic information of the student, first we have to decide the data members required to
represent basic information, then we need a member function to display basic
information.
{
String name;
String course
int roll_no;
6
} Class and Objects
The class Student is not yet complete because in this class only data members are
there. Still a member function to display basic information is needed.
Complete definition of class Student will have three data members-name, course and
age and one member function display_info( ).
//class definition
class Student
{
String name;
String course ;
int age;
void display_info( ) // function for displaying basic information
{
System.out.println(“ Student Information”);
System.out.println(“Name:”+name);
System.out.println(“Course:”+course);
System.out.println(“Age:”+age);
}
}
// end of Student class
As mentioned earlier class defines a new data type. In our case Student is a new data
type. Now Student can be used to declare objects of Student class.
An object of Student class can be created as follows:
As this statement is executed an object student 1 of Student class is created. You will
see a detailed discussion of this type of statements in a later section of this unit.
Each time you create an object of a class a copy of each instance variables defined in
the class is created. In other words you can say that each object of a class has its own
copy of data members defined in the class. Member functions have only one copy and
shared by all the objects of that class. All the objects may have their own value of
instance variables. As given in Figure1 every object of class Student will have its own
copy of name, course, and age but only one copy of method display_info( ) for all the
objects.
Name: Manoj
Course: CIC
Age: 22
Age: 25
Name: Manoj
Course: MCA
Age: 29
7
Object Oriented Concepts Now let us see how objects are declared and used inside a program.
and Exceptions Handling
The new operator in Java dynamically allocates memory for an object returns a
reference to object and binds the reference with the object. Here reference to object
means address of object. So it is essential in Java that all the objects must be
dynamically allocated.
Declaring Student object
Step 1:
Student student1; // declaring reference to object
Step 2:
Student1 = new Student( ); // allocating memory
Now you can see a complete Java program for displaying basic information of
students
//program
class Student
{
String name;
String course ;
int age;
void display_info( ) // function for displaying basic information
{
System.out.println(“ Student Information”);
System.out.println(“Name:”+name);
System.out.println(“Course:”+course);
System.out.println(“Age:”+age);
}
}
// end of Student class
class Display_Test
{
public static void main( String para[])
{
Student student1;
student1 = new Student();
sudent1.name = “Mr.Ravi”; //assigning value to name variable of student1 object
student1.course = “MCA”; //assigning value to course variable of student1 object
student1.age = 23; //assigning value to age variable of student1 object
student1.display_info(); // invoking display_info( ) method on student1 object
}
}
The output of this program is:
8
Name:Mr.Ravi Class and Objects
Course:MCA
Age:23
If you observe this program, instance variables of student1 are assigned values. Is
there any other way of assigning value to instance variables? For the questions the
answer is yes. An object created can be used only if its instance variables contain
value. The values of instance variables, represent the state of an objects for example,
student1 object in this program. At present student is representing a student named
Ravi, a MCA student of age 23.If you change the value of any of the instance
variable’s the state of the object will change.
Two approaches can be used for assigning value to the instance variables of the
objects. In first approval create an object and initialize instance variables one by one,
as done in the above program. Initialization of instance variable is done by using dot(
.) operator. Dot(.) operator is used to access members (data and method both) of the
objects.
object_name.variable_name = value;
But this method of initialization of objects is not convenient because all instance
variables of the object must be initialized carefully, and this exercise should be done
for all objects before they are used. In this method of initialization there is a chance of
skipping some variables unassigned where large number of objects are to be
initialized.
To overcome this problem second approach of object initialization with the help of
constructors, can be used. We will discuss how constructors are used in the next
section of this unit.
One object can be assigned to another object, of same type but it works very different
than a normal assignment. Let us see it with the help of a program.
class Person
{
String name;
int age ;
String address;
void Display( )
{
System.out.println("Person Information:"+name +" ("+age +")"+"\n"+address);
}
}
class Reference_Test
{
public static void main(String[] args)
{
Person p = new Person();
Person q = new Person();
p.name= "Mr.Naveen Kumar";
p.age= 24;
p.address = "248,Sector 22, Noida";
p.Display();
q = p;// q refer to p
q.name = "Mr.Suresh";
q.address = "22,Mahanadi,IGNOU,Maidan Garhi";
p.Display();
9
Object Oriented Concepts q.Display();
and Exceptions Handling }
}
In this program two objects p and q are created. Object p is initialized with some
values, then Display method is called through object p. It displays the information of
object p. Further, object p is assigned to object q as reference variable. You can see in
the figure both object p and object q are referring to the same object. Thus any change
made in object p or q changes will be reflect the both p and q. You can see in the
program that changes made in name and address by q are reflected in p also.
Whenever in a program this type of referencing of variables is used care should be
taken while changing the values of instance variables of object being referred.
An object is a class instance. The class of an object determines what it is and how it
can be manipulated. A class encapsulates methods, data, and implementation of
methods. This encapsulation is like a contract between the implementer of the class
and the user of that class.
……………………………………………………………………………………
……………………………………………………………………………………
2) What is the advantage of having member data and member functions within the
class?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
A Java class is a group of values with a set of operations. The user of a class
manipulates object of that class only through the methods of that class.
10
} Class and Objects
type specifies the type of data that will be returned by the method. This can be any
data type including class types. In case a method does not return any value, its return
type must be void. The argument–list is a list of type and identifier pairs separated by
commas. Arguments are basically variables that receive the values at the time of
method invocation. If a method has return type other than void, it returns a value to
the calling point using the following form of statement.
return value;// value is the value to be returned by function.
Static variables and methods are also known as class variables or class methods since
each class variable and each class method occurs once per class. Instance methods and
variables occur once per instance of a class or you can say every object is having its
own copy of instance variables.
One very important point to note here is that a program can execute a static method
without creating an object of the class. All other methods must be invoked through an
object, and, therefore an object must exist before they can be used. You have seen that
every Java application program has one main()method. This method is always static
because Java starts execution from this main()method, and at that time no object is
created.
The last line of the main() method uses the System class from the Java.lang package
to display the current date and time. See the line of code that invokes the println ()
method.
System.out.println(today);
Now look at the details of the argument passed to it.
System.out refers to the out variable of the System class. You already know that, to
refer to static variables and methods of a class, you use a syntax similar to the C and
C++ syntax for obtaining the elements in a structure. You join the class’s name and
the name of the static method or static variable together with a dot (.)
The point which should be noticed here is that the application never instantiated the
System class and that out is referred to directly from the class. This is because out is
declared as a static variable: a variable associated with the class rather than with an
instance of the class. You can also associate methods with a class--static methods--
using static keyword.
1.3.2 Constructors
A constructor initializes object with its creation. It has the same name as the name of
its class. Once a constructor is defined, it is automatically called immediately the
memory is allocated before the new operation completes. Constructor does not have
any return type, it implicit return type is class object. You can see constructor as a
class type. Its job is to initialize the instance variables of an object, so that the created
object is usable just after creation.
In program Complex_Test you have seen that for initializing imaginary and real parts,
two methods have been used. Both the methods have been invoked on the object one
by one. In place of methods if you use constructor for initialization you have to make
12
changes in Complex_Test program. Remove methods used for initializing variables Class and Objects
and use one method having same name of the class, i.e., Complex with two
arguments.
class Complex
{
double real;
double imag;
Complex( double p, double q)
{
System.out.println("Constructor in process...");
real = p;
real = p;
imag = q;
}
void showComplex ( )
{
System.out.println("The Complex Number is :"+ real +"+i"+imag);
}
}
class Complex_Test
{
public static void main(String[] args)
{
Complex R1 = new Complex(5,2);
R1.showComplex();
}
}
The output of this program is:
Constructor in process…
The Complex Number is :5.0+i2.0
If you compare this program and the previous Complex_Test program you will find
that in this program the instance variable of object R1 is initialized with the help of
constructor Complex (5, 2), value 5 has been assigned to the real variable and 2 has
been assigned to the imag variable. In the previous program, to initialize these
variables, methods assignReal() and assignImag() were used.
Constructors can be defined in two ways. First , a constructor may not have parameter.
This type of constructor is known as non-parameterized constructor. The second, a
constructor may take parameters. This type of constructor is known as parameterized
constructor.
For example, if you want to create Point class object with your initial values of x and
y coordinates, you can use parameterized constructor. You have to make the following
changes in the previous program.
class Point
{
int x;
int y;
Point(int a, int b)
{
x= a;
y= b;
}
void Display_Point()
{
System.out.println("The Point is: ("+x+","+y+")");
}
}
class Point_1_Test
{
public static void main( String args[])
{
Point p1 = new Point(2,5);
Point p2 = new Point(9,7);
p1.Display_Point();
p2.Display_Point();
}
}
14
In this program you can see that points p1 and p2 are initialized by values of Class and Objects
programmer’s choice by using parameterized constructor.
You may ask whether there can be more than one constructor in a class. Yes, there
may be more than one constructor in a class but all the constructors in a class either
will have different types of arguments or different number of arguments passed to it.
This is essential because without this it wouldn’t be possible to identify which of the
constructors is invoked. Having more than one constructor in a single class is known
as constructor overloading. In the program given below more than one constructor in
class Student are defined.
class Student
{
String name;
int roll_no;
String course;
Student( String n, int r, String c)
{
name = n;
roll_no = r;
course = c;
}
Student(String n, int r )
{
name = n;
roll_no = r;
course = "MCA";
}
void Student_Info( )
{
System.out.println("********* Student Information ***********");
System.out.println("Name:"+name);
System.out.println("Course:"+course);
System.out.println("Roll Number:"+roll_no);
}
}
class Student_Test
{
public static void main(String[] args)
{
Student s1 = new Student("Ravi Prakash", 987770012,"BCA");
Student s2 = new Student("Rajeev ", 980070042);
s1.Student_Info();
s2.Student_Info();
}
}
Output of this program is:
********* Student Information ***********
Name:Ravi Prakash
Course:BCA
Roll Number:987770012
********* Student Information ***********
Name:Rajeev
Course:MCA
Roll Number:980070042
15
Object Oriented Concepts Both the constructors of this program can be used for creating two different types of
and Exceptions Handling objects. Here different types are simply related to the values assigned to instance
variables of objects during their initialization through constructors. By using a
constructor with three arguments all the three instance variables name, roll_no and,
course can be initialized. If a constructor with two arguments is used to create object,
only instance variables name and roll_no can be given initial value through
constructor and the variable course is initialized by a constant value “MCA”.
If a method wants to refer the object through which it is invoked, it can refer to by
using this keyword. You know it is illegal to have two variables of the same name
within the same scope in a program. Suppose local variables in a method, or formal
parameters of the method, overlap with the name of instance variables of the class, to
differentiate between local variables or formal parameters and instance variables, this
keyword is used. The reason to use this keyword to resolve any name conflict that
might occur between instance variables and local variable is that this keyword can be
used to refer to the objects directly. You can see in this program class Test_This is
having one variable named rate and method. Total_Interest is also having one local
variable named rate. To avoid conflict between both the rate variables keyword has
been used with rate variable of Test_This class.
class Test_This
{
int rate ;
int amount;
int interest;
Test_This( int r, int a)
{
rate = r;
amount =a;
}
void Total_Interest( )
16
{ Class and Objects
int rate = 5;
rate = this.rate+rate;
interest = rate*amount/100;
System.out.println("Total Interest on "+amount+" is: "+interest);
}
}
class This_Test
{
public static void main(String[] args)
{
Test_This Ob1 = new This( 5, 5000);
Ob1.Total_Interest();
}
}
Output of this program is:
Total Interest on 5000 is: 500.
Objects can be based as parameter and can also be returned from the methods. Let us
look at the two aspects one by one.
During problem solving you need to pass arguments through methods. Basically by
passing arguments you can generalize a method. During problem solving generalized
methods can be used for performing operations on a variety of data.
You can see in this program below that for find the area of square and rectangle. The
methods Area_S and Area_R for finding area of square and rectangle respectively are
defined.
class Test
{
int Area_S( int i)
{
return i*i;
}
int Area_R(int a,int b)
{
return a*b;
}
}
class Area_Test
{
public static void main(String args[])
{
Test t = new Test();
int area;
area = t.Area_S(5);
System.out.println("Area of Square is : "+area);
area = t.Area_R(5,4);
System.out.println("Area of Rectangle is : "+area);
}
}
Output of this program is:
Area of Square is: 25
17
Object Oriented Concepts Area of Rectangle is: 20
and Exceptions Handling
Can you tell the way of passing parameters in methods Area_S and Area_R? It is call
by value. Right as you have studied about two basic way pass by value and pass by
reference of parameter passing in functions in course: MCS 01.
Now you may ask a question whether parameter passing in Java is by reference or by
value. The answer is everything in Java except value, passed by reference.
Pass-by-value means that when you call a method, a copy of the value of each of the
actual parameter is passed to the method. You can change that copy inside the
method, but this will have no effect on the actual parameters. You can see in the
program given below. The method max has two parameters that are passed by value.
class Para_Test
{
static int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
public static void main(String[] args)
{
int num1 = 40, num2 = 50, num3;
num3 = max( num1, num2);
System.out.println("The maximum in "+num1 +" and "+num2+" is : "+num3);
}
}
Output of this program is:
The maximum in 40 and 50 is: 50
The values of variables are always primitives or references, never objects. In Java, we
can pass a reference of the object to the formal parameter. If any changes to the local
object that take place inside the method will modify the object that was passed to it as
argument. Due to this reason objects passing as parameter in Java are referred to as
passing by reference. In the program given below object of the class Marks is passed
to method Set_Grade, in which instance variable grade of the object passed is
assigned some value. This reflects that the object itself is modified.
class Marks
{
String name;
int percentage;
String grade;
Marks(String n, int m)
{
name = n;
percentage = m;
}
void Display()
{
System.out.println("Student Name :"+name);
System.out.println("Percentage Marks:"+percentage);
System.out.println("Grade : "+grade);
System.out.println("*****************************");
}
}
class Object_Pass
{
public static void main(String[] args)
18
{ Class and Objects
Marks ob1 = new Marks("Naveen",75);
Marks ob2 = new Marks("Neeraj",45);
Set_Grade(ob1);
System.out.println("*****************************");
ob1.Display();
Set_Grade(ob2);
ob2.Display();
}
static void Set_Grade(Marks m)
{
if (m.percentage >= 60)
m.grade ="A";
else if( m.percentage >=40)
m.grade = "B";
else
m.grade = "F";
}
}
Output of this program is:
*****************************
Student Name :Naveen
Percentage Marks:75
Grade : A
*****************************
Student Name :Neeraj
Percentage Marks:45
Grade : B
*****************************
Like other basic data types, methods can return data of class type, i.e. object of class.
An object returned from a method can be stored in any other object of that class like
as values of basic type returned are stored in variables of that data type. You can see
in the program below where an the object of class Salary is returned by method the
incr_Salary.
class Salary.
{
int basic ;
String E_id;
Salary( String a, int b)
{
E_id = a;
basic = b;
}
Salary incr_Salary ( Salary s )
{
s.basic = basic*110/100;
return s;
}
}
class Ob_return_Test
{
public static void main(String[] args)
{
Salary s1 = new Salary("I100",5000);
Salary s2;// A new salary object
s1 = s1.incr_Salary( s1);
19
Object Oriented Concepts System.out.println("Current Basic Salary is : "+ s2.basic);
and Exceptions Handling }
}
Output of this program is:
Current Basic Salary is: 5500
Now once again you can return to a discussion of overloading, which was left at
section 1.3 of this unit.
Sometimes two or more methods which are very similar in nature are required. For
example, you can take methods Area_S and Area_R in a class Test in program of
section 1.3 of this unit. Both the methods are finding area but the argument passed to
them and their implementation is different. All the methods of similar kinds in a class
can have the same name, but remember all the methods will have different prototypes.
This concept is known as method overloading. By matching the type and order of
arguments passed to methods. The exact method that should execute is resolved.
Sometimes overloaded methods may have different return types, the return type alone
is not sufficient to differentiate two overloaded methods. You can see in the program
given below that the area()method is overloaded.
class Test
{
int Area( int i)
{
return i*i;
}
int Area(int a,int b)
{
return a*b;
}
}
class Area_Overload
{
public static void main(String args[])
{
Test t = new Test();
int area;
area = t.Area(5);
System.out.println("Area of Squire is : "+area);
area = t.Area(5,4);
System.out.println("Area of Rectangle is : "+area);
}
}
Output of this program is:
Area of Squire is: 25
Area of Rectangle is: 20.
20
The JVM’s heap stores all the objects created by an executing Java program. You Class and Objects
know Java’s “new” operator allocate memory to object at the time of creation on the
heap at run time. Garbage collection is the process of automatically freeing objects
that are no longer referenced by the program. This frees the programmer from having
to keep track of when to free allocated memory, thereby preventing many potential
bugs and thus reduces the programmer’s botheration.
The name “garbage collection" implies that objects that are no longer needed by the
program are “garbage” and can be thrown away and collects back into the heap. You
can see this process as “memory recycling”. When the program no longer references
an object, the heap space it occupies must be recycled so that the space is available for
subsequent new objects. The garbage collector must somehow determine which
objects are no longer referenced by the program and make available the heap space
occupied by such unreferenced objects.
Sometimes it is required to take independent resources from some object before the
garbage collector takes the object. To perform this operation, a method named
finalized () is used. Finalized () is called by the garbage collector when it determines
no more references to the object exist.
2. The garbage collector calls this method when it determines no more references
to the object exist.
3. The Object class finalize method performs no actions but it may be overridden
by any derived class.
21
Object Oriented Concepts Writing a finalize() Method
and Exceptions Handling
Before an object is garbage collected, the runtime system calls its finalize() method.
The intent for this to release system resources such as open files or open sockets
before object getting collected by garbage collector.
Your class can provide for its finalization simply by defining and implementing a
finalize() method in your class. Your finalize() method must be declared as follows:
class OpenAFile
{
FileInputStream aFile = null;
OpenAFile(String filename)
{
try
{
a File = new FileInputStream(filename);
}
catch (Java.io.FileNotFoundException e)
{
System.err.println("Could not open file " + filename);
}
}
}
The Java programmer must keep in mind that it is the garbage collector that runs
finalizers on objects. Because it is not generally possible to predict exactly when
unreferenced objects will be garbage collected, and to predict when object finalizers
will be run. Java programmers, therefore, should avoid writing code for which
program correctness depends upon the timely finalization of objects. For example, a
finalize of an unreferenced object may release a resource that is needed again later by
the program. The resource will not be made available until after the garbage collector
has run the object finalizer. If the program needs the resource before the garbage
collector has run the finalizer, the program is out of luck.
22
2) What are two major advantages of automatic de-allocation of memory? Class and Objects
……………………………………………………………………………………
……………………………………………………………………………………
3) Write a program in Java, in which a method named add is overloaded. The add method
sums two integer values, one integer value and one double value, two double values.
……………………………………………………………………………………
……………………………………………………………………………………
1.9 SUMMARY
In this unit, the concept of class is discussed. The process of object creation using
class is explained. Every object has its state and behavior. Objects behavior are
defined inside the class in terms of member functions. In this unit are also discussed
about creation of constructors and their advantages. The need of static methods is
explained with the help of the main () method of Java application program, which is
always static. This unit also explained how arguments are passed to a member
function. In the last section of this unit “Garbage Collection” one of the very
important concepts of Java programming is explained.
1.10 SOLUTIONS/ANSWERS
1) First define the class for the object to be declared. Then using new operator
allocates needed space to the variables of object.
Object definition in Java is a two-step process
i Declare variable of class type.
ii. Using new operator allocate needed space to the object.
2) Objects used in problem solving are instances of classes. Objects are treated as
black box with public interfaces. Interfaces are provided with the help of
member functions.
Member functions and member data are kept inside the class to ensure that data don’t
get accessed and modified accidentally. Definition of member functions is inside the
class and it provides scope of modification in the definition without affecting outside
world.
3) One object can be used as reference to another object provided both are of the
same class type. Objects should be used as a reference very carefully because if
there is any change in values of instance variables of one object, values of
instance variable of the second object also gets changed.
Check Your Progress 2
1) Member functions in a class are required for providing features of the objects.
In the program given below to display the price of book object member function
Get_Price() is defined.
23
Object Oriented Concepts //program
and Exceptions Handling class Book
{
String Title;
String Author;
int Price;
Book( String t, String a, int p)
{
Title = t;
Author = a;
Price = p;
}
void Get_Price ( )
{
System.out.println("Price of the book is :"+Price);
}
}
class Book_Test
{
public static void main(String args[])
{
Book b1 = new Book("Java Programming","Dr. Rajkumar Singh",250);
b1.Get_Price();
}
}
Output:
Price of the book is: 250
2) A static method can be invoked through the class itself rather than through an
object as in the case of member functions. There is no need of any object to call
a static method. Any static method is part of class not the part of objects. When
main method is called there does not exist any object. Therefore, it become
essential to have main () method as static.
//program
class Complex
{
int real;
int imaginary;
Complex ( int r, int i)
{
real = r;
imaginary = i;
}
void ShowNumber()
{
System.out.println("The Number is :" + real+"+" +imaginary+"i");
}
}
public class Cons_Test
{
public static void main(String args[])
{
Complex c= new Complex(5,3);
24
c.ShowNumber(); Class and Objects
}
}
Output
The Number is :5+3i
//program
class Bank_Account
{
String Name;
int Account_No;
String Address;
int Init_Bal;
Bank_Account(String n, int a)
{
Name = n;
Account_No = a;
}
Bank_Account (String n, String addr, int a , int b)
{
Name = n;
Address = addr;
Account_No = a;
Init_Bal = b;
}
}
public class Account_Test
{
public static void main( String args[])
{
Bank_Account Ac1 = new Bank_Account("Mr. Naveen", 11002345);
Bank_Account Ac2 = new Bank_Account( "Mr. Suresh", "K-2 MGI Basant Kunj
New Delhi", 12001347, 500);
System.out.println("Account No. of "+Ac1.Name + " is :"+Ac1.Account_No);
System.out.println("Initial Balance in account of "+Ac2.Name+" is
Rs."+Ac2.Init_Bal);
}
}
Output
Account No. of Mr. Naveen is :11002345
Initial Balance in account of Mr. Suresh is Rs.500.
Check Your Progress 3
1) Whenever there is a need to pass some value to a method from outside the
values are passed as arguments to method. Example’ is a method written to
calculate interest on some amount. Interest rate is a variable that can be passed
as argument to the method used for interest calculation.
Output
The Interest amount for Rs 5000 at the rate of 10 % for a year is: 500
3) //Program
class Add_Overload
{
Add_Overload()
{
System.out.println("*******Welcome to Overload Demo********");
}
void add( int i, int j)
{
System.out.println("Inside add(int,int)and the Sum is :"+(i+j));
}
void add( int i, double j)
{
System.out.println("Inside add(int,double)and the Sum is :"+(i+j));
}
void add( double i, double j)
{
System.out.println("Inside add(double,double)and the Sum is:"+(i+j));
}
}
public class OverloadTest
{
public static void main (String args[])
{
Add_Overload Ol = new Add_Overload();
Ol.add(34,35);
Ol.add( 25, 47.67);
Ol.add(12.5,25.5);
}
}
Output:
*******Welcome to Overload Demo********
Inside add(int,int)and the Sum is:69
Inside add(int,double)and the Sum is:72.67
Inside add(double,double)and the Sum is:38.0
26
Transactions and
UNIT 2 TRANSACTIONS AND Concurrency
Management
CONCURRENCY MANAGEMENT
Structure Page Nos.
2.0 Introduction 35
2.1 Objectives 35
2.2 The Transactions 35
2.3 The Concurrent Transactions 38
2.4 The Locking Protocol 42
2.4.1 Serialisable Schedules
2.4.2 Locks
2.4.3 Two Phase Locking (2PL)
2.5 Deadlock and its Prevention 49
2.6 Optimistic Concurrency Control 51
2.7 Summary 53
2.8 Solutions/ Answers 54
2.0 INTRODUCTION
One of the main advantages of storing data in an integrated repository or a database is
to allow sharing of it among multiple users. Several users access the database or
perform transactions at the same time. What if a user’s transactions try to access a
data item that is being used /modified by another transaction? This unit attempts to
provide details on how concurrent transactions are executed under the control of
DBMS. However, in order to explain the concurrent transactions, first we must
describe the term transaction.
Concurrent execution of user programs is essential for better performance of DBMS,
as concurrent running of several user programs keeps utilizing CPU time efficiently,
since disk accesses are frequent and are relatively slow in case of DBMS. Also, a
user’s program may carry out many operations on the data returned from DB, but
DBMS is only concerned about what data is being read /written from/ into the
database. This unit discusses the issues of concurrent transactions in more detail.
2.1 OBJECTIVES
After going through this unit, you should be able to:
• describe the term CONCURRENCY;
• define the term transaction and concurrent transactions;
• discuss about concurrency control mechanism;
• describe the principles of locking and serialisability, and
• describe concepts of deadlock & its prevention.
35
Structured Query or more data values in a database. Thus, it may require reading and writing of
Language and
Transaction Management
database value. For example, the withdrawal transactions can be written in pseudo
code as:
Example 1:
; Assume that we are doing this transaction for person
; whose account number is X.
Example 2:
; transfers transfer_amount from x’s account to y’s account
; assumes x&y both accounts exist
Please note the use of two keywords here COMMIT and ROLLBACK. Commit
makes sure that all the changes made by transactions are made permanent.
ROLLBACK terminates the transactions and rejects any change made by the
transaction. Transactions have certain desirable properties. Let us look into those
properties of a transaction.
Properties of a Transaction
A transaction has four basic properties. These are:
• Atomicity
• Consistency
• Isolation or Independence
• Durability or Permanence
36
Atomicity: It defines a transaction to be a single unit of processing. In other words Transactions and
Concurrency
either a transaction will be done completely or not at all. In the transaction example 1 Management
& 2 please note that transaction 2 is reading and writing more than one data items, the
atomicity property requires either operations on both the data item be performed or
not at all.
Consistency: This property ensures that a complete transaction execution takes a
database from one consistent state to another consistent state. If a transaction fails
even then the database should come back to a consistent state.
Durability: This property necessitates that once a transaction has committed, the
changes made by it be never lost because of subsequent failure. Thus, a transaction is
also a basic unit of recovery. The details of transaction-based recovery are discussed
in the next unit.
A transaction has many states of execution. These states are displayed in Figure 2.
Error!
Execute
Start Commit
Abort/
Rollback
37
Structured Query error at that point it may also be moved into the Abort state. During the execution
Language and
Transaction Management
transaction changes the data values and database moves to an inconsistent state. On
successful completion of transaction it moves to the Commit state where the durability
feature of transaction ensures that the changes will not be lost. In case of any error the
transaction goes to Rollback state where all the changes made by the transaction are
undone. Thus, after commit or rollback database is back into consistent state. In case a
transaction has been rolled back, it is started as a new transaction. All these states of
the transaction are shown in Figure 2.
A: Read X
Subtract 100
Write X
B: Read Y
Add 100
Write Y
Let us suppose an auditor wants to know the total assets of Mr. Sharma. He executes
the following transaction:
Transaction T2:
Read X
Read Y
Display X+Y
Suppose both of these transactions are issued simultaneously, then the execution of
these instructions can be mixed in many ways. This is also called the Schedule. Let us
define this term in more detail.
A schedule S is defined as the sequential ordering of the operations of the ‘n’
interleaved transactions. A schedule maintains the order of operations within the
individual transaction.
TA TB
38
READ X READ X Transactions and
Concurrency
WRITE X WRITE X Management
SCHEDULE TA TB
READ X READ X
READ X READ X
WRITE X WRITE X
WRITE X WRITE X
a) T2 is executed completely before T1 starts, then sum X+Y will show the
correct assets:
b) T1 is executed completely before T2 starts, then sum X+Y will still show the
correct assets:
39
Structured Query Add 100 Add 100 100100
Language and
Transaction Management Write Y Write Y Y= 100100
Please note that for the given transaction there are many more ways of this interleaved
instruction execution.
Thus, there may be a problem when the transactions T1 and T2 are allowed to execute
in parallel. Let us define the problems of concurrent execution of transaction more
precisely.
Let us assume the following transactions (assuming there will not be errors in data
while execution of transactions)
Transaction T3 and T4: T3 reads the balance of account X and subtracts a withdrawal
amount of Rs. 5000, whereas T4 reads the balance of account X and adds an amount
of Rs. 3000
T3 T4
READ X READ X
SUB 5000 ADD 3000
WRITE X WRITE X
1. Lost Updates: Suppose the two transactions T3 and T4 run concurrently and
they happen to be interleaved in the following way (assume the initial value of X as
10000):
T3 T4 Value of X
T3 T4
READ X 10000
READ X 10000
SUB 5000 5000
ADD 3000 13000
WRITE X 5000
WRITE X 13000
After the execution of both the transactions the value X is 13000 while the
semantically correct value should be 8000. The problem occurred as the update made
by T3 has been overwritten by T4. The root cause of the problem was the fact that
both the transactions had read the value of X as 10000. Thus one of the two updates
has been lost and we say that a lost update has occurred.
There is one more way in which the lost updates can arise. Consider the following part
of some transactions:
T5 T6 Value of x originally
2000
T5 T6
UPDATE X 3000
UPDATE X 4000
ROLLBACK 2000
40
Here T5 & T6 updates the same item X. Thereafter T5 decides to undo its action and Transactions and
Concurrency
rolls back causing the value of X to go back to the original value that was 2000. In Management
this case also the update performed by T6 had got lost and a lost update is said to have
occurred.
2. Unrepeatable reads: Suppose T7 reads X twice during its execution. If it did
not update X itself it could be very disturbing to see a different value of X in its next
read. But this could occur if, between the two read operations, another transaction
modifies X.
T7 T8 Assumed value of
X=2000
T7 T8
READ X 2000
UPDATE X 3000
READ X 3000
Thus, the inconsistent values are read and results of the transaction may be in error.
3. Dirty Reads: T10 reads a value which has been updated by T9. This update has
not been committed and T9 aborts.
Here T10 reads a value that has been updated by transaction T9 that has been aborted.
Thus T10 has read a value that would never exist in the database and hence the
problem. Here the problem is primarily of isolation of transaction.
4. Inconsistent Analysis: The problem as shown with transactions T1 and T2
where two transactions interleave to produce incorrect result during an analysis by
Audit is the example of such a problem. This problem occurs when more than one
data items are being used for analysis, while another transaction has modified some of
those values and some are yet to be modified. Thus, an analysis transaction reads
values from the inconsistent state of the database that results in inconsistent analysis.
Thus, we can conclude that the prime reason of problems of concurrent transactions is
that a transaction reads an inconsistent state of the database that has been created by
other transaction.
But how do we ensure that execution of two or more transactions have not resulted in
a concurrency related problem?
Well one of the commonest techniques used for this purpose is to restrict access to
data items that are being read or written by one transaction and is being written by
another transaction. This technique is called locking. Let us discuss locking in more
detail in the next section.
41
Structured Query 2) What are the problems of concurrent transactions? Can these problems occur in
Language and
Transaction Management
transactions which do not read the same data values?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
3) What is a Commit state? Can you rollback after the transaction commits?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
If the operations of two transactions conflict with each other, how to determine that no
concurrency related problems have occurred? For this, serialisability theory has been
developed. Serialisability theory attempts to determine the correctness of the
schedules. The rule of this theory is:
42
Write X Write X Transactions and
Read Y Read Y Concurrency
Read Y Read Y Management
Add 100 Add 100
Display X+Y Display X+Y
Write Y Write Y
Now, we have to figure out whether this interleaved schedule would be performing
read and write in the same order as that of a serial schedule. If it does, then it is
equivalent to a serial schedule, otherwise not. In case it is not equivalent to a serial
schedule, then it may result in problems due to concurrent transactions.
Serialisability
Any schedule that produces the same results as a serial schedule is called a serialisable
schedule. But how can a schedule be determined to be serialisable or not? In other
words, other than giving values to various items in a schedule and checking if the
results obtained are the same as those from a serial schedule, is there an algorithmic
way of determining whether a schedule is serialisable or not?
The basis of the algorithm for serialisability is taken from the notion of a serial
schedule. There are two possible serial schedules in case of two transactions (T1- T2
OR T2 - T1). Similarly, in case of three parallel transactions the number of possible
serial schedules is 3!, that is, 6. These serial schedules can be:
Given a graph with no cycles in it, there must be a serial schedule corresponding to it.
43
Structured Query Let us use this algorithm to check whether the schedule as given in Figure 4 is
Language and
Transaction Management
Serialisable. Figure 5 shows the required graph. Please note as per step 1, we draw the
two nodes for T1 and T2. In the schedule given in Figure 4, please note that the
transaction T2 reads data item X, which is subsequently written by T1, thus there is an
edge from T2 to T1 (clause 2.1). Also, T2 reads data item Y, which is subsequently
written by T1, thus there is an edge from T2 to T1 (clause 2.1). However, that edge
already exists, so we do not need to redo it. Please note that there are no cycles in the
graph, thus, the schedule given in Figure 4 is serialisable. The equivalent serial
schedule (as per step 3) would be T2 followed by T1.
T1 T2
Please note that the schedule given in part (c) of section 2.3 is not serialsable, because
in that schedule, the two edges that exist between nodes T1 and T2 are:
• T1 writes X which is later read by T2 (clause 2.2), so there exists an edge from T1
to T2.
• T2 reads X which is later written by T1 (clause 2.1), so there exists an edge from
T2 to T1.
T1 T2
Please note that the graph above has a cycle T1-T2-T1, therefore it is not serialisable.
2.4.2 Locks
Serialisabilty is just a test whether a given interleaved schedule is ok or has a
concurrency related problem. However, it does not ensure that the interleaved
concurrent transactions do not have any concurrency related problem. This can be
done by using locks. So let us discuss about what the different types of locks are, and
then how locking ensures serialsability of executing transactions.
Types of Locks
There are two basic types of locks:
• Binary lock: This locking mechanism has two states for to a data item: locked
or unlocked
• Multiple-mode locks: In this locking type each data item can be in three states
read locked or shared locked, write locked or exclusive locked or unlocked.
Let us first take an example for binary locking and explain how it solves the
concurrency related problems. Let us reconsider the transactions T1 and T2 for this
purpose; however we will add to required binary locks to them.
44
Schedule T1 T2 Transactions and
Concurrency
Lock X Lock X Management
Read X Read X
Subtract 100 Subtract 100
Write X Write X
Unlock X Unlock X
Lock X Lock X
Lock Y Lock Y
Read X Read X
Read Y Read Y
Display X+Y Display X+Y
Unlock X Unlock X
Unlock Y Unlock Y
Lock Y Lock Y
Read Y Read Y
Add 100 Add 100
Write Y Write Y
Unlock Y Unlock Y
Figure 7: An incorrect locking implementation
Does the locking as done above solve the problem of concurrent transactions? No the
same problems still remains. Try working with the old value. Thus, locking should be
done with some logic in order to make sure that locking results in no concurrency
related problem. One such solution is given below:
Schedule T1 T2
Lock X Lock X
Lock Y Lock Y
Read X Read X
Subtract 100 Subtract 100
Write X Write X
Lock X (issued by Lock X: denied as T1 holds the lock.
T2) The transaction T2 Waits and T1
continues.
Read Y Read Y
Add 100 Add 100
Write Y Write Y
Unlock X Unlock X
The lock request of T2 on X can now
be granted it can resumes by locking X
Unlock Y Unlock Y
Lock Y Lock Y
Read X Read X
Read Y Read Y
Display X+Y Display X+Y
Unlock X Unlock X
Unlock Y Unlock Y
Figure 8: A correct but restrictive locking implementation
Thus, the locking as above when you obtain all the locks at the beginning of the
transaction and release them at the end ensures that transactions are executed with no
concurrency related problems. However, such a scheme limits the concurrency. We
will discuss a two-phase locking method in the next subsection that provides sufficient
concurrency. However, let us first discuss multiple mode locks.
45
Structured Query Multiple-mode locks: It offers two locks: shared locks and exclusive locks. But why
Language and
Transaction Management
do we need these two locks? There are many transactions in the database system that
never update the data values. These transactions can coexist with other transactions
that update the database. In such a situation multiple reads are allowed on a data item,
so multiple transactions can lock a data item in the shared or read lock. On the other
hand, if a transaction is an updating transaction, that is, it updates the data items, it has
to ensure that no other transaction can access (read or write) those data items that it
wants to update. In this case, the transaction places an exclusive lock on the data
items. Thus, a somewhat higher level of concurrency can be achieved in comparison
to the binary locking scheme.
The properties of shared and exclusive locks are summarised below:
a) Shared lock or Read Lock
• It is requested by a transaction that wants to just read the value of data item.
• A shared lock on a data item does not allow an exclusive lock to be placed but
permits any number of shared locks to be placed on that item.
b) Exclusive lock
Let us describe the above two modes with the help of an example. We will once again
consider the transactions T1 and T2 but in addition a transaction T11 that finds the
total of accounts Y and Z.
Schedule T1 T2 T11
S_Lock X S_Lock X
S_Lock Y S_Lock Y
Read X Read X
S_Lock Y S_Lock Y
S_Lock Z S_Lock Z
Read Y
Read Z
X_Lock X X_Lock X. The exclusive lock request on X is
denied as T2 holds the Read lock. The
transaction T1 Waits.
Read Y Read Y
Display X+Y Display X+Y
Unlock X Unlock X
X_Lock Y X_Lock Y. The previous exclusive lock request
on X is granted as X is unlocked. But the new
exclusive lock request on Y is not granted as Y
is locked by T2 and T11 in read mode. Thus T1
waits till both T2 and T11 will release the read
lock on Y.
Display Y+Z Display Y+Z
Unlock Y Unlock Y
Unlock Y Unlock Y
Unlock Z Unlock Z
Read X Read X
Subtract 100 Subtract 100
Write X Write X
Read Y Read Y
Add 100 Add 100
46
Write Y Write Y Transactions and
Concurrency
Unlock X Unlock X Management
Unlock Y Unlock Y
Thus, the locking as above results in a serialisable schedule. Now the question is can
we release locks a bit early and still have no concurrency related problem? Yes, we
can do it if we lock using two-phase locking protocol. This protocol is explained in
the next sub-section.
Phase 2: Lock Release Phase: The existing locks can be released in any order but no
new lock can be acquired after a lock has been released. The locks are
held only till they are required.
Normally the locks are obtained by the DBMS. Any legal schedule of transactions that
follows 2 phase locking protocol is guaranteed to be serialisable. The two phase
locking protocol has been proved for it correctness. However, the proof of this
protocol is beyond the scope of this Unit. You can refer to further readings for more
details on this protocol.
The basic 2PL allows release of lock at any time after all the locks have been
acquired. For example, we can release the locks in schedule of Figure 8, after we
have Read the values of Y and Z in transaction 11, even before the display of the sum.
This will enhance the concurrency level. The basic 2PL is shown graphically in
Figure 10.
Time Æ
Figure 10: Basic Two Phase Locking
However, this basic 2PL suffers from the problem that it can result into loss of atomic
/ isolation property of transaction as theoretically speaking once a lock is released on a
47
Structured Query data item it can be modified by another transaction before the first transaction
Language and
Transaction Management
commits or aborts.
To avoid such a situation we use strict 2PL. The strict 2PL is graphically depicted in
Figure 11. However, the basic disadvantage of strict 2PL is that it restricts
concurrency as it locks the item beyond the time it is needed by a transaction.
Time Æ
Figure 11: Strict Two Phase Locking
Does the 2PL solve all the problems of concurrent transactions? No, the strict 2PL
solves the problem of concurrency and atomicity, however it introduces another
problem: “Deadlock”. Let us discuss this problem in next section.
1) Let the transactions T1, T2, T3 be defined to perform the following operations:
……………………………………………………………………………………
……………………………………………………………………………………
2) Consider the following two transactions, given two bank accounts having a
balance A and B.
Transaction T1: Transfer Rs. 100 from A to B
Transaction T2: Find the multiple of A and B.
Create a non-serialisable schedule.
……………………………………………………………………………………
……………………………………………………………………………………
48
Transactions and
2.5 DEADLOCK AND ITS PREVENTION Concurrency
Management
As seen earlier, though 2PL protocol handles the problem of serialisability, but it
causes some problems also. For example, consider the following two transactions and
a schedule involving these transactions:
TA TB
X_lock A X_lock A
X_lock B X_lock B
: :
: :
Unlock A Unlock A
Unlock B Unlock B
Schedule
T1: X_lock A
T2: X_lock B
T1: X_lock B
T2: X_lock A
As is clearly seen, the schedule causes a problem. After T1 has locked A, T2 locks B
and then T1 tries to lock B, but unable to do so waits for T2 to unlock B. Similarly, T2
tries to lock A but finds that it is held by T1 which has not yet unlocked it and thus
waits for T1 to unlock A. At this stage, neither T1 nor T2 can proceed since both of
these transactions are waiting for the other to unlock the locked resource.
Clearly the schedule comes to a halt in its execution. The important thing to be seen
here is that both T1 and T2 follow the 2PL, which guarantees serialisability. So
whenever the above type of situation arises, we say that a deadlock has occurred,
since two transactions are waiting for a condition that will never occur.
Also, the deadlock can be described in terms of a directed graph called a “wait for”
graph, which is maintained by the lock manager of the DBMS. This graph G is
defined by the pair (V, E). It consists of a set of vertices/nodes V is and a set of
edges/arcs E. Each transaction is represented by node and an arc from Ti Æ Tj, if Tj
holds a lock and Ti is waiting for it. When transaction Ti requests a data item
currently being held by transaction Tj then the edge Ti Æ Tj is inserted in the "wait
for" graph. This edge is removed only when transaction Tjj is no longer holding the
data item needed by transaction Ti.
A deadlock in the system of transactions occurs, if and only if the wait-for graph
contains a cycle. Each transaction involved in the cycle is said to be deadlocked.
To detect deadlocks, a periodic check for cycles in graph can be done. For example,
the “wait-for” for the schedule of transactions TA and TB as above can be made as:
TA TB
49
Structured Query In the figure above, TA and TB are the two transactions. The two edges are present
Language and
Transaction Management
between nodes TA and TB since each is waiting for the other to unlock a resource
held by the other, forming a cycle, causing a deadlock problem. The above case shows
a direct cycle. However, in actual situation more than two nodes may be there in a
cycle.
A deadlock is thus a situation that can be created because of locks. It causes
transactions to wait forever and hence the name deadlock. A deadlock occurs because
of the following conditions:
Deadlock Prevention
One of the simplest approaches for avoiding a deadlock would be to acquire all the
locks at the start of the transaction. However, this approach restricts concurrency
greatly, also you may lock some of the items that are not updated by that transaction
(the transaction may have if conditions).Thus, better prevention algorithm have been
evolved to prevent a deadlock having the basic logic: not to allow circular wait to
occur. This approach rolls back some of the transactions instead of letting them wait.
There exist two such schemes. These are:
A timestamp may loosely be defined as the system generated sequence number that is
unique for each transaction. Thus, a smaller timestamp means an older transaction.
For example, assume that three transactions T1, T2 and T3 were generated in that
sequence, then if T1requests for a data item which is currently held by transaction T2,
it is allowed to wait as it has a smaller time stamping than that of T1. However, if T3
requests for a data item which is currently held by transaction T2, then T3 is rolled
back (die).
T1 T2 T3
Wait Die
50
“Wound-wait” scheme: It is based on a preemptive technique. It is based on a simple Transactions and
Concurrency
rule: Management
If Ti requests a database resource that is held by Tj
then if Ti has a larger timestamp (Ti is younger) than that of Tj
it is allowed to wait;
else Tj is wounded up by Ti.
For example, assume that three transactions T1, T2 and T3 were generated in that
sequence, then if T1requests for a data item which is currently held by transaction T2,
then T2 is rolled back and data item is allotted to T1 as T1 has a smaller time
stamping than that of T2. However, if T3 requests for a data item which is currently
held by transaction T2, then T3 is allowed to wait.
T1 T2 T3
Wound T2 Wait
It is important to see that whenever any transaction is rolled back, it would not make a
starvation condition, that is no transaction gets rolled back repeatedly and is never
allowed to make progress. Also both “wait-die” & “wound-wait” scheme avoid
starvation. The number of aborts & rollbacks will be higher in wait-die scheme than in
the wound-wait scheme. But one major problem with both of these schemes is that
these schemes may result in unnecessary rollbacks. You can refer to further readings
for more details on deadlock related schemes.
a) READ Phase: A transaction T reads the data items from the database into its
private workspace. All the updates of the transaction can only change the local
copies of the data in the private workspace.
b) VALIDATE Phase: Checking is performed to confirm whether the read values
have changed during the time transaction was updating the local values. This is
performed by comparing the current database values to the values that were read
in the private workspace. In case, the values have changed the local copies are
thrown away and the transaction aborts.
c) WRITE Phase: If validation phase is successful the transaction is committed and
updates are applied to the database, otherwise the transaction is rolled back.
51
Structured Query • Timestamps: for each transaction T, the start-time and the end time are kept
Language and
Transaction Management for all the three phases.
More details on this scheme are available in the further readings. But let us show this
scheme here with the help of the following examples:
Consider the set for transaction T1 and T2.
T1 T2
Phase Operation Phase Operation
- - Read Reads the read set (T2). Let say
variables X and Y and
performs updating of local
values
Read Reads the read set (T1) lets - -
say variable X and Y and
performs updating of local
values
Validate Validate the values of (T1) - -
- - Validate Validate the values of (T2)
Write Write the updated values in - -
the database and commit
- - Write Write the updated values in the
database and commit
In this example both T1 and T2 get committed. Please note that Read set of T1 and
Read Set of T2 are both disjoint, also the Write sets are also disjoint and thus no
concurrency related problem can occur.
T1 T2 T3
Operation Operation Operation
Read R(A) -- --
-- Read R(A) --
-- -- Read (D)
-- -- Update(D)
-- -- Update (A)
Validate (D,A) finds OK
-- --
Write (D,A), COMMIT
Validate(A):Unsuccessful
-- --
Value changed by T3
Validate(A):Unsuccessful
-- --
Value changed by T3
ABORT T1 -- --
-- Abort T2 --
In this case both T1 and T2 get aborted as they fail during validate phase while only
T3 is committed. Optimistic concurrency control performs its checking at the
transaction commits point in a validation phase. The serialization order is determined
by the time of transaction validation phase.
T1: S_lock A -- --
-- T2: X_lock B --
-- T2: S_lock C --
52
-- -- T3: X_lock C Transactions and
Concurrency
-- T2: S_lock A -- Management
T1: S_lock B -- --
T1: S_lock A -- --
-- -- T3: S_lock A
All the unlocking requests start from here
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
………………………………………………………………………………….
2.7 SUMMARY
In this unit you have gone through the concepts of transaction and Concurrency
Management. A transaction is a sequence of many actions. Concurrency control deals
with ensuring that two or more users do not get into each other’s way, i.e., updates of
transaction one doesn’t affect the updates of other transactions.
Serializability is the generally accepted criterion for correctness for the concurrency
control. It is a concept related to concurrent schedules. It determines how to analyse
whether any schedule is serialisable or not. Any schedule that produces the same
results as a serial schedule is a serialisable schedule.
Locks are of two type a) shared lock b) Exclusive lock. Then we move on to a method
known as Two Phase Locking (2PL). A system is in a deadlock state if there exist a
set of transactions such that every transaction in the set is waiting for another
transaction in the set. We can use a deadlock prevention protocol to ensure that the
system will never enter a deadlock state.
53
Structured Query
Language and
Transaction Management
2.8 SOLUTIONS / ANSWERS
A transaction can update more than one data values. Some transactions can do
writing of data without reading a data value.
A simple transaction example may be: Updating the stock inventory of an item
that has been issued. Please create a sample pseudo code for it.
1) There are six possible results, corresponding to six possible serial schedules:
Initially: A=0
T1-T2-T3: A=1
T1-T3-T2: A=2
T2-T1-T3: A=1
T2-T3-T1: A=2
T3-T1-T2: A=4
T3-T2-T1: A=3
2)
Schedule T1 T2
Read A Read A
A = A - 100 A = A - 100
Write A Write A
Read A Read A
Read B Read B
54
Read B Read B Transactions and
Concurrency
Result = A * B Result = A * B Management
Display Result Display Result
B = B + 100 B = B + 100
Write B Write B
Please make the precedence graph and find out that the schedule is not serialisable.
3)
Schedule T1 T2
Lock A Lock A
Lock B Lock B
Read A Read A
A = A - 100 A = A - 100
Write A Write A
Unlock A Unlock A
Lock A Lock A: Granted
Lock B Lock B: Waits
Read B Read B
B = B + 100 B = B + 100
Write B Write B
Unlock B Unlock B
Read A Read A
Read B Read B
Result = A * B Result = A * B
Display Result Display Result
Unlock A Unlock A
Unlock B Unlock B
You must make the schedules using read and exclusive lock and a schedule in strict
2PL.
1) The transaction T1 gets the shared lock on A, T2 gets exclusive lock on B and
Shared lock on A, while the transactions T3 gets exclusive lock on C.
The Wait for graph for the transactions for the given schedule is:
T1 T3
T2
55
Structured Query Since there exists a cycle, therefore, the schedule is deadlocked.
Language and
Transaction Management
2) The basic philosophy for optimistic concurrency control is the optimism that
nothing will go wrong so let the transaction interleave in any fashion, but to
avoid any concurrency related problem we just validate our assumption before
we make changes permanent. This is a good model for situations having a low
rate of transactions.
56
Database Recovery and
UNIT 3 DATABASE RECOVERY AND Security
SECURITY
Structure Page Nos.
3.0 Introduction 57
3.1 Objectives 57
3.2 What is Recovery? 57
3.2.1 Kinds of failures
3.2.2 Failure controlling methods
3.2.3 Database errors
3.3 Recovery Techniques 61
3.4 Security & Integrity 66
3.4.1 Relationship between Security and Integrity
3.4.2 Difference between Operating System and Database Security
3.5 Authorisation 68
3.6 Summary 71
3.7 Solutions/Answers 71
3.0 INTRODUCTION
In the previous unit of this block, you have gone through the concepts of transactions
and Concurrency management. In this unit we will introduce two important issues
relating to database management systems.
A computer system suffers from different types of failures. A DBMS controls very
critical data of an organisation and therefore must be reliable. However, the reliability
of the database system is linked to the reliability of the computer system on which it
runs. In this unit we will discuss recovery of the data contained in a database system
following failure of various types and present the different approaches to database
recovery. The types of failures that the computer system is likely to be subjected to
include failures of components or subsystems, software failures, power outages,
accidents, unforeseen situations and natural or man-made disasters. Database
recovery techniques are methods of making the database consistent till the last
possible consistent state. The aim of recovery scheme is to allow database operations
to be resumed after a failure with minimum loss of information at an economically
justifiable cost.
The second main issue that is being discussed in this unit is Database security.
“Database security” is protection of the information contained in the database against
unauthorised access, modification or destruction. The first condition for security is to
have Database integrity. “Database integrity” is the mechanism that is applied to
ensure that the data in the database is consistent.
3.1 OBJECTIVES
During the life of a transaction, that is, a after the start of a transaction but before the
transaction commits, several changes may be made in a database state. The database
during such a state is in an inconsistent state. What happens when a failure occurs at
this stage? Let us explain this with the help of an example:
Assume that a transaction transfers Rs.2000/- from A’s account to B’s account. For
simplicity we are not showing any error checking in the transaction. The transaction
may be written as:
Transaction T1:
READ A
A = A – 2000
WRITE A
Failure
READ B
B = B + 2000
WRITE B
COMMIT
What would happen if the transaction fails after account A has been written back to
database? As far as the holder of account A is concerned s/he has transferred the
money but that has never been received by account holder B.
What is the solution? In this case where the transaction has not yet committed the
changes made by it, the partial updates need to be undone.
The recovery mechanisms must ensure that a consistent state of database can be
restored under all circumstances. In case of transaction abort or deadlock, the system
remains in control and can deal with the failure but in case of a system failure the
system loses control because the computer itself has failed. Will the results of such
failure be catastrophic? A database contains a huge amount of useful information and
58
any system failure should be recognised on the restart of the system. The DBMS Database Recovery and
should recover from any such failures. Let us first discuss the kinds of failure for Security
identifying how to recover.
3.2.1 Kinds of Failures
The kinds of failures that a transaction program during its execution can encounter
are:
1) Software failures: In such cases, a software error abruptly stops the execution
of the current transaction (or all transactions), thus leading to losing the state of
program excution and the state/ contents of the buffers. But what is a buffer? A
buffer is the portion of RAM that stores the partial contents of database that is
currently needed by the transaction. The software failures can further be
subdivided as:
a) Statement or application program failure
b) Failure due to viruses
c) DBMS software failure
d) Operating system failure
The basic unit of recovery is a transaction. But, how are the transactions handled
during recovery? Consider that some transactions are deadlocked, then at least one of
these transactions has to be restarted to break the deadlock and thus the partial updates
made by such restarted program in the database need to be undone so that the
database does not go to an inconsistent state. So the transaction may have to be rolled
back which makes sure that the transaction does not bring the database to an
inconsistent state. This is one form of recovery. Let us consider a case when a
transaction has committed but the changes made by the transaction have not been
communicated to permanently stored physical database. A software failure now
occurs and the contents of the CPU/ RAM are lost. This leaves the database in an
inconsistent state. Such failure requires that on restarting the system the database be
brought to a consistent state using redo operation. The redo operation makes the
59
Structured Query changes made by the transaction again to bring the system to a consistent state. The
Language and
database system then can be made available to the users. The point to be noted here is
Transaction Management
that the database updates are performed in the buffer in the memory. Figure 1 shows
some cases of undo and redo. You can create more such cases.
A= 6000
…………
B= 8000
A= 6000 ………
B= 8000
Failures can be handled using different recovery techniques that are discussed later in
the unit. But the first question is do we really need recovery techniques as a failure
control mechanism? The recovery techniques are somewhat expensive both in terms
of time and in memory space for small systems. In such a case it is more beneficial to
better avoid the failure by some checks instead of deploying recovery technique to
make database consistent. Also, recovery from failure involves manpower that can be
used in some other productive work if failure can be avoided. It is, therefore,
important to find out some general precautions that help in controlling failure. Some
of these precautions may be:
• having a regulated power supply.
• having a better secondary storage system such as RAID.
• taking periodic backup of database states and keeping track of transactions after
each recorded state.
• properly testing the transaction programs prior to use.
• setting important integrity checks in the databases as well as user interfaces etc.
60
However, it may be noted that if the database system is critical it must use a DBMS Database Recovery and
that is suitably equipped with recovery procedures. Security
1) User error: This includes errors in the program (e.g., Logical errors) as well as
errors made by online users of database. These types of errors can be avoided
by applying some check conditions in programs or by limiting the access rights
of online users e.g., read only. So only updating or insertion operation require
appropriate check routines that perform appropriate checks on the data being
entered or modified. In case of an error, some prompts can be passed to user to
enable him/her to correct that error.
2) Consistency error: These errors occur due to the inconsistent state of database
caused may be due to wrong execution of commands or in case of a transaction
abort. To overcome these errors the database system should include routines
that check for the consistency of data entered in the database.
3) System error: These include errors in database system or the OS, e.g.,
deadlocks. Such errors are fairly hard to detect and require reprogramming the
erroneous components of the system software.
Database errors can result from failure or can cause failure and thus will require
recovery. However, one of the main tasks of database system designers is to make
sure that errors minimised. These concepts are also related to database integrity and
have also been discusses in a later section.
After going through the types of failures and database errors, let us discuss how to
recover from the failures. Recovery can be done using/restoring the previous
consistent state (backward recovery) or by moving forward to the next consistent state
as per the committed transactions (forward recovery) recovery. Please note that a
system can recover from software and hardware failures using the forward and
backward recovery only if the system log is intact. What is system log? We will
discuss it in more detail, but first let us define forward and backward recovery.
61
Structured Query
Language and
Transaction Management
Database with
changes
Database
UNDO
without
changes
Before
images
Database
without
changes REDO Database
with
changes
After
images
In simpler words, when a particular error in a system is detected, the recovery system
makes an accurate assessment of the state of the system and then makes the
appropriate adjustment based on the anticipated results - had the system been error
free.
One thing to be noted is that the Undo and Redo operations must be idempotent, i.e.,
executing them several times must be equivalent to executing them once. This
characteristic is required to guarantee correct behaviour of database even if a failure
occurs during the recovery process.
Let us first define the term transaction log in the context of DBMS. A transaction log
is a record in DBMS that keeps track of all the transactions of a database system that
update any data values in the database. A log contains the following information about
a transaction:
62
• The operations being performed by the transaction such as update, delete, Database Recovery and
insert. Security
• The data items or objects that are affected by the transaction including name of
the table, row number and column number.
• The before or previous values (also called UNDO values) and after or changed
values (also called REDO values) of the data items that have been updated.
• A pointer to the next transaction log record, if needed.
• The COMMIT marker of the transaction.
But how do we recover using log? Let us demonstrate this with the help of an example
having three concurrent transactions that are active on ACCOUNTS table as:
Assume that these transactions have the following log file (hypothetical) at a point:
Now assume at this point of time a failure occurs, then how the recovery of the
database will be done on restart.
63
Structured Query A 1000 1000 (assuming update has UNDO 1000
Language and
Transaction Management
not been done in physical
database)
Z 900 900 (assuming update has REDO 400
not been done in physical
database)
The selection of REDO or UNDO for a transaction for the recovery is done on the
basis of the state of the transactions. This state is determined in two steps:
• Look into the log file and find all the transactions that have started. For example,
in Figure 3, transactions T1, T2 and T3 are candidates for recovery.
• Find those transactions that have committed. REDO these transactions. All other
transactions have not committed so they should be rolled back, so UNDO them.
For example, in Figure 3 UNDO will be performed on T1 and T2; and REDO will
be performed on T3.
Please note that in Figure 4 some of the values may not have yet been communicated
to database, yet we need to perform UNDO as we are not sure what values have been
written back to the database.
But how will the system recover? Once the recovery operation has been specified, the
system just takes the required REDO or UNDO values from the transaction log and
changes the inconsistent state of database to a consistent state. (Please refer to
Figure 3 and Figure 4).
Let us consider several transactions with their respective start & end (commit) times
as shown in Figure 5.
T1
T2
T3
T4
t2
Failure time
64
Database Recovery and
T1 Security
T2
T3
T4
t1 t2 time
Checkpoint Failure
A checkpoint is taken at time t1 and a failure occurs at time t2. Checkpoint transfers
all the committed changes to database and all the system logs to stable storage (it is
defined as the storage that would not be lost). At restart time after the failure the stable
check pointed state is restored. Thus, we need to only REDO or UNDO those
transactions that have completed or started after the checkpoint has been taken. The
only possible disadvantages of this scheme may be that during the time of taking the
checkpoint the database would not be available and some of the uncommitted values
may be put in the physical database. To overcome the first problem the checkpoints
should be taken at times when system load is low. To avoid the second problem some
systems allow some time to the ongoing transactions to complete without restarting
new transactions.
• The transaction T1 will not be considered for recovery as the changes made by
it have already been committed and transferred to physical database at
checkpoint t1.
• The transaction T2 since it has not committed till the checkpoint t1 but have
committed before t2, will be REDONE.
• T3 must be UNDONE as the changes made by it before checkpoint (we do not
know for sure if any such changes were made prior to checkpoint) must have
been communicated to the physical database. T3 must be restarted with a new
name.
• T4 started after the checkpoint, and if we strictly follow the scheme in which
the buffers are written back only on the checkpoint, then nothing needs to be
done except restarting the transaction T4 with a new name.
The restart of a transaction requires the log to keep information of the new name of
the transaction and maybe give higher priority to this newer transaction.
But one question is still unanswered that is during a failure we lose database
information in RAM buffers, we may also lose log as it may also be stored in RAM
buffers, so how does log ensure recovery?
The answer to this question lies in the fact that for storing transaction log we follow a
Write Ahead Log Protocol. As per this protocol, the transaction logs are written to
stable storage before any item is updated. Or more specifically it can be stated as; the
undo potion of log is written to stable storage prior to any updates and redo
portion of log is written to stable storage prior to commit.
Log based recovery scheme can be used for any kind of failure provided you have
stored the most recent checkpoint state and most recent log as per write ahead log
65
Structured Query protocol into the stable storage. Stable storage from the viewpoint of external failure
Language and
requires more than one copy of such data at more than one location. You can refer to
Transaction Management
the further readings for more details on recovery and its techniques.
66
1) Physical: The site or sites containing the computer system must be physically Database Recovery and
secured against illegal entry of unauthorised persons. Security
USER
Information modification
Integrity No
Violation Integrity
Violation
67
Structured Query 3.4.2 Difference between Operating System and Database Security
Language and
Transaction Management
Security within the operating system can be implemented at several levels ranging
from passwords for access to system, to the isolation of concurrent executing
processes with the system. However, there are a few differences between security
measures taken at operating system level as compared to those that of database
system. These are:
• Database system protects more objects, as the data is persistent in nature. Also
database security is concerned with different levels of granularity such as file,
tuple, an attribute value or an index. Operating system security is primarily
concerned with the management and use of resources.
• Database system objects can be complex logical structures such as views, a
number of which can map to the same physical data objects. Moreover different
architectural levels viz. internal, conceptual and external levels, have different
security requirements. Thus, database security is concerned with the semantics –
meaning of data, as well as with its physical representation. Operating system can
provide security by not allowing any operation to be performed on the database
unless the user is authorized for the operation concerned.
Figure 8 shows the architecture of a database security subsystem that can be found in
any commercial DBMS.
USERS
Untrusted DBMS
3.5 AUTHORISATION
68
4) DELETE: allows deletion of data only. Database Recovery and
Security
A user may be assigned all, none or a combination of these types of Authorisation,
which are broadly called access authorisations.
You must refer to Section 1.5 of Unit 1 of this block for the SQL commands relating
to data and user control.
The ultimate form of authority is given to the database administrator. He is the one
who may authorize new users, restructure the database and so on. The process of
Authorisation involves supplying information known only to the person the user has
claimed to be in the identification procedure.
A basic model of Database Access Control
Models of database access control have grown out of earlier work on protection in
operating systems. Let us discuss one simple model with the help of the following
example:
Assuming there are two users: Personnel manager and general user . What access
rights may be granted to each user? One extreme possibility is to grant an
unconstrained access or to have a limited access.
Unconstrained Object
access Strictly limited
access
One of the most influential protection models was developed by Lampson and
extended by Graham and Denning. This model has 3 components:
1) A set of objects: where objects are those entities to which access must be
controlled.
2) A set of subjects: where subjects are entities that request access to objects.
3) A set of all access rules: which can be thought of as forming an access (often
referred to as authorisation matrix).
69
Structured Query Let us create a sample authorisation matrix for the given relation:
Language and
Transaction Management
Object Empno Name Address Deptno Salary Assessment
Subject
Personnel Read All All All All All
Manager
General Read Read Read Read Not Not
User accessible accessible
As the above matrix shows, Personnel Manager and general user are the two subjects.
Objects of database are Empno, Name, Address, Deptno, Salary and Assessment. As
per the access matrix, Personnel Manager can perform any operation on the database
of an employee except for updating the Empno that may be self-generated and once
given can never be changed. The general user can only read the data but cannot
update, delete or insert the data into the database. Also the information about the
salary and assessment of the employee is not accessible to the general user.
In summary, it can be said that the basic access matrix is the representation of basic
access rules. These rules may be implemented using a view on which various access
rights may be given to the users.
1) What are the different types of data manipulation operations and control
operations?
……………………………………………………………………………………
……..…………………………………………………………………………….
………..……..……………………………………………………………………
……………..……..…..………………………………………………………….
……………………..……..……..……………………………………………….
……………………………………………………………………………………
……………………………………………………………………………………
2) What is the main difference between data security and data integrity?
……………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………
70
4) Name the 3 main components of Database Access Control Model? Database Recovery and
Security
……………………………………………………………………………………
……………………………………………………………………………………
……….………...…………………………………………………………………
……………………………………………………………………………………
3.6 SUMMARY
In this unit we have discussed the recovery of the data contained in a database system
after failures of various types. The types of failures that the computer system is likely
to be subject to include that of components or subsystems, software failures, power
outages, accidents, unforeseen situations, and natural or man-made disasters.
Database recovery techniques are methods of making the database fault tolerant. The
aim of the recovery scheme is to allow database operations to be resumed after a
failure with the minimum loss of information and at an economically justifiable cost.
Security and integrity concepts are crucial since modifications in a database require
the replacement of the old values. The DBMS security mechanism restricts users to
only those pieces of data that are required for the functions they perform. Security
mechanisms restrict the type of actions that these users can perform on the data that is
accessible to them. The data must be protected from accidental or intentional
(malicious) corruption or destruction. In addition, there is a privacy dimension to data
security and integrity.
3.7 SOLUTIONS/ANSWERS
1) Recovery is needed to take care of the failures that may be due to software,
hardware and external causes. The aim of the recovery scheme is to allow
database operations to be resumed after a failure with the minimum loss of
information and at an economically justifiable cost. One of the common
techniques is log-based recovery. A transaction is the basic unit of recovery.
2) A checkpoint is a point when all the database updates and logs are written to
stable storage. A checkpoint ensures that not all the transactions need to be
REDONE or UNDONE. Thus, it helps in faster recovery from failure. You
should create a sample example, for checkpoint having a sample transaction
log.
71
Structured Query • Loss of data should be minimal
Language and
Transaction Management • Recovery should be quick
• Recovery should be automatic
• Affect small portion of database.
• Read
• Insert
• Delete
• Update
• Objects
• Subjects
• Access rights
72
Distributed and Client
UNIT 4 DISTRIBUTED AND CLIENT Server Databases
SERVER DATABASES
Structure Page Nos.
4.0 Introduction 73
4.1 Objectives 73
4.2 Need for Distributed Database Systems 73
4.3 Structure of Distributed Database 74
4.4 Advantages and Disadvantages of DDBMS 78
4.4.1 Advantages of Data Distribution
4.4.2 Disadvantages of Data Distribution
4.5 Design of Distributed Databases 81
4.5.1 Data Replication
4.5.2 Data Fragmentation
4.6 Client Server Databases 87
4.6.1 Emergence of Client Server Architecture
4.6.2 Need for Client Server Computing
4.6.3 Structure of Client Server Systems
4.6.4 Advantages of Client Server Systems
4.7 Summary 91
4.8 Solutions/Answers 92
4.0 INTRODUCTION
In the previous units, we have discussed the basic issues relating to Centralised
database systems. This unit discusses the distributed database systems which are
primarily relational and one important implementation model: the client server model.
This unit focuses on the basic issues involved in the design of distributed database
designs. The unit also discusses some very basic concepts; in respect of client server
computing including the basic structure, advantages/disadvantages etc. It will be
worth mentioning here that almost all the commercial database management systems
follow such a model of implementation. Although in client server model one of the
concepts is the concept of distribution but it is primarily distribution of roles on server
computers or rather distribution of work. So a client server system may be a
centralised database.
4.1 OBJECTIVES
The processors in a distributed system may vary in size and function such as small
microcomputers, workstation, minicomputers, and large general-purpose computer
system. These processors are referred to by sites, nodes, computers, and so on,
depending on the context in which they are mentioned. We mainly use the term site, in
order to emphasise the physical distribution of these systems.
Independent or decentralised systems were normally used in earlier days. There was
duplication of hardware and other facilities. Evolution of computer systems also led to
incompatible procedures and lack of management control. A centralised database
system was then evolved. In a centralised database the DBMS and data reside at a
single database instance. Although for recovery purposes we keep redundant database
information yet it is under the control of a single DBMS. A further enhancement of
the centralised database system may be to provide access to centralised data from a
number of distributed locations through a network. In such a system a site failure
except the central site will not result in total system failure. Although, communication
technology has greatly improved yet the centralised approach may create problems for
an organization that has geographically dispersed operations and data has to be
accessed from a centralised database. Some of the problems may be:
74
participate in the execution of global transactions, those transactions that access data Distributed and Client
Server Databases
at several sites. The architecture of Distributed Database systems is given in Figure 1
Figure 1: Three different database system architectures. (a) No database sharing architecture.
(b) A networked architecture with a centralised database.
(c) A distributed database architecture
75
Structured Query The execution of global transactions on the distributed architecture requires
Language and
Transaction Management
communication among the sites. Figure 2 illustrates a representative distributed
database system architecture having transactions. Please note that both the
transactions shown are global in nature as they require data from both the sites.
Well we will provide the basic answers to most of these questions during the course of
this unit. However, for more details, you may refer to further readings.
76
The sites in a distributed system can be connected physically in a variety of ways. The Distributed and Client
Server Databases
various topologies are represented as graphs whose nodes correspond to sites. An edge
from node A to node B corresponds to a direct connection between the two sites.
Some of the most common connection structures are depicted in Figure 3. The major
differences among these configurations involve:
• Installation cost. The cost of physically linking the sites in the system
• Communication cost. The cost in time and money to send a message from site
A to site B.
• Availability. The degree to which data can be accessed despite failure of some
links or sites.
These differences play an important role in choosing the appropriate mechanism for
handling the distribution of data The sites of a distributed database system may be
distributed physically either over a large geographical area (such as all over India), or
over a small geographical area such as a single building or a number of adjacent
buildings). The former type of network is based on wide area network, while the latter
uses local-area network.
77
Structured Query Since the sites in wide area networks are distributed physically over a large
Language and
Transaction Management
geographical area, the communication links are likely to be relatively slow and less
reliable as compared with local-area networks. Typical wide area links are telephone
lines, microwave links, and satellite channels. Many newer enhanced communication
technologies including fiber optics are also used for such links. The local-area
network sites are close to each other, communication links are of higher speed and
lower error rate than their counterparts in wide area networks. The most common
links are twisted pair, base band coaxial, broadband coaxial, and fiber optics.
A Distributed Transaction
Let us illustrate the concept of a distributed transaction by considering a banking
system consisting of three branches located in three different cities. Each branch has
its own computer with a database consisting of all the accounts maintained at that
branch. Each such installation is thus a site. There also exists one single site which
maintains information about all the other branches of the bank. Suppose that the
database systems at the various sites are based on the relational model. Each branch
maintains its portion of the relation: DEPOSIT (DEPOSIT-BRANCH) where
A site containing information about the four branches maintains the relation branch-
details, which has the schema as:
There are other relations maintained at the various sites which are ignored for the
purpose of our example.
A local transaction is a transaction that accesses accounts in one single site, at which
the transaction was initiated. A global transaction, on the other hand, is one which
either accesses accounts in a site different from the one at which the transaction was
initiated, or accesses accounts in several different sites. To illustrate the difference
between these two types of transactions, consider the transaction to add Rs.5000 to
account number 177 located at the Delhi branch. If the transaction was initiated at the
Delhi branch, then it is considered local; otherwise, it is considered global. A
transaction to transfer Rs.5000 from account 177 to account 305, which is located at
the Bombay branch, is a global transaction since accounts in two different sites are
accessed as a result of its execution. A transaction finding the total financial standing
of all branches is global.
What makes the above configuration a distributed database system are the facts that:
• The various sites may be locally controlled yet are aware of each other.
• Each site provides an environment for executing both local and global
transactions.
78
cost, greater potential for bugs, and increased processing overheads. In this section, Distributed and Client
Server Databases
we shall elaborate briefly on each of these.
Improved Reliability
In a centralised DBMS, a server failure terminates the operations of the DBMS.
However, a failure at one site of a DDBMS, or a failure of a communication link
making some sites inaccessible, does not make the entire system inoperable.
Distributed DBMSs are designed to continue to function despite such failures. In
particular, if data are replicated in several sites, a transaction needing a particular data
item may find it at several sites. Thus, the failure of a site does not necessarily imply
the shutdown of the system.
The failure of one site must be detected by the system, and appropriate action may be
needed to recover from the failure. The system must no longer use the services of the
failed site. Finally, when the failed site recovers or is repaired, mechanisms must be
available to integrate it smoothly back into the system. The recovery from failure in
distributed systems is much more complex than in a centralised system.
Improved availability
The data in a distributed system may be replicated so that it exists at more than one
site. Thus, the failure of a node or a communication link does not necessarily make the
data inaccessible. The ability of most of the systems to continue to operate despite the
failure of one site results in increased availability which is crucial for database
79
Structured Query systems used for real-time applications. For example, loss of access to data in an
Language and
airline may result in the loss of potential ticket buyers to competitors.
Transaction Management
Improved performance
As the data is located near the site of its demand, and given the inherent parallelism
due to multiple copies, speed of database access may be better for distributed
databases than that of the speed that is achievable through a remote centralised
database. Furthermore, since each site handles only a part of the entire database, there
may not be the same contention for CPU and I/O services as characterized by a
centralised DBMS.
• Greater potential for bugs: Since the sites of a distributed system operate
concurrently, it is more difficult to ensure the correctness of algorithms. The art
of constructing distributed algorithms is an active and important area of
research.
80
• Complexity: A distributed DBMS that is reliable, available and secure is Distributed and Client
Server Databases
inherently more complex than a centralised DBMS. Replication of data
discussed in the next section, also adds to complexity of the distributed DBMS.
However, adequate data replication is necessary to have availability, reliability,
and performance.
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
2) Differntiate between global and local transaction.
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
The distributed databases are primarily relational at local level. So a local database
schema is the same as that of a centralised database design. However, a few more
dimensions have been added to the design of distributed database. These are:
81
Structured Query • Fragmentation: It is defined as partitioning of a relation into several fragments.
Language and
Transaction Management Each fragment can be stored at a different site.
• Increased parallelism: Since the replicated date has many copies a query can
be answered from the least loaded site or can be distributed. Also, with more
replicas you have greater chances that the needed data is found on the site where
the transaction is executing. Hence, data replication can minimise movement of
data between sites.
82
Use of partial data by applications: In general, applications work with views Distributed and Client
Server Databases
rather than entire relations. Therefore, it may be more appropriate to work with
subsets of relations rather than entire data.
Increases efficiency: Data is stored close to most frequently used site, thus retrieval
would be faster. Also, data that is not needed by local applications is not stored, thus
the size of data to be looked into is smaller.
Parallelism of transaction execution: A transaction can be divided into several
sub-queries that can operate on fragments in parallel. This increases the degree of
concurrency in the system, thus allowing transactions to execute efficiently.
Security: Data not required by local applications is not stored at the site, thus no
unnecessary security violations may exist.
But how do we carry out fragmentation? Fragmentation may be carried out as per the
following rules:
a) Completeness: This rule ensures that there is no loss of data during
fragmentation. If a relation is decomposed into fragments, then each data item
must appear in at least one fragment.
b) Reconstruction: This rule ensures preservation of functional dependencies.
It should be possible to define a relational operation that will reconstruct the
relation from its fragments.
Horizontal Fragmentation
Horizontal fragmentation groups together the tuples in a relation that are collectively
used by the important transactions. A horizontal fragment is produced by specifying a
WHERE clause condition that performs a restriction on the tuples in the relation. It
can also be defined using the Selection operation of the relational algebra.
83
Structured Query Example:
Language and
Transaction Management
Let us illustrate horizontal fragmentation with the help of an example.
So let us decompose the table in Figure 5 into horizontal fragments. Let us do these
fragments on the branch-code as 1101 and 1102
These two fragments are shown in Figure 6. Fragment 1 can be stored in the branch
whose code is 1101 while the second fragment can be stored at branch 1102.
In our example, the fragments are disjoint. However, by changing the selection
predicates used to construct the fragments; we may have overlapping horizontal
fragments. This is a form of data replication.
Vertical Fragmentation
Vertical fragmentation groups together only those attributes in a relation that are used
jointly by several important transactions. A vertical fragment is defined using the
Projection operation of the relational algebra. In its most simple form, vertical
84
fragmentation is the same as that of decomposition. In general, a relation can be Distributed and Client
Server Databases
constructed on taking Natural join of all vertical fragments.
This relation now can be decomposed into two fragments as: shows a vertical
decomposition of the scheme Deposit-scheme tuple number into:
DEPOSIT3
Branch-code Customer-name Tuple-number
1101 Suresh 1
1101 Swami 2
1102 Swami 3
1102 Khan 4
1101 Khan 5
1102 Khan 6
1102 Khan 7
DEPOSIT4
Account number Balance Tuple-number
3050 5000 1
2260 3360 2
1170 2050 3
4020 10000 4
1550 620 5
4080 1123 6
6390 7500 7
Figure 8: Vertical fragmentation of relation DEPOSIT
How can we reconstruct the original relation from these two fragments? By taking
natural join of the two vertical fragments on tuple-number. The tuple number allows
direct retrieval of the tuples without the need for an index. Thus, this natural join may
be computed much more efficiently than typical natural join.
However, please note that as the tuple numbers are system generated, therefore they
should not be visible to general users. If users are given access to tuple-number, it
becomes impossible for the system to change tuple addresses.
85
Structured Query Mixed fragmentation
Language and
Transaction Management Sometimes, horizontal or vertical fragmentation of a database schema by itself is
insufficient to adequately distribute the data for some applications. Instead, mixed or
hybrid fragmentation is required. Mixed fragmentation consists of a horizontal
fragment that is vertically fragmented, or a vertical fragment that is then horizontally
fragmented.
(1) Examine the nature of distribution. Find out whether an organisation needs to
have a database at each branch office, or in each city, or possibly at a regional
level. It has direct implication from the viewpoint of fragmentation. For
example, in case database is needed at each branch office, the relations may
fragment on the basis of branch number.
(2) Create a detailed global E-R diagram, if so needed and identify relations from
entities.
(3) Analyse the most important transactions in the system and identify where
horizontal or vertical fragmentation may be desirable and useful.
(4) Identify the relations that are not to be fragmented. Such relations will be
replicated everywhere. From the global ER diagram, remove the relations that
are not going to be fragmented.
(5) Examine the relations that are on one-side of a relationship and decide a
suitable fragmentation schema for these relations. Relations on the many-side
of a relationship may be the candidates for derived fragmentation.
(6) During the previous step, check for situations where either vertical or mixed
fragmentation would be needed, that is, where the transactions require access to
a subset of the attributes of a relation.
86
Distributed and Client
4.6 CLIENT SERVER DATABASES Server Databases
The client-server systems are connected though the network. This network need not
only be the Local Area Network (LAN). It can also be the Wide Area Network
(WAN) across multiple cities. The client and server machines communicate through
standard application program interfaces (called API), and remote procedure calls
(RPC). The language through which RDBMS based C/S environment communicate is
the structured query language (SQL).
• The bottom layer provides the generalized services needed by the other layers
including file services, print services, communications services and database
services. One example of such a service may be to provide the records of
customer accounts.
These functional units can reside on either the client or on one or more servers in the
application:
88
Two-tier client/server provides the user system interface usually on the desktop Distributed and Client
Server Databases
environment to its users. The database management services are usually on the server
that is a more powerful machine and services many clients. Thus, 2-Tier client-server
architecture splits the processing between the user system interface environment and
the database management server environment. The database management server also
provides stored procedures and triggers. There are a number of software vendors that
provide tools to simplify the development of applications for the two-tier client/server
architecture.
In 2-tier client/server applications, the business logic is put inside the user interface on
the client or within the database on the server in the form of stored procedures. This
results in division of the business logic between the client and server. File servers and
database servers with stored procedures are examples of 2-tier architecture.
3-tier architecture
As the number of clients increases the server would be filled with the client requests.
Also, because much of the processing logic was tied to applications, changes in
business rules lead to expensive and time-consuming alterations to source code.
Although the ease and flexibility of two-tier architecture tools continue to drive many
small-scale business applications, the need for faster data access and rapid
developmental and maintenance timelines has persuaded systems developers to seek
out a new way of creating distributed applications.
The three-tier architecture emerged to overcome the limitations of the two tier
architecture (also referred to as the multi-tier architecture). In the three-tier
architecture, a middle tier was added between the user system interface client
environment and the database management server environment. The middle tier may
consist of transaction processing monitors, message servers, or application servers.
The middle-tier can perform queuing, application execution, and database staging. For
example, on a middle tier that provides queuing, the client can deliver its request to
the middle layer and simply gets disconnected because the middle tier will access the
data and return the answer to the client. In addition the middle layer adds scheduling
and prioritisation for various tasks that are currently being performed.
The three-tier client/server architecture has improved performance for client groups
with a large number of users (in the thousands) and improves flexibility when
compared to the two-tier approach. Flexibility is in terms of simply moving an
application on to different computers in three-tier architecture. It has become as
simple as “drag and drop” tool. Recently, mainframes have found a new use as servers
in three-tier architectures:
In 3-tier client/server applications, the business logic resides in the middle tier,
separate from the data and user interface. In this way, processes can be managed and
deployed separately from the user interface and the database. Also, 3-tier systems can
integrate data from multiple sources.
90
• One major advantage of the client/server model is that by allowing multiple users Distributed and Client
Server Databases
to simultaneously access the same application data, updates from one computer
are instantly made available to all computers that had access to the server.
Since PCs can be used as clients, the application can be connected to the spreadsheets
and other applications through Dynamic Data Exchange (DDE) and Object Linking
and Embedding (OLE). If the load on database machine grows, the same application
can be run on a slightly upgraded machine provided it offers the same version of
RDBMSs.
A more detailed discussion on these topics is beyond the scope of this unit. You can
refer to further readings for more details.
………………………………………………………………………………….
………………………………………………………………………………….
2) Describe the Architecture of Client/Server system.
………………………………………………………………………….
………………………………………………………………………….
4.7 SUMMARY
There are several reasons for building distributed database systems, including sharing
of data, reliability and availability, and speed of query processing. However, along
with these advantages come several disadvantages, including software development
cost, greater potential for bugs, and increased processing overheads. The primary
disadvantage of distributed database systems is the added complexity required to
ensure proper co-ordination among the sites.
There are several issues involved in storing a relation in the distributed database,
including replication and fragmentation. It is essential that the system minimise the
degree to which a user needs to be aware of how a relation is stored.
Companies that have moved out of the mainframe system to Client/Server architecture
have found three major advantages:
91
Structured Query
Language and
Transaction Management
4.8 SOLUTIONS/ANSWERS
2)
1) Fragmentation cannot be carried out haphazardly. There are three rules that must
be followed during fragmentation.
92
Availability: If one of the sites containing relation R fails, then the relation R Distributed and Client
Server Databases
may found in another site.
Increased parallelism: In cases where the majority of access to the relation R
results in only the reading of the relation, several sites can process queries
involving R in parallel.
Increasing overhead on update: The system must ensure that all replicas of a
relation R are consistent, otherwise erroneous computations may result. This
implies that whenever R is updated, this update must be propagated to all sites
containing replicas, resulting in increased overheads.
With two-tier client/server, the user system interface is usually located in the
user’s desktop environment and the database management services are usually
in a server. Processing management is spilt between the user system interface
environment and the database management server environment. The database
management server provides stored procedures and triggers. In 2-tier
client/server applications, the business logic is buried inside the user interface
on the client or
within the database on the server in the form of stored procedures.
Alternatively, the business logic can be divided between client and server.
3-tier architecture
The three-tier architecture (also referred to as the multi-tier architecture) emerged to
overcome the limitations of the two-tier architecture. In the three-tier architecture, a
middle tier was added between the user system interface client environment and the
database management server environment. There are a variety of ways of
implementing this middle tier, such as transaction processing monitors, message
servers, or application servers. The middle tier can perform queuing, application
execution, and database staging. In addition the middle layer adds scheduling and
prioritization for work in progress. The three-tier client/server architecture has been
shown to improve performance for groups with a large number of users (in the
thousands) and improves flexibility when compared to the two-tier approach.
93
Structured Query In 3-tier client/server application, the business logic resides in the middle tier, separate
Language and
Transaction Management
from the data and user interface. In this way, processes can be managed and deployed
separately from the user interface and the database. Also, 3-tier systems can integrate
data from multiple sources.
94