You are on page 1of 7

IV.

The Lesson Structure

Module No. 3 Advance Database Concepts


Module No. and Title

Lesson No. 1: Transaction Management and Concurrency Control


Lesson No. and Title

At the end of this module, learners are expected to:


• Define and Discuss when and how to use Transactions
• Create transactions using BEGIN, COMMIT and ROLLBACK
Module Learning Outcomes
• Discuss the role of concurrency control in maintaining the database
integrity

Time Frame 5 Hours

Introduction Hello students, welcome to Lesson 1 of Module 3 of Advance Database System!


This lesson discusses the concepts of database transactions and their
properties. What concurrency control is and what role it plays in maintaining
the database’s integrity and what locking methods are and how they work

Activity

Visit the urls:


• https://www.youtube.com/watch?v=C_J6K8DodS8
• https://www.youtube.com/watch?v=d4Ziyuri0L0
• https://www.youtube.com/watch?v=IIcR3G5w4ZI

And give at least one example of each concurrency problem.

Analysis

What does database transaction mean? Why it is important?


Abstraction

Transaction Management

A sequence of many actions that are being performed by a single user or application program, which reads or
updates the contents of the database.

Properties of Transaction

There are properties that all transactions should follow and possess. The four basic are in combination termed as
ACID properties.

• Atomicity: A transaction is a single unit of operation. You either execute it entirely or do not execute it at
all. There cannot be partial execution.
• Consistency: Once the transaction is executed, it should move from one consistent state to another (ex.
integrity constraints).
• Isolation: Transaction should be executed in isolation from other transactions (no Locks). During concurrent
transaction execution, intermediate transaction results from simultaneously executed transactions should
not be made available to each other. (Level 0,1,2,3)
• Durability: · After successful completion of a transaction, the changes in the database should persist. Even
in the case of system failures.

Examples:

Transaction 1 is transferring $50 from account X to account Y.

Begin X=X+50, Y = Y-50 END

Transaction 2 is crediting each account with a 10% interest payment.


Begin X=1.1*X, Y=1.1*Y END

Transaction State

Transaction state A transaction must be in one of the following states:


State Diagram of Transaction

• Active - the initial state, the transaction stays in this state while it is executing.
• Partially committed - after the final statement has been executed.
• Failed - after the discovery that normal execution can no longer proceed.
• Aborted - after the transaction has been rolled back and the database has been restored to its state prior to
the start of the transaction.
• Committed - after successful completion.

Defining Transaction

• To start a transaction, you use the START TRANSACTION statement.


• To commit the current transaction and make its changes permanent, you use the COMMIT statement.
• To roll back the current transaction and cancel its changes, you use the ROLLBACK statement.

Example1:

Assuming we have table STUDENT(id,lastname,firstname, age).The group of operations given below is called as a
transaction.

START TRANSACTION;
INSERT INTO STUDENT(id,lastname,firstname,age) VALUES(1,’Doe’,’John’,25);
INSERT INTO STUDENT(id,lastname,firstname,age) VALUES(2,’Lim’,’Paw’,30);
UPDATE STUDENT SET lastname=’En’, firstname=’Shang’ WHERE id = 2;
ROLLBACK;

You can see that within the above transaction there are two INSERTS and one UPDATE. These are successfully be
executed; however, these will be saved in the database since it issues a ROLLBACK statement.

Example2:

START TRANSACTION;
INSERT INTO STUDENT(id,lastname,firstname,age) VALUES(1,’Doe’,’John’,25);
INSERT INTO STUDENT(id,lastname,firstname,age) VALUES(2,’Lim’,’Paw’,30);
UPDATE STUDENT SET lastname=’En’, firstname=’Shang’ WHERE id = 2;
COMMIT;

Example 2, will successfully execute the TRANSACTION and finally save in the database because of the COMMIT
statement.

Note: On failure of an operation within the transaction in the case of power interruption or any errors , the complete
group of operations are rolled back to its previous state.
What is Concurrency Control?

