Professional Documents
Culture Documents
Concurrency
1.1 Introduction
In information technology and computer science, especially in the fields of computer
programming, operating systems, multiprocessors, and databases, concurrency control ensures
that correct results for concurrent operations are generated, while getting those results as quickly
as possible.
Computer systems, both software and hardware, consist of modules, or components. Each
component is designed to operate correctly, i.e., to obey or to meet certain consistency rules.
When components that operate concurrently interact by messaging or by sharing accessed data
(in memory or storage), a certain component's consistency may be violated by another
component. The general area of concurrency control provides rules, methods, design
methodologies, and theories to maintain the consistency of components operating concurrently
while interacting, and thus the consistency and correctness of the whole system. Introducing
concurrency control into a system means applying operation constraints which typically result in
some performance reduction. Operation consistency and correctness should be achieved with as
good as possible efficiency, without reducing performance below reasonable levels. Concurrency
control can require significant additional complexity and overhead in a concurrent algorithm
compared to the simpler sequential algorithm.
For example, a failure in concurrency control can result in data corruption from torn read or
write operations.
Multiple transactions are allowed to run concurrently in the system. Advantages are:
Concurrency control schemes mechanisms to achieve isolation; that is, to control the
interaction among the concurrent transactions in order to prevent them from destroying the
consistency of the database.
1.2 Schedule
Schedule a sequences of instructions that specify the chronological order in which instructions
of concurrent transactions are executed.
a schedule for a set of transactions must consist of all instructions of those transactions.
1|Page
must preserve the order in which the instructions appear in each individual transaction.
A transaction that successfully completes its execution will have a commit instructions as the
last statement (will be omitted if it is obvious).
A transaction that fails to successfully complete its execution will have an abort instructions as
the last statement (will be omitted if it is obvious).
1.3 Serializability
For correctness, a common major goal of most concurrency control mechanisms is
generating schedules with the Serializability property. Without serializability undesirable
phenomena may occur, e.g., money may disappear from accounts, or be generated from
nowhere. Serializability of a schedule means equivalence (in the resulting database values) to
some serial schedule with the same transactions (i.e., in which transactions are sequential with
no overlap in time, and thus completely isolated from each other: No concurrent access by any
two transactions to the same data is possible). Serializability is considered the highest level
of isolation among database transactions, and the major correctness criterion for concurrent
transactions. In some cases compromised, relaxed forms of serializability are allowed for better
performance
(e.g.,
the
popular Snapshot
isolation mechanism)
or
to
meet availability requirements in highly distributed systems (see Eventual consistency), but only
if application's correctness is not violated by the relaxation (e.g., no relaxation is allowed
for money transactions, since by relaxation money can disappear, or appear from nowhere).
Almost all implemented concurrency control mechanisms achieve serializability by
providing Conflict serializablity, a broad special case of serializability (i.e., it covers, enables
most serializable schedules, and does not impose significant additional delay-causing
constraints) which can be implemented efficiently.
2|Page
Intuitively, a conflict between Ii and Ij forces a (logical) temporal order between them. If Ii and
Ij are consecutive in a schedule and they do not conflict, their results would remain the same
even if they had been interchanged in the schedule.
If a schedule S can be transformed into a schedule S by a series of swaps of non-conflicting
instructions, we say that S and S are conflict equivalent.
We say that a schedule S is serializable if it is conflict equivalent to a serial schedule.
Schedule S3 can be transformed into S6, a serial schedule where T2 follows T1 , by series of
swaps of non-conflicting instructions.
Therefore Schedule S3 is serializable.
Schedule S3
T1
T2
Read(A)
Write(A)
Read(A)
Write(A)
Schedule S6
T1
T2
Read(A)
Write(A)
Read(B)
Write( B)
Read(B)
Write(B)
Read(B)
Write(B)
Read(A)
Write(A)
Read(B)
Write( B)
serializable, and
are recoverable and preferably cascadeless .
A policy in which only one transaction can execute at a time generates serial schedules, but
provides a poor degree of concurrency and low throughput. Are serial schedules
recoverable/cascadeless?
Testing a schedule for serializability after it has executed is a little too late!
Goal to develop concurrency control protocols that will assure serializability.
In a multiprogramming environment where multiple transactions can be executed
simultaneously, it is highly important to control the concurrency of transactions. We have
3|Page
Binary Locks A lock on a data item can be in two states; it is either locked or
unlocked.
Lock requests are made to concurrency-control manager. Transaction can proceed only after
request is granted. A transaction may be granted a lock on an item if the requested lock is
compatible with locks already held on the item by other transactions. Any number of
transactions can hold shared locks on an item, but if any transaction holds an exclusive on the
item no other transaction may hold any lock on the item. If a lock cannot be granted, the
requesting transaction is made to wait till all incompatible locks held by other transactions have
been released. The lock is then granted.
Example of a transaction with locking:
T2 : lock-S(A);
read(A);
unlock(A);
lock-S(B);
4|Page
read(B);
unlock(B);
display(A+B);
Locking as above is not sufficient to guarantee serializability
if A and B get updated in-between the read of A and B, the displayed sum would be
wrong.
A locking protocol is a set of rules followed by all transactions while requesting and
releasing locks. Locking protocols restrict the set of possible schedules. Locking may be
dangerous.
1.4.1.1
5|Page
This locking protocol divides the execution phase of a transaction into three parts. In the first
part, when the transaction starts executing, it seeks permission for the locks it requires. The
second part is where the transaction acquires all the locks. As soon as the transaction releases its
first lock, the third phase starts. In this phase, the transaction cannot demand any new locks; it
only releases the acquired locks.
Two-phase locking has two phases, one is growing, where all the locks are being acquired by
the transaction; and the second phase is shrinking, where the locks held by the transaction are
being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock and then
upgrade it to an exclusive lock.
1.4.1.2
The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first phase, the
transaction continues to execute normally. But in contrast to 2PL, Strict-2PL does not release a
lock after using it. Strict-2PL holds all the locks until the commit point and releases all the locks
at a time.
6|Page
1.4.2.1
Operation rejected.
Operation executed.
7|Page
Operation rejected.
8|Page
Optimistic Execution: It perform read and compute operation without validation and perform
validation just before write operation.
Read Composite Validate Write
1.4.3.1
1.4.3.2
1.4.3.3
Only suitable for environments where there are few conflicts and no long transactions.
Acceptable for mostly Read or Query database systems that requires very few update
transactions.
case is a temporal database, which keeps track of all changes and the times at which they
occurred. In such cases, there is no additional storage penalty for multiversion techniques, since
older versions are already maintained.
Multiversion Technique Based on Timestamp Ordering
In this method, several versions , , ..., of each data item X are maintained. For each version, the
value of version and the following two timestamps are kept:
1. read_TS: The read timestamp of is the largest of all the timestamps of transactions that have
successfully read version .
2. write_TS: The write timestamp of is the timestamp of the transaction that wrote the value of
version .
Whenever a transaction T is allowed to execute a write_item(X) operation, a new version of
item X is created, with both the write_TS and the read_TS set to TS(T). Correspondingly, when a
transaction T is allowed to read the value of version Xi, the value of read_TS() is set to the larger
of the current read_TS() and TS(T).
To ensure serializability, the following two rules are used:
1. If transaction T issues a write_item(X) operation, and version i of X has the highest write_TS()
of all versions of X that is also less than or equal to TS(T), and read_TS() > TS(T), then abort
and roll back transaction T; otherwise, create a new version of X with read_TS() = write_TS() =
TS(T).
2. If transaction T issues a read_item(X) operation, find the version i of X that has the highest
write_TS() of all versions of Xthat is also less than or equal to TS(T); then return the value of to
transaction T, and set the value of read_TS() to the larger of TS(T) and the current read_TS().
As we can see in case 2, a read_item(X) is always successful, since it finds the appropriate
version to read based on the write_TS of the various existing versions of X. In case 1, however,
transaction T may be aborted and rolled back. This happens if T is attempting to write a version
of X that should have been read by another transaction T whose timestamp is read_TS();
however, T has already read version Xi, which was written by the transaction with timestamp
equal to write_TS(). If this conflict occurs, T is rolled back; otherwise, a new version of X,
written by transaction T, is created. Notice that, if T is rolled back, cascading rollback may
occur. Hence, to ensure recoverability, a transaction T should not be allowed to commit until
after all the transactions that have written some version that T has read have committed.
10 | P a g e
1.6 Deadlock
In a multi-process system, deadlock is an unwanted situation that arises in a shared resource
environment, where a process indefinitely waits for a resource that is held by another process.
For example, assume a set of transactions {T0, T1, T2, ...,Tn}. T0 needs a resource X to complete
its task. Resource X is held by T1, and T1 is waiting for a resource Y, which is held by T2. T2 is
waiting for resource Z, which is held by T0. Thus, all the processes wait for each other to release
resources. In this situation, none of the processes can finish their task. This situation is known as
a deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the transactions
involved in the deadlock are either rolled back or restarted.
Transaction is unit of work done. So a database management system will have number of
transactions. There may be situations when two or more transactions are put into wait state
simultaneously .In this position each would be waiting for the other transaction to get released.
Suppose we have two transactions one and two both executing simultaneously. In transaction
numbered one we update student table and then update course table. We have transaction two in
which we update course table and then update student table. We know that when a table is
updated it is locked and prevented from access from other transactions from updating.
So in transaction one student table is updated it is locked and in transaction two course table is
updated and it is locked. We have given already that both transactions gets executed
11 | P a g e
simultaneously. So both student table and course table gets locked so each one waits for the other
to get released. This is the concept of deadlock in DBMS.
A deadlock is a condition wherein two or more tasks are waiting for each other in order to be
finished but none of the task is willing to give up the resources that other task needs. In this
situation no task ever gets finished and is in waiting state forever.
Coffman conditions
Coffman stated four conditions for a deadlock occurrence. A deadlock may occur if all the
following conditions holds true.
Mutual exclusion condition: There must be at least one resource that cannot be used by
more than one process at a time.
Hold and wait condition: A process that is holding a resource can request for additional
resources that are being held by other processes in the system.
No preemption condition: A resource cannot be forcibly taken from a process. Only the
process can release a resource that is being held by it.
Circular wait condition: A condition where one process is waiting for a resource that is
being held by second process and second process is waiting for third process .so on and
the last process is waiting for the first process. Thus making a circular chain of waiting.
The question is why? Why instead of dealing with a deadlock they ignore it and why this is
being called as Ostrich algorithm?
Well! Let me answer the second question first, This is known as Ostrich algorithm because in
this approach we ignore the deadlock and pretends that it would never occur, just like Ostrich
behavior to stick ones head in the sand and pretend there is no problem.
Lets discuss why we ignore it: When it is believed that deadlocks are very rare and cost of
deadlock handling is higher, in that case ignoring is better solution than handling it. For example:
Lets take the operating system example If the time requires handling the deadlock is higher
than the time requires rebooting the windows then rebooting would be a preferred choice
considering that deadlocks are very rare in windows.
Removing mutual exclusion: All resources must be sharable that means at a time more
than one processes can get a hold of the resources. That approach is practically
impossible.
Removing hold and wait condition: This can be removed if the process acquires all the
resources that are needed before starting out. Another way to remove this to enforce a
rule of requesting resource when there are none in held by the process.
Preemption of resources: Preemption of resources from a process can result in rollback
and thus this needs to be avoided in order to maintain the consistency and stability of the
system.
13 | P a g e
Avoid circular wait condition: This can be avoided if the resources are maintained in a
hierarchy and process can hold the resources in increasing order of precedence. This
avoid circular wait. Another way of doing this to force one resource per process rule A
process can request for a resource once it releases the resource currently being held by it.
This avoids the circular wait.
Wait/Die
Wound/Wait
Here is the table representation of resource allocation for each algorithm. Both of these
algorithms take process age into consideration while determining the best possible way of
resource allocation for deadlock avoidance.
Wait/Die
Wound/Wait
Younger
process waits
14 | P a g e
If TS(Ti) > TS(Tj), then Ti is forced to wait until the resource is available.
This scheme, allows the younger transaction to wait; but when an older transaction requests an
item held by a younger one, the older transaction forces the younger one to abort and release the
item.
In both the cases, the transaction that enters the system at a later stage is aborted.
Aborting a transaction is not always a practical approach. Instead, deadlock avoidance
mechanisms can be used to detect any deadlock situation in advance. Methods like "wait-for
graph" are available but they are suitable for only those systems where transactions are
lightweight having fewer instances of resource. In a bulky system, deadlock prevention
techniques may work well.
Wait-for Graph
This is a simple method available to track if any deadlock situation may arise. For each
transaction entering into the system, a node is created. When a transaction Ti requests for a lock
on an item, say X, which is held by some other transaction Tj, a directed edge is created from
Ti to Tj. If Tj releases item X, the edge between them is dropped and Ti locks the data item.
The system maintains this wait-for graph for every transaction waiting for some data items held
by others. The system keeps checking if there's any cycle in the graph.
15 | P a g e
Chapter : 2
Object Oriented Database Development
2.1 Introduction
In this chapter, we will describe how such conceptual objectoriented models can be transformed
into logical schemas that can be directly implemented using an object database management
system (ODBMS). As you will learn later, although relational databases are effective for
traditional business applications, they have severe limitations (in the amount of programming
required and DBMS performance) when it comes to storing and manipulating complex data and
relationships. In this chapter, we will show how to implement applications within an objectoriented database environment.
An object database (also object-oriented database management system, OODBMS) is
a database management system in which information is represented in the form of objects as
used in object-oriented programming. Object databases are different from relational
databases which are table-oriented. Object-relational databases are a hybrid of both approaches.
Object databases have been considered since the early 1980s.
An object-oriented database management system (OODBMS), sometimes shortened
to ODBMS for object database management system), is a database management system (DBMS)
that supports the modelling and creation of data as objects. This includes some kind of support
for classes of objects and the inheritance of class properties and methods by subclasses and their
objects. There is currently no widely agreed-upon standard for what constitutes an OODBMS,
and OODBMS products are considered to be still in their infancy. In the meantime, the objectrelational database management system (ORDBMS), the idea that object-oriented database
concepts can be superimposed on relational databases, is more commonly encountered in
available products. An object-oriented database interface standard is being developed by an
industry group, the Object Data Management Group (ODMG). The Object Management Group
(OMG) has already standardized an object-oriented data brokering interface between systems in
a network.
16 | P a g e
Defining Structures
Structure = user-defined type with components struct keyword
Example:
struct Address
{
String street_address
String city;
String state;
String zip;
};
18 | P a g e
Defining Operations
Return type
Name
Parenthesis following the name
Arguments within the parentheses
Defining Relationships
19 | P a g e
This creates a new instance of Course. The object tag name, MBA699, can be used to reference
this object. We have not specified the attribute values for this object at this point.
Suppose you want to create a new student object and initialize some of its attributes.
Cheryl student (name: Cheryl Davis, dateOfBirth: 4/5/77);
This creates a new student object with a tag name of Cheryl and initializes the values of two
attributes. You can also specify the values for the attributes within a structure, as in the following
example:
Jack student (
name: Jack Warner, dateOfBirth: 2/12/74,
address: {street_address 310 College Rd, city Dayton, state Ohio, zip 45468},
phone: {area_code 937, personal_number 2282252}
);
For a multivalued attribute, you can specify a set of values.
For example, you can specify the skills for an employee called Dan Bellon as follows:
Dan employee (
emp_id: 3678, name: Dan Bellon,
skills: {Database design, OO Modeling }
);
Establishing links between objects for a given relationship is also easy. Suppose you want to
store the fact that Cheryl took three courses in fall 1999. You can write:
Cheryl student (
takes: {OOAD99F, Telecom99F, Java99F }
);
where OOAD99F, Telecom99F, and Java99F are tag names for three course-offering objects.
This definition creates three links for the takes relationship, from the object tagged Cheryl to
each of the course offering objects.
Consider another example. To assign Dan to the TQM project, we write:
assignment (start_date: 2/15/2001, allocated_to: Dan, for TQM);
Notice that we have not specified a tag name for the assignment object. Such objects will be
identified by the system-generated object identifiers. The assignment object has a link to an
employee object (Dan) and another link to a project object (TQM).
20 | P a g e
from students s
where s.name = John Marsh
The query returns an integer value, assuming that there is only one student with that name. In
addition to literal values, a query can also return objects with identity.
For example, the query
select s
from students s
where s.gpa = 3.0
returns a collection (bag) of student objects for which the gpa is greater than or equal to 3.0.
Notice that we have used the gpa operation in the where clause. If we want to formulate the
same query, but only for those students who do not reside in Dayton, we can use the not operator
as in SQL: select s
from students s
where s.gpa = 3.0
and not (s.address.city = Dayton)
Instead of using not, we could have specified the new condition as follows:
s.address.city ! = Dayton
where ! is the inequality operator.
Now suppose that we want to find the ages of all students whose gpa is less than 3.0. This query
is select s.age
from students s
where s.gpa < 3.0
22 | P a g e
References
1.
2.
3.
4.
5.
6.
7.
http://mafisamin.web.ugm.ac.id/materikuliah/sistembasisdata/chap15r.pdf
database system concept by Silberschatz, Korth, Sudarshan S. 2007
codex.cs.yale.edu/avi/db-book/slide-dir/ch16.ppt
https://www.tutorialspoint.com/dbms/dbms_deadlock.htm
wikipedia
Fundamentals of Database Systems by Elmasri Navathe
OBrien, J. A., & Marakas, G. M. (2009). Management Information Systems (9th ed.).
New York, NY: McGraw-Hill/Irwin
8. http://wps.prenhall.com/wps/media/objects/3310/3390076/hoffer_ch15.pdf
23 | P a g e