0% found this document useful (0 votes)
16 views22 pages

Database Transaction Management

The document outlines the principles of ACID transaction management, which includes Atomicity, Consistency, Isolation, and Durability, essential for database transactions. It describes a simple transaction model, various SQL transaction commands, isolation levels, and concurrency control schemes, including lock-based, timestamp-based, and optimistic concurrency control. Additionally, it covers implementing snapshot locking and restrictions on database operations using SQL.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Database Transaction Management

The document outlines the principles of ACID transaction management, which includes Atomicity, Consistency, Isolation, and Durability, essential for database transactions. It describes a simple transaction model, various SQL transaction commands, isolation levels, and concurrency control schemes, including lock-based, timestamp-based, and optimistic concurrency control. Additionally, it covers implementing snapshot locking and restrictions on database operations using SQL.

Uploaded by

mariamgodwin987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DATABASE MANAGEMENT

GROUP NO 8
1. AMOS MVULA: 23050513033
2. BARAKA MAKUNDI: 23050513096
3. JOSEPH J JOSEPH: 23050513026
4. JUSTIEL RICHARD: 23050513112
5. ADAM MWAMIN: 23050513013
6. ZAKIA RAMADHAN:23062213081
Describe ACID Transaction Management

• ACID stands for Atomicity, Consistency, Isolation, and Durability, which are the key
properties of a transaction in a database system:

• 1. Atomicity: Ensures that all operations within a transaction are completed successfully. If
any operation fails, the entire transaction is aborted, and the database state is unchanged.

• 2. Consistency: Guarantees that a transaction brings the database from one valid state to
another valid state, maintaining all defined rules and constraints (like foreign keys).

• 3. Isolation: Ensures that transactions are executed in isolation from each other.
Intermediate states of a transaction are not visible to other transactions until it is
committed.

• 4. Durability: Ensures that once a transaction is committed, the changes are permanent
and survive system failures.
Describe a Simple Transaction Model in the Database

• A simple transaction model involves the following steps:

1. Begin Transaction: Marks the start of the transaction.

2. Execute Operations: Perform the necessary SQL operations (INSERT, UPDATE, DELETE).

3. Commit/Rollback: Commit saves the changes permanently, while rollback undoes the operations in case of
failure.
c) Write Transactions Using SQL

• Example: Transferring money between accounts. • UPDATE accounts


• BEGIN TRANSACTION; • SET balance = balance + 200
• UPDATE accounts • WHERE account_id = 2;
• SET balance = balance - 200
• IF @@ERROR > 0
• WHERE account_id = 1;
• ROLLBACK;
• COMMIT;
• ELSE

• COMMIT;
D) Implement Isolation Levels in the Relational Database
• Isolation levels determine how transactions interact with each other,
particularly concerning visibility and consistency of data. They are
crucial for maintaining data integrity in multi-user database
environments.
• Importance of Isolation Levels
• Data Integrity: Isolation levels help ensure that transactions do not
interfere with each other, preserving the integrity of the database.
• Concurrency: They allow multiple transactions to operate
concurrently while managing potential conflicts.
• Performance: Different isolation levels offer various trade-offs
between data consistency and system performance.
• Read Uncommitted:
• Description: Allows transactions to read data that has been modified but not yet
committed by other transactions. This can lead to dirty reads.
• Use Case: Useful in scenarios where performance is critical, and exact data consistency
is not a priority.
• Example:
• set transaction isolation level read uncommitted;
• Read Committed:
• Description: Ensures that transactions only read data that has been committed. This
prevents dirty reads but allows non-repeatable reads.
• Use Case: Commonly used in applications where you need to avoid dirty reads but can
tolerate changes in data between reads.
• Example:
• set transaction isolation level read committed;
Cont…
• Repeatable Read:
• Description: Guarantees that if a transaction reads a row, subsequent reads
will see the same value, preventing both dirty reads and non-repeatable
reads. However, it may allow phantom reads.
• Use Case: Suitable for applications requiring a higher degree of consistency
while allowing some concurrency.
• Example:
• set transaction isolation level repeatable read;
• Serializable:
• Description: The highest isolation level, ensuring complete isolation from
other transactions. It prevents dirty reads, non-repeatable reads, and
phantom reads by locking the data being read.
Example: Setting Isolation Level in SQL
• SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

