Sie sind auf Seite 1von 12

MODULE OF INSTRUCTION

Database Transaction, Concurrentcy


Control and Locking

Welcome to the fourth module of this course. For this lesson, we will
discuss how to manage different transactions that triggers the database
to perform changes based on request.

It is important to identify when to save permanently, when to retrieve


data and what are the causes a system failure. What is the state of the
data before and after commit or rollback statement is issued and why
locking and deadlock is encountered.

User and Roles


Transactions are completed by COMMIT or ROLLBACK SQL
statements, which indicate a transaction’s beginning or end. The ACID
acronym defines the properties of a database transaction, as follows:

 Atomicity: A transaction must be fully complete, saved


(committed) or completely undone (rolled back). A sale in a
retail store database illustrates a scenario which explains
atomicity, e.g., the sale consists of an inventory reduction and a
record of incoming cash. Both either happen together or do not
happen - it's all or nothing.

 Consistency: The transaction must be fully compliant with the


state of the database as it was prior to the transaction. In other
words, the transaction cannot break the database’s constraints.
For example, if a database table’s Phone Number column can
only contain numerals, then consistency dictates that any
transaction attempting to enter an alphabetical letter may not
commit.

 Isolation: Transaction data must not be available to other


transactions until the original transaction is committed or rolled
back.

 Durability: Transaction data changes must be available, even


in the event of database failure.

After completing this lesson, the student should be able to:

Database Management System 2 1


Week 5-6 Database Transaction, Concurency and Locking

• Define what is Transaction, Concurrency and Locking.