Concurrency Control in Database Management System is a procedure of managing simultaneous operations


without conflicting with each other. It ensures that Database transactions are performed concurrently and
accurately to produce correct results without violating data integrity of the respective Database.

DBMS Concurrency Control is used to address such conflicts, which mostly occur with a multi-user system.
Therefore, Concurrency Control is the most important element for proper functioning of a Database Management
System where two or more database transactions are executed simultaneously, which require access to the same
data.

Problems in Concurrency Control

• Lost update

• Occurs in two concurrent transactions when:

• Same data element is updated

• One of the updates is lost

• Uncommitted dependency

Occurs when:

• Two transactions are executed concurrently

• First transaction is rolled back after the second transaction has already accessed
uncommitted data

• Inconsistent Analysis

Occurs when a transaction accesses data before and after one or more other transactions finish working
with such data

Why use Concurrency method?

Reasons for using Concurrency control method is DBMS:

• To apply Isolation through mutual exclusion between conflicting transactions


• To resolve read-write and write-write conflict issues
• To preserve database consistency through constantly preserving execution obstructions
• The system needs to control the interaction among the concurrent transactions. This control is achieved
using concurrent-control schemes.
• Concurrency control helps to ensure serializability

Example

Assume that two people who go to electronic kiosks at the same time to buy a movie ticket for the same movie and
the same show time.

However, there is only one seat left in for the movie show in that particular theatre. Without concurrency control in
DBMS, it is possible that both moviegoers will end up purchasing a ticket. However, concurrency control method
does not allow this to happen. Both moviegoers can still access information written in the movie seating database.
But concurrency control only provides a ticket to the buyer who has completed the transaction process first.

Concurrency Control techniques in DBMS:

1. Lock-Based Protocols
In DBMS is a mechanism in which a transaction cannot Read or Write the data until it acquires an
appropriate lock. Lock based protocols help to eliminate the concurrency problem in DBMS for
simultaneous transactions by locking or isolating a particular transaction to a single user.

2. Two Phase Locking Protocol

Two Phase Locking Protocol also known as 2PL protocol is a method of concurrency control in DBMS that
ensures serializability by applying a lock to the transaction data which blocks other transactions to access
the same data simultaneously

3. Timestamp-Based Protocols

Timestamp based Protocol in DBMS is an algorithm which uses the System Time or Logical Counter as a
timestamp to serialize the execution of concurrent transactions. The Timestamp-based protocol ensures
that every conflicting read and write operations are executed in a timestamp order.

4. Validation-Based Protocols

Validation based Protocol in DBMS also known as Optimistic Concurrency Control Technique is a method to
avoid concurrency in transactions. In this protocol, the local copies of the transaction data are updated
rather than the data itself, which results in less interference while execution of the transaction.

Characteristics of Good Concurrency Protocol

An ideal concurrency control DBMS mechanism has the following objectives:

• Must be resilient to site and communication failures.


• It allows the parallel execution of transactions to achieve maximum concurrency.
• Its storage mechanisms and computational methods should be modest to minimize overhead.
• It must enforce some constraints on the structure of atomic actions of transactions.
Application

Perform the given tasks.

1. Create relation emptrans.

2. Create a stored procedure.

3. Execute the Stored Procedure


a. CALL EmployeeTransaction(1,200,2);
b. CALL EmployeeTransaction(2,300,3);
c. CALL EmployeeTransaction(3,400,4);
What it the final balance of each employee? (Show the screenshot of the results of your SQL query.)

4. Execute the Stored Procedure


CALL EmployeeTransaction(1,-75,2);

What happen to the account of employee#1? Why?

5. Execute the Stored Procedure


CALL EmployeeTransaction(2,500,2);

What happen to the account of employee#2? Why?

Assessment

for completing the 1st lesson of the 3rd module in Advance


Database System

You might also like