Module 3 - Advance Database Concepts-Lesson 1
Module 3 - Advance Database Concepts-Lesson 1
Activity
Analysis
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 State
• 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
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?
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.
• Lost update
• Uncommitted dependency
Occurs when:
• 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
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.
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.
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.
Assessment