• Enumerate and make use of four (40 types of Database
Transaction.
• Define when does a Transaction starts and ends.
• Describe the locking mechanism and explain how Oracle
manages data concurrency.
• Define and identify the cause of Locking.

What is a Transaction?

 The oracle server ensures that data consistency is based on


transactions.

 Transaction – gives you more flexibility and control when


changing data, and they ensure data consistency in the event of
user process or system failure.

 One or more SQL statements altogether treated as one single


unit.

 Also known as a Unit of Work (UOW)

ACID rules

Atomicity

 All statements in the transaction are treated as a unit.

 If the transaction completes successfully, everything is


committed.

 If the transaction fails, everything done up to the point


of failure is rolled back.

Consistency

 Any transaction will take the data from one consistent


state to another, so only valid consistent data is stored
in the database.

Isolation

 Concurrent transactions cannot interfere with each


other.

2
MODULE OF INSTRUCTION

Durability

 Committed transactions have their changes persisted in


the database.

A database transaction is consists of one of the ff:

Transaction Types:

1. DML (Data Manipulation Language) – consist of any


number or DML statements that oracle server treat as a
single entity or logical unit of work.
(INSERT,UPDATE,DELETE)

2. DDL (Data Definition Language) – consist of only


one DDL statement. (CREATE TABLE)

3. DCL (Data Control Language) – consist of only one


DCL statement. (COMMIT)

Advantages of Commit and Rollback statement

1. Ensures data consistency.

2. Preview data changes before making changes permanent.

3. Group logically related operations.

Explicit Transaction Control Statements

1. COMMIT;

 ends the current transactions by making all pending


data changes permanent.

2. SAVEPOINT savepoint_name;

 Marks a savepoint within the current transaction.

3. ROLLBACK;

 ends the current transaction by discarding all pending


data changes.

Database Management System 2 3


Week 5-6 Database Transaction, Concurency and Locking

4. ROLLBACK TO savepoint_name rolls back the current


transaction to the specified savepoint, thereby discarding any
changes or savepoint created after the savepoint to which you
are rolling back.

Implicit transaction processing

• An automatic commit occurs under the ff. circumstances:

 DDL statement is issued.

 DCL statement is issued.

 Normal exits from sqlplus without explicitly issuing


commit or rollback statement. (Committed by typing
EXIT in isqlplus)

• An automatic rollback occurs under an abnormal termination of


sqlplus or a system failure. (Committed by clicking the Close
button (x) of isqlplus ).

 System Failure

 When a transaction is interrupted by a system


failures the entire transaction is automatically
rolled back. Return the table to the last state at
the time of the last commit.

State of the data before commit or rollback

1. The previous state of the data can be recovered.

2. The current user can review the results of the DML operations
by using the SELECT statement.

3. Other user cannot view the results of the DML statement by the
current user.

4. The affected rows are locked other users cannot change the
data within the affected rows.

State of the Data after Commit or Rollback

4
MODULE OF INSTRUCTION

After Commit

 Data changes are made permanent in the database.

 The previous state of the data is permanently lost.

 All users can view the results.

 Locks on the affected rows are released; those rows are


available for other users to manipulate.

 All savepoints are erased.

After Rollback

• Discard all pending changes by using the rollback statement:

– Data changes are undone

– Previous state of the data is restored

– Locks on the data affected rows are release.

Concurrency and Locking

 Prevent multiple sessions from changing the same data at the


same time.

 Are automatically obtained at the lowest possible level for a


given statement.

 Do not escalate.

 Locks are acquired automatically as needed to support a


transaction based on “isolation levels”.

 COMMIT and ROLLBACK statements release all locks.

Concurrency:

 Multiple users accessing the same resources at the same


time.

Locking:

 Mechanism to ensure data integrity and consistency

Database Management System 2 5


Week 5-6 Database Transaction, Concurency and Locking

Locking Mechanism

 The locking mechanism is designed to provide the maximum


possible degree of data concurrency within the database.
Transactions that modify data acquire row-level locks rather
than block-level or table-level locks. Modifications to objects
(such as table moves) obtain object-level locks rather than
whole database or schema locks.

 Data queries do not require a lock, and a query succeeds even


if someone has locked the data (always showing the original,
prelock value reconstructed from undo information).

 When multiple transactions need to lock the same resource, the


first transaction to request the lock obtains it. Other
transactions wait until the first transaction completes. The
queue mechanism is automatic and requires no administrator
interaction.

 All locks are released as transactions are completed (that is,


when a COMMIT or ROLLBACK is issued). In the case of a
failed transaction, the same background process that
automatically rolls back any changes from the failed
transaction releases all locks held by that transaction.

Two basic types of locks:

1. Share locks (S locks) – acquired when an application wants to


read and prevent others from updating the same row.

2. Exclusive locks (X locks) – acquired when an application


updates, inserts, or deletes a row

Locking Mechanism

• High level of data concurrency:

– Row-level locks for inserts, updates, and deletes

– No locks required for queries

• Automatic queue management

6
MODULE OF INSTRUCTION

• Locks held until the transaction ends (with the COMMIT or


ROLLBACK operation)

• Each DML transaction must acquire two locks:

– EXCLUSIVE row lock on the row or rows being


updated

– Table lock (TM) in ROW EXCLUSIVE (RX) mode on


the table containing the rows

Enqueue Mechanism

• The enqueue mechanism keeps track of:

– Sessions waiting for locks

– Requested lock mode

– Order in which sessions requested the lock

• Requests for locks are automatically queued. As soon as the


transaction holding a lock is completed, the next session in line
receives the lock.

• The enqueue mechanism tracks the order in which locks are


requested and the requested lock mode.

• Sessions that already hold a lock can request that the lock be
converted without having to go to the end of the queue. For
example, suppose a session holds a SHARE lock on a table.
The session can request that the SHARE lock be converted to
an EXCLUSIVE lock. If no other transaction already has an
EXCLUSIVE or SHARE lock on the table, the session holding
the SHARE lock is granted an EXCLUSIVE lock without
having to wait in the queue again.

Possible Causes of Lock Conflicts and Problems if there is no


Concurrency Control

Possible Causes of Lock

1. Uncommitted changes

2. Long-running transactions

Database Management System 2 7


Week 5-6 Database Transaction, Concurency and Locking

3. Unnecessarily high locking levels

Problems if there is no Concurrency Control

1. Lost update

2. Uncommitted read

3. Non-repeatable read

4. Phantom read

Locks

 Before the database allows a session to modify data, the


session must first lock the data that is being modified. A lock
gives the session exclusive control over the data so that no
other transaction can modify the locked data until the lock is
released.

 Transactions can lock individual rows of data, multiple rows,


or even entire tables. Oracle Database supports both manual
and automatic locking. Automatically acquired locks always
choose the lowest possible level of locking to minimize
potential conflicts with other transactions.

Lock Wait

 By default, an application waits indefinitely to obtain any


needed locks.

Deadlock

 Occurs when two or more applications wait indefinitely for a


resources.

 Each application is holding a resource that the other needs.

 Waiting is never resolved.

 When two (2) or more users are accessing the same table and
waits for the first transaction to finished it is called as
‘Deadlocks’.

8
MODULE OF INSTRUCTION

 A deadlock is a special example of a lock conflict. Deadlocks arise


when two or more sessions wait for data that has been locked by
the other. Because each is waiting for the other, neither can
complete their transaction to resolve the conflict.

 Oracle Database automatically detects deadlocks and terminates


the statement with an error. The proper response to that error is
either commit or rollback, which releases any other locks in that
session so that the other session can continue its transaction.

LESSON SUMMARY:

 A database transaction are changes made by the DBA


(Database Administrator) that triggers the database to perform
come changes based on request.

 Transaction start when first DML (Data Manipulation


Language) is encountered and ends when a user closer the
sqlplus or encounter system failure.

 System Failure are uncontrolled circumstances beyond the


control of the DBA.

 A transaction is composed of four (4) types:

 COMMIT: when a user wanted to make changes


permanent.

 ROLLBACK: is used when a user wanted to discard all


changes made.

 SAVEPOINT: is used to divide the transaction into


smaller section.

 ROLLBACK TO SAVEPOINT: is used to rollback to a


specific marker.

 If there is no concurrency control the user will encounter the


following problems: Lost Update, Uncommitted Read, Non-
repeatable read and Phantom.

 Lock wait is encounter when two simultaneous user were


accessing the same table.

Database Management System 2 9


Week 5-6 Database Transaction, Concurency and Locking

 Deadlocks is occurs when two or more applications wait


indefinitely for a resource.

Activities/Exercises
 Download the Laboratory Exercise 2: Transaction
 Download the Departments.txt – this is the table to use
for the succeeding activity
 Follow the instruction carefully, when you skip once
transaction that must be performed before jumping
tothe next number this might cause a different output.
 In each number questions with essay part should
answered briefly, question with PL/SQL requires the
PL/SQL used in order to come up with the solution.

Glossary
 Atomicity - All statements in the transaction are treated as a
unit.

 COMMIT - ends the current transactions by making all


pending data changes permanent.

 Concurrency - Multiple users accessing the same resources at


the same time.

 Consistency - Any transaction will take the data from one


consistent state to another, so only valid consistent data is
stored in the database

 Deadlock - Occurs when two or more applications wait


indefinitely for a resource

 Durability - Committed transactions have their changes


persisted in the database.

 Exclusive locks (X locks) – acquired when an application


updates, inserts, or deletes a row

 Isolation - Concurrent transactions cannot interfere with each


other.

 Locks - are acquired automatically as needed to support a


transaction based on “isolation levels”.

10
MODULE OF INSTRUCTION

 Locking - Mechanism to ensure data integrity and consistency

 ROLLBACK - ends the current transaction by discarding all


pending data changes.

 ROLLBACK TO savepoint_name - rolls back the current


transaction to the specified savepoint, thereby discarding any
changes or savepoint created after the savepoint to which you
are rolling back.

 Share locks (S locks) – acquired when an application wants to


read and prevent others from updating the same row.

 SAVEPOINT savepoint_name - Marks a savepoint within the


current transaction.

 Transaction – gives you more flexibility and control when


changing data, and they ensure data consistency in the event of
user process or system failure.

References
Textbook:
 Oracle Database 11g 2nd Edition K Gopalakrishnan (
2012)

References:
 Carlos, Peter (2009). Database Systems
 Connoly, Thomas & Begg, Carolyn (2010). Database
Systems : A practical approach to design,
implementation and management
 Sciore, Edward (2009). Database Design and
Implementation
 Bulusu, Lakshman (2008). Oracle PL/SQL : Expert
Techniques for Developers and Database
Administrators
 Loshin, David (2008). Master Data Management

Other Suggested Readings (e.g. periodicals, articles, websites, IT


applications/software, etc.):
 www.oracle.com
 www.apex.oracle.com

Database Management System 2 11


Week 5-6 Database Transaction, Concurency and Locking

 SQL Tutorial. In ws3schools, Retrieved from


http://www.w3schools.com/sql/default.asp
 SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/S
QL
 Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_administ
ration.aspx
 SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspxLearning
Icons

12

Das könnte Ihnen auch gefallen