• START TRANSACTION;

• SELECT * FROM accounts WHERE balance > 300;

• COMMIT;
e) Describe Concurrent Control Schemes

• Concurrency control schemes manage simultaneous transactions to avoid conflicts:

1. Lock-Based Protocols: Use locks (shared or exclusive) to control access.

2. Timestamp-Based Protocols: Use timestamps to order transactions.

3. Optimistic Concurrency Control: Assumes minimal conflict and validates transactions before commit.
1. Lock-Based Protocols
• Lock-based protocols use locks to control access to data resources during transactions.
Locks prevent multiple transactions from accessing the same resource in conflicting
ways.
• Types of Locks:
 Shared Lock (S): Allows multiple transactions to read a resource simultaneously but
prevents any transaction from writing to it.
 Exclusive Lock (X): Prevents all other transactions from reading or writing to a
resource.
• Lock Modes:
 Read Locks: Multiple transactions can hold shared locks for reading.
 Write Locks: Only one transaction can hold an exclusive lock for writing.
• Lock-Based Protocols:
 Two-Phase Locking (2PL):
o Growing Phase: A transaction can acquire locks but cannot release
them.
o Shrinking Phase: A transaction releases locks and cannot acquire
new ones.
o Ensures serializability but may lead to deadlocks.
 Strict Two-Phase Locking: Locks are held until the transaction is
committed or rolled back, preventing cascading rollbacks.
Example:
• BEGIN TRANSACTION;

• SELECT * FROM accounts WITH (XLOCK) WHERE account_id = 1;


• UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;

• SELECT * FROM accounts WITH (XLOCK) WHERE account_id = 2;

• UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;

• COMMIT;
• Advantages:

 Ensures strict consistency.

 Prevents write conflicts.

• Disadvantages:

 Can lead to deadlocks and reduced concurrency.


2. Timestamp-Based Protocols
• Timestamp-based protocols assign a unique timestamp to each transaction. Transactions are ordered based on
their timestamps to avoid conflicts.

• How It Works:

 Each data item has two timestamps:


o Read Timestamp (RTS): The last time the data was read.

o Write Timestamp (WTS): The last time the data was modified.

 A transaction’s timestamp determines whether it can read or write a data item:


o Read Rule: A transaction can read a data item if its timestamp is greater than or equal to the data item's
WTS.
o Write Rule: A transaction can write to a data item if its timestamp is greater than the RTS and WTS.
• Advantages:

 No locks are required, avoiding deadlocks.

 Ensures serializability.

• Disadvantages:

 May result in abortions of transactions due to timestamp conflicts.

 Overhead in maintaining timestamps.


3. Optimistic Concurrency Control (OCC)
• OCC assumes that conflicts are rare and allows transactions to proceed without locking resources.
Transactions are validated before committing to ensure consistency.

• Phases in OCC:

1. Read Phase: The transaction reads data and performs operations locally without locking any data.

2. Validation Phase: Before committing, the transaction checks if its operations conflict with other ongoing
transactions.

3. Write Phase: If validation is successful, the transaction writes its changes to the database.
• Advantages:

 Maximizes concurrency and system throughput.

 No locks are used, avoiding deadlocks.

• Disadvantages:

 High overhead due to validation checks.

 Transactions may be frequently aborted in high-conflict scenarios.


g) Implement Snapshot Locking Scheme Using SQL

• Snapshot isolation allows reading data from a consistent snapshot, avoiding


read locks. Example:
• SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
• BEGIN TRANSACTION;
• SELECT * FROM accounts WHERE balance > 1000;
• COMMIT;
h) Implement Restrictions on Read, Write, and Delete Operations
Using SQL
• You can implement restrictions using triggers, stored procedures, or by setting permissions at the user level.
• -- Create a view that only shows certain columns
• CREATE VIEW limited_view AS
• SELECT column1, column2 FROM your_table;
• -- Grant select permission on the view
• GRANT SELECT ON limited_view TO 'limited_user'@'localhost';

You might also like