Computer Security
Computer Security
Or
Let Transaction T transfers $50 from Account A to Account B,
Read(A)
A:=A-50
Write(A)
Read(B)
B:= B+50
Write(B)
Operations of Transaction:
Following are the main operations of transaction:
Read(A) or Retrieve A : Read operation is used to read the value of A from the
database and stores it in a buffer in main memory.
Write(A) or Update A: Write operation is used to write the value back to the database
from the buffer.
1
Let's take an example to debit transaction from an account which consists of following
operations:
1. Read(A)
2. A:=A-50
3. Write(A)
Let's assume the value of A before starting of the transaction is 100 and the value of B is
0.
• The first operation reads A's value from database and stores it in a buffer.
• The second operation will decrease the value of A by 50. So buffer will contain
50.
• The third operation will write the buffer's value to the database. So A's final
value will be 50.
But it may be possible that because of the failure of hardware, software or power,
etc. that transaction may fail before finished all the operations in the set. For
example: If in the above transaction, the debit transaction fails after executing
operation 2 then A's value will remain 100 in the database which is not acceptable by
the bank.
To solve this problem, Oracle Database must allow:
Commit work commits the current transaction; that is, it makes the updates
performed by the transaction become permanent in the database. Or It is used
to save the work done permanently. After the transaction is committed, a new
transaction is automatically started.
Rollback work causes the current transaction to be rolled back; that is, it
undoes all the updates performed by the SQL statements in the transaction. Or It
is used to undo the work done. Thus, the database state is restored to what it was
before the first statement of the transaction was executed.
.
A transaction begins with the first executable SQL statement. A transaction ends
when it is committed or rolled back, either explicitly with
a COMMIT or ROLLBACK statement or implicitly when a DDL statement is issued.
A transaction ends when any of the following occurs:
A user issues a COMMIT or ROLLBACK statement without
a SAVEPOINT clause.
A user runs a DDL statement such as CREATE, DROP, RENAME, or ALTER.
A user disconnects from Oracle Database. The current transaction is
committed.
A user process terminates abnormally. The current transaction is rolled
back.
Committing means that a user has explicitly or implicitly requested that the
changes in the transaction be made permanent. An explicit request occurs when
the user issues a COMMIT statement. An implicit request occurs after normal
termination of an application or completion of a data definition language (DDL)
operation. Oracle Database marks the transaction complete.
Rolling back means undoing any changes to data that have been performed by
SQL statements within an uncommitted transaction. The transaction ends.
2
ACID Properties
A transaction is a very small unit of a program and it may contain several low-level
tasks. A transaction in a database system must
maintain Atomicity, Consistency, Isolation, and Durability − commonly known as ACID
properties − in order to ensure accuracy, completeness, and data integrity.
• Atomicity − This property states that a transaction must be treated as an atomic
unit, that is, either all of its operations are executed or none. There must be no
state in a database where a transaction is left partially completed. States should
be defined either before the execution of the transaction or after the
execution/abortion/failure of the transaction.
• Consistency − The database must remain in a consistent state after any
transaction. No transaction should have any adverse effect on the data residing
in the database. If the database was in a consistent state before the execution of
a transaction, it must remain consistent after the execution of the transaction as
well.
• Durability − The database should be durable enough to hold all its latest
updates even if the system fails or restarts. If a transaction updates a chunk of
data in a database and commits, then the database will hold the modified data. If
a transaction commits but the system fails before the data could be written on
to the disk, then that data will be updated once the system springs back into
action.
• Isolation − In a database system where more than one transaction are being
executed simultaneously and in parallel, the property of isolation states that all
the transactions will be carried out and executed as if it is the only transaction
in the system. No transaction will affect the existence of any other transaction.
What is concurrency?
Concurrency in terms of database means allowing multiple users to access the data
within a database at the same time.
Concurrent execution of transaction is essential for good DBMS performance.
(2)It reduces waiting time in a serial processing. A short transaction may have to
wait for a long transaction to complete, in the concurrent execution it reduces average
response time and the average time for a transaction to be completed.
If the concurrent access is not managed by DBMS, so that simultaneous operations may
interfere with one another; problems can occur when various transactions are working
together interleaved their operations and resulting an inconsistent database.
Concurrency control
• Concurrency control is the process of managing simultaneous execution of transactions
(such as queries, updates, inserts, deletes and so on) in a multiprocessing database
3
system without having them interfere with one another.
• This property of DBMS allows many transactions to access the same database at the
same time without interfering with each other.
• The primary goal of concurrency is to ensure the atomicity of the execution of
transactions in a multi-user database environment.
o When two transactions that access the same database items contain their
operations in a way that makes the value of some database item incorrect, then
the lost update problem occurs.
o If two transactions T1 and T2 read a record and then update it, then the effect of
updating of the first record will be overwritten by the second update.
Example:
Here,
o The dirty read occurs in the case when one transaction updates an item of the
database, and then the transaction fails for some reason. The updated database
item is accessed by another transaction before it is changed back to the original
value.
o A transaction T1 updates a record which is read by T2. If T1 aborts then T2 now
has values which have never formed part of the stable database.
Example:
Example:
5
ACC1 ACC2 ACC3
BALANCE=40 BALANCE=50 BALANCE=30
Read ACC3 t8
Sum=110 not 120
In this type of protocol, any transaction cannot read or write data until it acquires an
appropriate lock on it. There are two types of lock:
o It is also known as a Read-only lock. In a shared lock, the data item can only read
by the transaction. Whenever transaction wants to read the data it should
acquired shared lock on that data.
o More than one transaction can acquired shared lock at a time on same data but if
any other transaction holds X lock on that data then shared lock cannot be
acquired on that data.
o It can be shared between the transactions because when the transaction holds a
lock, then it can't update the data on the data item.
o S-lock is requested using lock-S instruction.
6
2. Exclusive lock: (X- LOCK)
o In the exclusive lock, the data item can be both reads as well as written by the
transaction.
o It is also known as write lock whenever transaction want to write (update, insert,
delete) on data on the database, it should acquired X lock on that data.
o This lock is exclusive, and in this lock, multiple transactions do not modify the
same data simultaneously.
o If any other transaction holds either shared or exclusive lock on that data
exclusive lock will not acquired by this transaction. It will goes into wait state.
o X-lock is requested using lock-X instruction.
S X --
S X
X X X
-- --
7
Three Concurrency Problems Revisited:
Read (A)
Request S lock
--- t3
(acquire lock - S)
Update(A)
(Request X lock)
Wait… t4 ---
Wait….
Update(A)
(Request X lock)
--- t5 Wait…
Wait….
8
2. Uncommitted Dependency Problem:
Read A
Request S lock
Wait… t3 ---
Wait…
Rollback
--- t4
(Release lock on A)
Resume: Read A
Request S lock
(acquire lock -S) t5 ---
9
3. Inconsistent Analysis Problem (Unrepeatable read or
Inconsistent Retrievals Problem)
Read ACC2
Request S lock
t2 --
(acquire lock –S on ACC2)
Read ACC3
Request S lock
-- t3
(acquire lock –S on ACC3)
Update ACC3
Request X lock
-- t4
(acquire lock -X)
Read ACC1
Request S lock
t5
(acquire lock –S on ACC1)
Update ACC1
Request X lock
t6 Wait …
Wait…
Read ACC3
Request S lock on ACC3
Wait.. t7
Wait..
Deadlock in DBMS
In a database, a deadlock is an unwanted situation in which two or more
transactions are waiting indefinitely for one another to give up locks.
Deadlock is said to be one of the most feared complications in DBMS as it brings
the whole system to a Halt.
Example – let us understand the concept of Deadlock with an example:
Suppose, Transaction T1 holds a lock on some rows in the Students table
and needs to update some rows in the Grades table. Simultaneously,
Transaction T2 holds locks on those very rows (Which T1 needs to update) in the
Grades table but needs to update the rows in the Student table held by
Transaction T1.
Now, the main problem arises. Transaction T1 will wait for transaction T2 to
release (give up) lock, and similarly transaction T2 will wait for transaction T1
to release (give up) lock. As a consequence, All activity comes to a halt and
remains at a standstill forever unless the DBMS detects the deadlock and
aborts one of the transactions.
11
Deadlock Detection –
When a transaction waits indefinitely to obtain a lock, The database management
system should detect whether the transaction is involved in a deadlock or not.
Wait-for-graph is one of the methods for detecting the deadlock situation. This
method is suitable for smaller database. In this method a graph is drawn based
on the transaction and their lock on the resource. If the graph created has a
closed loop or a cycle, then there is a deadlock.
For the above mentioned scenario the Wait-For graph is drawn below
Deadlock Avoidance –
When a database is stuck in a deadlock, It is always better to avoid the deadlock rather
than restarting or aborting the database.
Deadlock prevention –
Suppose, there are two transactions T1 and T2 and Let timestamp of any
transaction T be TS (T).
Now, If there is a lock on T2 by some other transaction and T1 is requesting for
resources held by T2, then
13
Database Recovery
Failure: A failure is a situation that makes the database in inconsistent state. Failure
may be local or global.
Types of Failures :
Two types: Local Failure and Global Failure
1. Local Failure: A Local Failure affects only the transaction in which failure has
actually occurred Like:
Transaction failure :
Logical errors: transaction cannot complete due to some internal error
condition
System errors: the database system must terminate an active
transaction due to an error condition (e.g., deadlock)
2. Global Failure: A global failure affects all the transactions in progress at the
time of the failure and hence a significant system wide implications. The global
failure can be divided into two categories.
System failure which affect all transactions currently in progress but do
not damage physically the database. It is known as soft crash. A power
failure causes the system to crash.
Media failure ( Disk failure) Which do cause the damage the database or
to some portion of it and affect at least the transactions currently using
the portion. A Media failure is called hard crash. A head crash or similar
disk failure destroys all or part of disk storage
Recovery Concepts:
[1] System Recovery :-
The key point of system failure is that the contents of main memory are lost so
the state of transaction which was in progress at the time of failure is therefore
no longer known. Such transaction can never be successfully completed. So must
be undone – rollback when the system restarts.
It might be necessary to redo the certain transactions at restart time that did
successfully complete prior to the failure but did not manage their updates from
buffer to physical database.
So How does the system know at restart time which transactions to undo and
whch to redo?
14
The answer is: - the system automatically takes a checkpoint which shows the
transaction entries written in log.
Taking a checkpoint involves
Physically writing contents of the database buffers out to physical database.
Physically writing special checkpoint record to physical log.
The checkpoint record gives a list of all transactions that were in progress at the
time checkpoint was taken.
Following is the example with five transactions categories recover with system
recovery.
15
3. If a BEGIN TRANSACTION log entry is found for transaction T, add T to the
UNDO list.
4. If a COMMIT log entry is found for transaction T, move T from the UNDO
list to the REDO list.
5. When the end of the log is reached the UNDO and REDO lists identify
respectively, transaction of types T3 and T5 and transaction of types T2
and T4
Now the system works backward through the log. Undoing in the transaction in the
UNDO list then it works forward again Redoing in the transaction in the REDO list.
A media failure is a failure such as disk had crash or disk controller failure in
which some portion of the database has been physically destroyed or damaged.
Recovery from such a failure involves reloading the database from a backup copy
or dump and then using the log both active and achieve portion. In general to
redo all the Transactions that completed since the backup copy was taken. There
is no need to undo Transactions that were still in progress at the time of failure
because all updates of such transactions have been actually lost.
Two phase commit is important whenever a given transaction can interact with several
independent “resource managers” each managing its own set of recoverable resources
and maintaining its own recovery log.
for ex :- If a transaction running on an IBM mainframe that updates two database(oracle
and db2) then if the transaction completes successfully then all of its updates to both
Oracle data and db2 data must be committed but if it fails then all of its updates must
be rollback. In other words it is not possible for oracle to commit the update and db2
rolled back or vice versa. Otherwise the transaction would no longer be atomic.
If the system crashes both two situations (one is committing and other is roll back) then
it is very critical and database will be inconsistent. Therefore the transaction issue a
single system wide COMMIT (or ROLLBACK). That global COMMIT or ROLLBACK is
handled by system component called coordinator, whose task is to guarantee that both
resource managers (oracle and db2 ) commit or rollback the updates they are
16
Responsible for in unison and it also provide guarantee even if system fails in
the middle of the process.
It is the Two Phase Commit protocol that enables the coordinator to provide
such a guarantee. The working of protocol is: Assume a transaction has
completed its database processing successfully so that the system wide
instruction it issue is COMMIT, not ROLLBACK. On receiving that COMMIT
request the coordinator goes through the following two-phase process:
1. It instructs all the resource manages to get ready to “go either way “ on
the transaction. That means each participant in the process must force
all log entries for local resources used by the transaction out to its
physical log. Assuming the force writ is successful; the resource
manager replies “OK” to the coordinator otherwise it replies”NOT OK”.
2. When the coordinator has received replies from all participants, it
forces an entry to its own physical log, recording the decision
regarding the transaction. If all replies were “OK” that decision is
“COMMIT”, if any reply was “NOT OK” the decision is ROLLBACK. In
either way the coordinator informs each participants must commit or
rollback the transaction locally as instructed by coordinator.
17