You are on page 1of 96

What is Database?

 Database is a collection of interrelated data which helps in the efficient


retrieval, insertion, and deletion of data from the database and organizes
the data in the form of tables, views, schemas, reports, etc. For Example,
a university database organizes the data about students, faculty, admin
staff, etc. which helps in the efficient retrieval, insertion, and deletion of
data from it.
What are the data languages?

There are four types of Data Languages

1.Data Definition Language (DDL)

2.Data Manipulation Language(DML)

3.Data Control Language(DCL)

4.Transactional Control Language(TCL)


What is database management system?

The software which is used to manage databases is called Database


Management System (DBMS). For Example, MySQL, Oracle, etc. are
popular commercial DBMS used in different applications. DBMS
allows users the following tasks:
• Data Definition: It helps in the creation, modification, and removal of
definitions that define the organization of data in the database.
• Data Updation: It helps in the insertion, modification, and deletion of
the actual data in the database.
• Data Retrieval: It helps in the retrieval of data from the database
which can be used by applications for various purposes.

• User Administration: It helps in registering and monitoring users,
enforcing data security, monitoring performance, maintaining data
integrity, dealing with concurrency control, and recovering
information corrupted by unexpected failure.
Paradigm Shift from File System to DBMS

File System manages data using files on a hard disk. Users are allowed
to create, delete, and update the files according to their requirements.
Let us consider the example of file-based University Management
System.
Data of students is available to their respective Departments, Academics
Section, Result Section, Accounts Section, Hostel Office, etc. Some of
the data is common for all sections like Roll No, Name, Father Name,
Address, and Phone number of students but some data is available to a
particular section only like Hostel allotment number which is a part of
the hostel office.

Let us discuss the issues with this system:
• Redundancy of data: Data is said to be redundant if the same data is
copied at many places. If a student wants to change their Phone
number, he or has to get it updated in various sections. Similarly, old
records must be deleted from all sections representing that student.
• Inconsistency of Data: Data is said to be inconsistent if multiple
copies of the same data do not match each other. If the Phone number
is different in Accounts Section and Academics Section, it will be
inconsistent. Inconsistency may be because of typing errors or not
updating all copies of the same data.

• Difficult Data Access: A user should know the exact location of the
file to access data, so the process is very cumbersome and tedious. If
the user wants to search the student hostel allotment number of a
student from 10000 unsorted students’ records, how difficult it can be.
• Unauthorized Access: File Systems may lead to unauthorized access
to data. If a student gets access to a file having his marks, he can
change it in an unauthorized way.

• No Concurrent Access: The access of the same data by multiple users
at the same time is known as concurrency. The file system does not
allow concurrency as data can be accessed by only one user at a time.
• No Backup and Recovery: The file system does not incorporate any
backup and recovery of data if a file is lost or corrupted.
Advantages of DBMS over File system

File System: A File Management system is a DBMS that allows access


to single files or tables at a time. In a File System, data is directly stored
in set of files. It contains flat files that have no relation to other files
(when only one table is stored in single file, then this file is known as
flat file).
DBMS: A Database Management System (DBMS) is a application
software that allows users to efficiently define, create, maintain and
share databases. Defining a database involves specifying the data types,
structures and constraints of the data to be stored in the database.

Creating a database involves storing the data on some storage medium
that is controlled by DBMS. Maintaining a database involves updating
the database whenever required to evolve and reflect changes in the
miniworld and also generating reports for each change. Sharing a
database involves allowing multiple users to access the database. DBMS
also serves as an interface between the database and end users or
application programs. It provides control access to the data and ensures
that data is consistent and correct by defining rules on them.
An application program accesses the database by sending queries or
requests for data to the DBMS. A query causes some data to be retrieved
from database.
Advantages of DBMS over File system:

Data redundancy and inconsistency: Redundancy is the concept of


repetition of data i.e. each data may have more than a single copy. The
file system cannot control the redundancy of data as each user defines
and maintains the needed files for a specific application to run. There
may be a possibility that two users are maintaining the data of the same
file for different applications.
Hence changes made by one user do not reflect in files used by second
users, which leads to inconsistency of data. Whereas DBMS controls
redundancy by maintaining a single repository of data that is defined
once and is accessed by many users. As there is no or less redundancy,
data remains consistent.

