Anjali DB 3
Anjali DB 3
DBMS
ASSIGNMENT - 3
Schedule 1
T1: write(A)
T1: read(F)
T2: write(B)
T3: increment(C)
T1: increment(E)
T4: increment(E)
T2: read(F)
T4: read(D)
T3: read(A)
T4: write(B)
T2: write(C)
T1: increment(D)
T3: read(F)
T2: increment(E)
T1 T2 T3 T4
write(A)
read(F)
incrément(E)
incrément(D)
write(B)
read(F)
write(C)
increment(E)
increment(C)
read(A)
read(F)
increment(E)
read(D)
write(B)
Schedule 2
T2: write(B)
T1: write(A)
T2: read(F)
T3: increment(C)
T4: increment(E)
T1: read(F)
T2: increment(E)
T1: read(D)
T4: write(B)
T2: write(C)
T3: read(A)
T4: increment(D)
T1: increment(E)
T3: read(F)
T2 T1 T3 T4
write(B)
read(F)
increment(E)
write(C)
write(A)
read(F)
read(D)
increment(E)
increment(C)
read(A)
read(F)
increment(E)
write(B)
increment(D)
2.Identify various transaction control statements in oracle. How is
locking applied in oracle?
Ans. A transaction is a series of SQL statements that either succeeds
or fails as a unit. Transactions are a standard part of relational
databases and prevent inconsistent data. The classic example of this is
a bank transaction:
update accounts
set balance = balance - trans_amount
where account_no = from_account;
update accounts
set balance = balance + trans_amount
where account_no = to_account;
Suppose the first update statement succeeds, but the second statement
fails due to an error. The data is now inconsistent. We prevent this
combining two statements into a transaction, whereby either both
statements will succeed or both statements will fail.
A transaction begins with the first SQL statement issued after the
previous transaction, or the first SQL statement after connection to the
database. The transaction ends with the commit or rollback statement.
Savepoints
The rollback statement undoes the entire transaction. With the savepoint
command, however, only a part of the transaction need be undone.
Note that savepoints are not declared in the declarative section, since
they are global to a transaction, and the transaction can continue past
the end of the block. Once a savepoint is defined, the program can roll
back to the savepoint using the "to savepoint" rollback syntax. When a
rollback to savepoint is issued, the following things occur:
Any work done since the savepoint is undone. The savepoint remains
active, however. It can be rolled back to again, if needed.
Any locks and resources acquired by the SQL statements since the
savepoint will be released.
The transaction is not finished, because SQL statements are still
pending.
savepoint is often used before a complicated section of a transaction. If
this part of the transaction fails, it can be rolled back, allowing the earlier
part to continue
locking in oracle
Locks are mechanisms that prevent destructive interaction between
transactions accessing the same resource.
General Object Type Affected By Locks:
• User objects, such as tables and rows (structures and data)
• System objects not visible to users, such as shared data structures in
the memory and data dictionary rows
Isolation Levels
2. Serializable Transactions
See only those changes that were committed at the time the
transaction began, plus those changes made by the transaction itself
through INSERT, UPDATE, and DELETE statements.
3. Read-Only
See only those changes that were committed at the time the
transaction began and do not allow INSERT, UPDATE, and DELETE
statements.
Setting at Transaction Level:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SET TRANSACTION ISOLATION LEVEL READ ONLY;
Lock duration
• All locks acquired by statements within a transaction are held for the
duration of the transaction.
• Oracle releases all locks acquired by the statements within a
transaction when an explict or implied commit or roll back is
executed. Oracle also releases locks acquired after a savepoint when
rolling back to the savepoint.
* Note: Only transactions not waiting for the previously locked
resources can acquire locks on now available resources. Waiting
transactions continue to wait until after the original transaction
commits or completely rolls back.
Lock modes
DEADLOCK OCCURENCE
• Deadlocks often occur when transactions override Oracle default
locking. Oracle itself does no lock escalation and does not use read
locks for queries and does not use page-level locking, deadlocks
rarely occur in Oracle.
DEADLOCK AVOIDANCE
• Deadlocks can usually be avoided if transactions accessing the same
tables lock those tables in the same order, either through implicit or
explicit locks and when a sequence of locks for one transaction are
required, you should consider acquiring the most exclusive (least
compatible) lock first
• Always close explicit cursors when finished to free locks.
PART B