• Data sharing: The file system does not allow sharing of data or
sharing is too complex. Whereas in DBMS, data can be shared easily
due to a centralized system.
• Data concurrency: Concurrent access to data means more than one
user is accessing the same data at the same time. Anomalies occur
when changes made by one user get lost because of changes made by
another user. The file system does not provide any procedure to stop
anomalies. Whereas DBMS provides a locking system to stop
anomalies to occur.

• Data searching: For every search operation performed on the file
system, a different application program has to be written. While
DBMS provides inbuilt searching operations. The user only has to
write a small query to retrieve data from the database.
• Data integrity: There may be cases when some constraints need to be
applied to the data before inserting it into the database. The file system
does not provide any procedure to check these constraints
automatically. Whereas DBMS maintains data integrity by enforcing
user-defined constraints on data by itself.

• System crashing: In some cases, systems might have crashed due to
various reasons. It is a bane in the case of file systems because once
the system crashes, there will be no recovery of the data that’s been
lost. A DBMS will have the recovery manager which retrieves the data
making it another advantage over file systems.
• Data security: A file system provides a password mechanism to
protect the database but how long can the password be protected? No
one can guarantee that. This doesn’t happen in the case of DBMS.
DBMS has specialized features that help provide shielding to its data.

• Backup: It creates a backup subsystem to restore the data if required.
• Interfaces: It provides different multiple user interfaces like graphical
user interface and application program interface.
• Easy Maintenance: It is easily maintainable due to its centralized
nature.
• DBMS is continuously evolving from time to time. It is a power tool
for data storage and protection. In the coming years, we will get to
witness an AI-based DBMS to retrieve databases of ancient eras.
ACID Properties in DBMS

A transaction is a single logical unit of work that accesses and possibly


modifies the contents of a database. Transactions access data using read
and write operations.
In order to maintain consistency in a database, before and after the
transaction, certain properties are followed. These are
called ACID properties.

• Atomicity:
• By this, we mean that either the entire transaction takes place at once
or doesn’t happen at all. There is no midway i.e. transactions do not
occur partially. Each transaction is considered as one unit and either
runs to completion or is not executed at all. It involves the following
two operations.
—Abort: If a transaction aborts, changes made to the database are not
visible.
—Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.

Consider the following transaction T consisting of T1 and T2: Transfer
of 100 from account X to account Y.

If the transaction fails after completion of T1 but before completion
of T2.( say, after write(X) but before write(Y)), then the amount has
been deducted from X but not added to Y. This results in an inconsistent
database state. Therefore, the transaction must be executed in its entirety
in order to ensure the correctness of the database state.

• Consistency:
• This means that integrity constraints must be maintained so that the
database is consistent before and after the transaction. It refers to the
correctness of a database. Referring to the example above,
The total amount before and after the transaction must be maintained.
Total before T occurs = 500 + 200 = 700.
Total after T occurs = 400 + 300 = 700.
Therefore, the database is consistent. Inconsistency occurs in
case T1 completes but T2 fails. As a result, T is incomplete.

• Isolation:
• This property ensures that multiple transactions can occur concurrently
without leading to the inconsistency of the database state. Transactions
occur independently without interference. Changes occurring in a
particular transaction will not be visible to any other transaction until
that particular change in that transaction is written to memory or has
been committed. This property ensures that the execution of transactions
concurrently will result in a state that is equivalent to a state achieved
these were executed serially in some order.
Let X= 500, Y = 500.
Consider two transactions T and T”.
Suppose T has been executed till Read (Y) and then T’’ starts. As a result, interleaving of operations
takes place due to which T’’ reads the correct value of X but the incorrect value of Y and sum computed
by
T’’: (X+Y = 50, 000+500=50, 500)
is thus not consistent with the sum at end of the transaction:
T: (X+Y = 50, 000 + 450 = 50, 450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take place in
isolation and changes should be visible only after they have been made to the main memory.

• Durability:
• This property ensures that once the transaction has completed
execution, the updates and modifications to the database are stored in
and written to disk and they persist even if a system failure occurs.
These updates now become permanent and are stored in non-volatile
memory. The effects of the transaction, thus, are never lost.
Some important points:

The ACID properties, in totality, provide a mechanism to ensure the
correctness and consistency of a database in a way such that each
transaction is a group of operations that acts as a single unit, produces
consistent results, acts in isolation from other operations, and updates
that it makes are durably stored.
Advantages of Database Management System

Database Management System (DBMS) is basically a collection of


interrelated data and a set of software tools/programs which access,
process, and manipulate data. It allows access, retrieval, and use of that
data by considering appropriate security measures. The Database
Management system (DBMS) is really useful for better data integration
and security.
• Advantages of Database Management System (DBMS):
• Some of them are given as follows below.

1.Better Data Transferring: Database management creates a place where
users have an advantage of more and better-managed data. Thus making it
possible for end-users to have a quick look and to respond fast to any
changes made in their environment.
2.Better Data Security: The more accessible and usable the database, the
more it is prone to security issues. As the number of users increases, the data
transferring or data sharing rate also increases thus increasing the risk of data
security. It is widely used in the corporate world where companies invest
money, time, and effort in large amounts to ensure data is secure and is used
properly. A Database Management System (DBMS) provides a better
platform for data privacy and security policies thus, helping companies to
improve Data Security.

1.Better data integration: Due to the Database Management System we have an
access to well managed and synchronized form of data thus it makes data handling
very easy and gives an integrated view of how a particular organization is working
and also helps to keep a track of how one segment of the company affects another
segment.
2.Minimized Data Inconsistency: Data inconsistency occurs between files when
different versions of the same data appear in different places. For Example, data
inconsistency occurs when a student’s name is saved as “John Wayne” on a main
computer of the school but on the teacher registered system same student name is
“William J. Wayne”, or when the price of a product is $86.95 in the local system of
the company and its National sales office system shows the same product price as
$84.95. So if a database is properly designed then Data inconsistency can be
greatly reduced hence minimizing data inconsistency.

1.Faster data Access: The Database management system (DBMS) helps to produce
quick answers to database queries thus making data access faster and more
accurate. For example, to read or update the data. For example, end-users, when
dealing with large amounts of sale data, will have enhanced access to the data,
enabling a faster sales cycle. Some queries may be like:
1. What is the increase in sales in the last three months?
2. What is the bonus given to each of the salespeople in the last five months?
3. How many customers have a credit score of 850 or more?
2.Better decision making: Due to DBMS now we have Better managed data and
Improved data access because of which we can generate better quality information
hence on this basis better decisions can be made. Better Data quality improves
accuracy, validity, and time it takes to read data. DBMS does not guarantee data
quality, it provides a framework to make it easy to improve data quality.

3. Increased end-user productivity: The data which is available with
the help of a combination of tools that transform data into useful
information, helps end-users to make quick, informative, and better
decisions that can make difference between success and failure in the
global economy.
4. Simple: Database management system (DBMS) gives a simple and
clear logical view of data. Many operations like insertion, deletion, or
creation of files or data are easy to implement.

5. Data abstraction: The major purpose of a database system is to
provide users with an abstract view of the data. Since many complex
algorithms are used by the developers to increase the efficiency of
databases that are being hidden by the users through various data
abstraction levels to allow users to easily interact with the system.
6. Reduction in data Redundancy: When working with a structured
database, DBMS provides the feature to prevent the input of duplicate
items in the database. for e.g. – If there are two same students in
different rows, then one of the duplicate data will be deleted.
Chapter-1

Transaction Management and


Concurrency Control
What is a Transaction?
• Any action that reads from and/or writes to a
database may consist of
• Simple SELECT statement to generate a list of table contents
• A series of related UPDATE statements to change the values
of attributes in various tables
• A series of INSERT statements to add rows to one or more
tables
• A combination of SELECT, UPDATE, and INSERT statements
What Is a Transaction?

• A transaction is a logical unit of work


that must be either entirely completed or aborted;
no intermediate states are acceptable.
• Most real-world database transactions are formed by two or more database
requests.
• A database request
is a single SQL statement in an application program or transaction.
What Is• Aaconsistent
Transaction?
state
is one which all data integrity constrains are satisfied.
• A transaction that changes the contents of the database
must alter the database
from one consistent state to another.
• To ensure consistency of the database,
every transaction must begin with the database in a known
consistent state.

consistent consistent
transaction
state state

database requests
What is a Transaction?
What Is a Transaction?

• Evaluating Transaction Results


• Examining the current balance for an account:
SELECT CUST_NUMBER, CUST_BALANCE
FROM CUSTOMER
WHERE CUST_NUMBER = 10016;

• represents a transaction because we accessed the database.


• The database remains in a consistent state after the transaction,
because it did not alter the database.
What Is a Transaction?
• Evaluating Transaction Results
• An accountant wishes to register the credit sale of 100
units of product X to customer Y in the amount of $500.00:
• Reducing product X’s Quantity on hand by 100.
• Adding $500.00 to customer Y’s accounts receivable.

A real-world UPDATE PRODUCT


transaction SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
UPDATE CUSTOMER
SET CUST_BALANCE = CUST_BALANCE + 500
WHERE CUST_NUMBER = ‘Y’;

• If the above two requests are not completely executed, the


transaction yields an inconsistent database.
• The DBMS must be able to recover the database
to a previous consistent state.
Evaluating Transaction Results

Figure 9.2
What Is a Transaction?
• Transaction Properties
• Atomicity
• All transaction operations must be completed
• Incomplete transactions aborted
• Consistency
permanence of the database’s consistent state.
• Isolation
means that the data used during the execution of a transaction cannot be
used by a second transaction until the first one is completed.
What Is a Transaction?
• Transaction Properties
• Durability
once transaction changes are done (committed), they cannot be
undone.
• Serializability
• concurrent transactions are treated as though they were executed in
serial order (one after another).
• Ensures that the concurrent execution of several transactions yields
consistent results
• important in multi-user and distributed databases.
What Is a Transaction?
• Transaction Management with SQL
• Transaction support is provided
• COMMIT
• ROLLBACK
• When a transaction sequence is initiated,
it must continue through all succeeding SQL statements until one
of the following four events occurs:
• A COMMIT statement is reached.
The COMMIT statement automatically ends the SQL transaction.
• A ROLLBACK statement is reached.
• The end of a program is successfully reached ( = COMMIT).
• The program is abnormally terminated ( = ROLLBACK).
What Is a Transaction?
• Transaction Management with SQL
• Example:
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 2
WHERE PROD_CODE = ‘1558-QW1’;
UPDATE CUSTOMER
SET CUST_BALANCE = CUST_BALANCE + 87.98
WHERE CUST_NUMBER = ‘10011’;
COMMIT;

• If UPDATE is the application’s last action and


the application terminates normally
 COMMIT is not necessary
• A transaction begins implicitly when the first SQL statement is
encountered.
Some SQL (not follow ANSI) use: BEGIN TRANSACTION;
What Is a Transaction?
• The Transaction Log
• A transaction log
keeps track of all transactions that update the database.
• The information stored in the log is used by the DBMS
for a recovery requirement triggered by a ROLLBACK statement or a
system failure.
• The transaction log
stores before-and-after data about the database and any of the tables,
rows, and attribute values that participated in the transaction.
• The transaction log is itself a database, and it is managed by the DBMS
like any other database.
The Transaction Log
Before After
Concurrency Control

• Concurrency control
coordinates simultaneous execution of transactions in a
multiprocessing database.
• The objective of concurrency control is to
ensure the serializability of transactions in a multi-user database
environment.
Concurrency Control

• Simultaneous execution of transactions over a shared database can


create several data integrity and consistency problems:
• Lost Updates.
• Uncommitted Data.
• Inconsistent retrievals.
Concurrency Control
• Lost Updates
• Two concurrent transactions update PROD_QOH:

TRANSACTION COMPUTATION
T1: Purchase 100 units PROD_QOH = PROD_QOH + 100
T2: Sell 30 units PROD_QOH = PROD_QOH - 30

• See Table 10.2 for the serial execution under normal


circumstances.
• See Table 10.3 for the lost update problems resulting from
the execution of the second transaction before the first
transaction is committed.
TABLE 10.2

TABLE 10.3
Concurrency Control
• Uncommitted Data
• Data are not committed when
two transactions T1 and T2 are executed concurrently and the
first transaction is rolled back after
the second transaction has already accessed the uncommitted
data –
thus violating the isolation property of the transaction.

TRANSACTION COMPUTATION
T1: Purchase 100 units PROD_QOH = PROD_QOH + 100 (Rolled back)
T2: Sell 30 units PROD_QOH = PROD_QOH - 30
TABLE 10.4

TABLE 10.5
Concurrency Control

• Inconsistent Retrievals
• Inconsistent retrievals occur when a transaction calculates some summary
(aggregate) functions over a set of data while other transactions are
updating the data.

• Example:
• T1 calculates the total quantity on hand of the products stored in the PRODUCT table.
• At the same time, T2 updates the quantity on hand (PROD_QOH) for two of the
PRODUCT table’s products.
Retrieval During Update
TABLE
10.6 T2
T1
Transaction Results:
Data Entry Correction
TABLE
10.7
Inconsistent
TABLE
Retrievals
10.8
Concurrency Control
• The Scheduler
• The scheduler establishes the order in which the operations
within concurrent transactions are executed.
• The scheduler interleaves the execution of database
operations to ensure serializability and isolation.
• To determine the appropriate order, the scheduler bases its
actions on concurrency control algorithms, such as locking or
time stamping methods.
• The scheduler also makes sure that the computer’s CPU is
used efficiently.
Read/Write Conflict Scenarios:
Conflicting Database Operations Matrix

TABLE
10.9

• T1 and T2 are executed concurrently over the same data.


Concurrency Control with
Locking Methods
• Concurrency can be controlled using locks.
• A lock guarantees exclusive use of a data item to a current
transaction.
• A transaction acquires a lock prior to data access; the lock is
released (unlocked) when the transaction is completed.
• All lock of information is managed by a lock manager.
Concurrency Control with
Locking Methods
• Lock Granularity
• Lock granularity indicates the level of lock use.
• Database level (See Figure 10.3)
• Table level (See Figure 10.4)
• Page level (See Figure 10.5)
• Row level (See Figure 10.6)
• Field level (attribute)
A Database-Level Locking Sequence
• T1 and T2 cannot access the same database concurrently,
even if they use different tables.

T1( Update Table A ) T2( Update Table B )

FIGURE 10.3
An Example Of A Table-Level Lock
•T1 and T2 cannot access the same table concurrently,
even if they use different rows.

T1( Update Row 5 ) T2( Update Row 30 )

FIGURE 10.4
An Example Of A Page-Level Lock
•T1 and T2 cannot access the same page concurrently,
even if they use different rows.
• the most frequently used multiuser DBMS locking methods.

FIGURE 10.5
An Example Of A Row-Level Lock
• Although it improves the availability of data,
its management requires high overhead cost.

row

FIGURE 10.6
Concurrency Control with
Locking Methods
• Lock types
• Binary Locks
• Shared/Exclusive Locks

• Binary Locks
• A binary lock has only two states:
locked (1) or unlocked (0).
• If an object is locked by a transaction,
no other transaction can use that object.
• If an object is unlocked, any transaction can lock the object for its
use.
• A transaction must unlock the object
after its termination.
Binary Lock
Concurrency Control with Locking Methods

• Shared/Exclusive Locks
(1)Exclusive Locks
• An exclusive lock exists when access is specially reserved for
the transaction that locked the object.
• The exclusive lock must be used
when the potential for conflict exists.

issued when a transaction wants to


update unlocked data item.
Concurrency Control with Locking Methods

(2)Shared Locks
• A shared lock exists
when concurrent transactions are granted READ access on the basis
of a common lock.
• A shared lock produces no conflict as long as the concurrent
transactions are read only.

issued when a transaction wants to


read data and no exclusive lock is held on that data item.
Concurrency Control with Locking Methods

• Shared/Exclusive Locks
 Although the possibility of shared locks renders data access more
efficient,
a shared/exclusive lock schema increases the lock manager’s
overhead.
• Three lock operations needed:
• READ_LOCK(check the type of lock)
• WRITE_LOCK(issue the lock)
• UNLOCK(release the lock)
Concurrency Control with Locking Methods
• Shared/Exclusive Locks

Current
Shared Exclusive
Unlock
Want to Lock Lock

Shared Shared
Read Wait
Lock Lock

Exclusive
Write Wait Wait
Lock
Concurrency Control with Locking Methods

• Although locks prevent serious data inconsistencies, Potential


Problems with Locks
• The resulting transaction schedule may not be serializable.
• The schedule may create deadlocks.
• Solutions
• Two-phase locking for the serializability problem.
• Deadlock detection and prevention techniques for the
deadlock problem.
Concurrency Control with Locking Methods

• Two-Phase Locking
• The two-phase locking protocol defines how transactions
acquire and relinquish locks.
It guarantees serializability,
but it does not prevent deadlocks.
• In a growing phase,
a transaction acquires all the required locks without
unlocking any data.
Once all locks have been acquired,
the transaction is in its locked point.
• In a shrinking phase,
a transaction releases all locks and cannot obtain any
new locks.
Concurrency Control with Locking Methods

• Rules for Two-Phase Locking Protocol


• Two transactions cannot have conflicting locks.
• No unlock operation can precede a lock operation in the
same transaction.
• No data are affected until all locks are obtained –
that is, until the transaction is in its locked point.
Two-Phase Locking Protocol
Concurrency Control with Locking Methods

• Deadlocks (Deadly Embrace)


• Occurs when two transactions wait for each other to
unlock data
• Deadlocks exist when two transactions T1 and T2 exist in
the following mode:
holds T1 requests

T1 = requests X and holds Y


T2 = requests Y and holds X Y X

• If T1 has not unlocked data item Y, T2 requests


cannot begin; and,
T2 holds
if T2 has not unlocked data item X, T1 cannot continue.
(See Table 9.11)
How A Deadlock Condition Is Created

Table 9.11
Four Conditions for Deadlock
 All four of these conditions must be present :
1. Mutual exclusion condition

each resource assigned to 1 process or is available
2. Hold and wait condition

process holding resources can request additional
3. No preemption condition

previously granted resources cannot forcibly taken away
4. Circular wait condition
• must be a circular chain of 2 or more processes
• each is waiting for resource held by next member of the
chain
Concurrency Control with Locking Methods

• Three Techniques to Control Deadlocks:


• Deadlock Prevention
A transaction requesting a new lock is aborted
if there is a possibility that a deadlock can occur.
( Attacking the four conditions )
• Deadlock Detection
The DBMS periodically tests the database for deadlocks.
If a deadlock is found, one of the transactions (“victim”) is aborted, and
the other transaction continues.
• Deadlock Avoidance
The transaction must obtain all the locks it needs before it can be
executed.
Avoid deadlocks by allocating resources carefully.
Detection with Multiple Resource of Each Type
 n processes
 m resource classes

Data structures needed by deadlock detection algorithm


Detection with Multiple Resource of Each Type

• An example for the deadlock detection algorithm


1. R1 !≦ A
2. R2 !≦ A
3. R3 ≦ A → P3 runs and releases → A=(2 2 2 0)
4. R2 ≦ A → P2 runs and releases → A=(4 2 2 1)
5. R1 ≦ A → P1 runs and releases → A=(4 2 3 1)
Deadlock Avoidance
• Based on the concept of safe states
• At point t , B is requesting plotter
• Grant- enter an unsafe region and eventually deadlock
• Deny- B suspended until A has requested and released plotter

Two process Resource Trajectories


Safe and Unsafe States
• Safe state
is not deadlock and
there is some scheduling order in which every processes can run to
completion even if all of them suddenly request their maximum
number of resources immediately.
• (a) is safe
because the system, by careful scheduling, can avoid deadlock.
• The system can guarantee that all processes will finish

(a) (b) (c) (d) (e)

Demonstration that the state in (a) is safe


Safe and Unsafe States
• Unsafe state
• The system cannot guarantee that all processes will finish
• is not deadlock state.
• The system can run for a while.
• Some process can even complete.
• It is possible that A might releases a resource before asking for any more,
allowing C to complete and avoiding deadlock altogether.

(a) (b) (c) (d)

Demonstration that the sate in (b) is not safe


Deadlock Prevention
• (1) Attacking the Mutual Exclusion Condition
• (2) Attacking the Hold and Wait Condition
• (3) Attacking the No Preemption Condition
• (4) Attacking the Circular Wait Condition
Deadlock Prevention
(1) Attacking the Mutual Exclusion Condition
• If no resources were ever assigned exclusively to a single process,
we would never have deadlocks.
• Some devices (such as printer) can be spooled
• only the printer daemon ( only one process) requests printer resource
• thus deadlock for printer eliminated
• Not all devices can be spooled
(2) Attacking the Hold and Wait Condition
• Require processes to request all resources before
starting
• a process never has to wait for what it needs

• Problems
1. may not know required resources at start of run
2. Resources will not be used optimally.

• Variation:
• process must give up all resources it currently holds
then request all at once
(3) Attacking the No Preemption Condition

• This is not a viable option


• Consider a process given the printer
• halfway through its job
• now forcibly take away printer
• !!??
(4) Attacking the Circular Wait Condition
• Normally ordered resources-
provide a global numbering of all the resources

• Resource allocation graph


• If i > j – A is denied
• If i < j – B is denied

(a) (b)
Concurrency Control with
Time Stamping Methods

• The time stamping approach


assigns a global unique time stamp to each transaction to
schedule concurrent transactions.
• The time stamp value produces an explicit order in which
transactions are submitted to the DBMS.
• Time stamps must have two properties:
• Uniqueness
assures that no equal time stamp values can exist.
• Monotonicity
assures that time stamp values always increase.
• The DBMS executes conflicting operations
in time stamp order to ensure the serializability.
Wait/Die and Wound/Wait Schemes
• Wait/die (Owning)
• Older transaction waits and the younger is rolled back and rescheduled

• Wound/wait (Older)
• Older transaction rolls back the younger transaction and reschedules it

Requesting Owning
Wait/Die Wound/Wait
Lock Lock
T1 preempts
T1(Older) T2(Younger) T1 Wait
T2
T2(Younger) T1(Older) T2 Dies T2 Waits
Wait/Die and Wound/Wait
Concurrency Control Schemes
Concurrency Control with Optimistic Methods

• Optimistic Methods
• It is based on the assumption
that the majority of the database operations do not conflict.
• A transaction is executed without restrictions
until it is committed.
Database• Recovery
Recovery Management
restores a database
from a given state, usually inconsistent,
to a previously consistent state.
• Recovery techniques are based on the atomic transaction
property:
All portions of the transaction must be applied and
completed to produce a consistent database.
If, for some reason, any transaction operation cannot be
completed, the transaction must be aborted, and any
changes to the database must be rolled back.
Database Recovery Management

• Levels of Backup
• Full backup of the database
It backs up or dumps the whole database.

• Differential backup of the database


Only the last modifications done to the database are copied.

• Backup of the transaction log only


It backs up all the transaction log operations that are not reflected in a previous backup
copy of the database.
Database Recovery Management
• Database Failures
• Software
Operating system, DBMS, application programs, viruses
• Hardware
Memory chip errors, disk crashes, bad disk sectors, disk full errors
• Programming Exemption
Application programs (a withdrawal of zero balance account)
end users ( [Ctrl]+[C] )
• Transaction
Deadlocks
• External
Fire, earthquake, flood
Database• Transaction
Recovery Management
Recovery Procedures:
• Deferred-write/Deferred-update
Transaction operations do not immediately update the database.
Instead, all changes are written to the transaction log.
The database is updated only after the transaction reaches its commit
point.

• Write-through/Immediate-update
The database is immediately updated by transaction operations during
the transaction’s execution, even before the transaction reaches its
commit point.
The transaction log is also updated.
If a transaction fails, the database uses the log information to roll back
the database.

You might also like