You are on page 1of 42

Database Systems

Introduction to Transaction
Processing Concepts

1
Transaction Processing

•Parsing

•Query Optimization

•Execution: Transaction Initiation

2
Transaction
• It is a set of operations used to
perform a logical unit of work.
– For example, transfer money, withdraw
amount.
• A transaction generally represent
change in database.

3
A Transaction: An Informal Example
• Transfer 400,000 from checking account to
savings account
• For a user it is one activity
• To database:
– Read balance of checking account: read( X)
– Read balance of savings account: read (Y)
– Subtract 400,000 from X
– Add 400,000 to Y
– Write new value of X back to disk
– Write new value of Y back to disk
4
A Transaction: A Formal Example
T1
t0 read_item(X);
read_item(Y);  Read the data
 Modify read data value
X:=X - 400000;  Write the data back
 Commit the transactio
Y:=Y + 400000;
write _item(X);
tk write_item(Y);
If successful, then DB is in
consistent state otherwise 5
Details of the read operation
• Read-item (X)
1. Find the address of the disk block that
contains item X
2. Copy the disk block into a buffer in main
memory
3. Copy the item X from the buffer to the
program variable named X

6
1. Locate the disk block that contains the data item x

7
2. Read (copy) the disk block that contains the data item x
into a system buffer (inside the Operating System)

8
3. Read (copy) the data item x from the Operating System
buffer into a program variable inside the DBMS

9
Details of Write Operation
• Write-item (X)
1. Find the address of the disk block that contains item X.
2. Copy that disk block into a buffer in main memory
3. Copy item X from the program variable named X into its correct
location in the buffer.
4. Store the updated block from the buffer back to disk (either
immediately or at some later point in time).

10
1. First, note that the DBMS variable x will be
used to update the data item x in the database.

11
1.Locate the disk block that contains the data item
x:

12
2. Read (copy) the disk block that contains the data item x
into a system buffer (inside the Operating System):

13
3. Write (copy) the value:

14
4. Write (copy) the disk block to disk:

15
Challenges:

Updating the data in memory and updating the data in the


database
In place (or immediate) write = a write operation to the
database is issued as soon as a memory variable is updated –
frequent I/O operations
Deferred write = all write operations to the database are
issued at the end of the transaction – greater recovery time
16
Transaction and System Concepts
Transaction states
1.BEGIN_TRANSACTION: marks start of transaction
2.READ or WRITE: two possible operations on the data
3.END_TRANSACTION: marks the end of the read or write
operations; start checking whether everything went
according to plan
4.COMMIT_TRANSACTION: signals successful end of
transaction; changes can be “committed” to DB
5.ROLLBACK (or ABORT): signals unsuccessful end of
transaction, changes applied to DB must be undone

18
1.Atomicity:
Atomicity ensures that a transaction is treated
as a single, indivisible unit of work. Either all the
changes made within the transaction are
committed to the database, or none of them
are. If any part of the transaction fails (due to
errors or exceptions), the entire transaction is
rolled back to its initial state, ensuring that the
database remains in a consistent state.

19
2. Consistency:
Consistency ensures that a transaction brings
the database from one valid state to another.
The database must satisfy a set of integrity
constraints before and after the transaction. If a
transaction violates the integrity constraints, it is
rolled back, preserving the overall consistency
of the database.

20
3. Isolation:
Isolation ensures that the execution of one
transaction is isolated from the execution of
other transactions. Even though multiple
transactions may be executing concurrently,
each transaction is unaware of the others until
they commit. Isolation prevents interference and
ensures that the final state of the database is
the same as if the transactions were executed
serially, one after the other.
21
4. Durability:
Durability guarantees that once a transaction is
committed, its effects are permanent, and they
survive any subsequent failures, such as power
outages or system crashes. The changes made
by committed transactions are stored in non-
volatile memory (e.g., disk storage), and even in
the event of a system failure, the database can
be recovered to a consistent state using
transaction logs.
22
Concurrent Transactions

B B B
CPU2
A A
CPU1 A
CPU1

time
t1 t2 t1 t2
Concurrent or Interleaved processing Parallel processing
(Single processor) (Two or more processors)

23
Example

24
Concurrency
• Concurrency relates to an application that is processing
more than one task at the same time.
• Concurrency is an approach that is used for decreasing
the response time of the system by using the single
processing unit.
• Concurrency creates the illusion of parallelism, however
actually the chunks of a task aren’t parallel processed,
but inside the application, there are more than one task
is being processed at a time.
• It doesn’t fully end one task before it begins another

25
Transaction operations in DB
• In any database, transaction operations are
composed of two important tasks:
– Read: Read the data value from the database. It
is a safe operation, as it does not update any
information in the database.
– Write : It writes data into the database and
saved it for further use. This operation is prone
to vulnerability as it involves modifying
database information.

26
Read-Write conflict
• While these operations are performed by different users
on the same data at the same time a data conflict may
arise.

• How to make the simultaneous interactions of


multiple users with the database safe,
consistent, correct, and efficient?
– an airline reservations system is used by hundreds
of travel agents and reservation clerks concurrently

27
What can go wrong in transaction processing…

Three problems are


1. The lost update problem
2. The temporary update (dirty read)
problem
3. Incorrect summary problem

28
Lost update
• An update is lost if after executing a transaction,
the result of the transaction is not reflected in the
final database state

29
An example of a concurrent execution of database transactions that
results in a lost update

30
Dirty Reads
• When a transaction is allowed to read a row
that has been modified by another
transaction which is not committed yet that
time Dirty Reads occurred.

• It is mainly occurred because of multiple


transaction at a time which is not committed.

31
Example
• If we have a ticket booking system and One Customer is trying to
book a ticket at that time available number of the ticket is 10, before
completing the payment.

• The Second Customer wants to book a ticket that time this 2nd
transaction will show the second customer that the number of the
available tickets is 9.

• The twist is here if the first customer does not have sufficient fund in
his debit card or in his wallet then the 1st transaction will Rollback,
that time 9 seat available which is read by the 2nd transaction is
Dirty Read.

32
During the payment of 1st
customer 2nd transaction read it 9
seat is available if some how 1st
transaction Rollback then available
seat 9 that is Dirty read data. After
rollback of the 1st transaction
available seat is 10 again.

33
Desirable Properties of Transactions
1. Atomicity
A transaction is an atomic unit of processing; it is either
performed in its entirety or not performed at all.

2. Consistency preservation
A transaction is consistency preserving if its complete execution takes the
database from one consistent state to another

3. Isolation
The execution of a transaction should not be interfered with by any other
transactions executing concurrently

4. Durability
The changes applied to the database by a committed transaction must
persist in the database. These changes must not be lost because of any
failure
34
Problems

• Inconsistent state due to concurrency


– Concurrency Control subsystem ensures
scheduling

• System failures may occur


– Types of failures:
• System crash
• Errors
• Disk failure
• Physical failures
– DBMS has a Recovery Subsystem to
protect database against system failures.
35
Transaction States: A state transition diagram

 Active: Insertion or deletion or updating a record is done here. But all the
records are still not saved to the database.
 Partially committed: A transaction executes its final operation, but the
data is still not saved to the database.
 Committed: A committed state when it executes all its operations
successfully.
 Failed: If any of the checks made by the database recovery system fails,
then the transaction is said to be in the failed state.

36
Logs in DBMS
• Logs are one of the mechanisms used for recovering DB
from failure.

• It will have details about each step in the transaction so


that in case of any failure, database can be recovered to
the previous consistent state or taken to the consistent
state after transaction.

37
Checkpoints
• Whenever transaction logs are created in a real-time environment, it
eats up lots of storage space.

• Also keeping track of every update and its maintenance may


increase the physical space of the system.

• The checkpoint is used to declare a point before which the DBMS


was in the consistent state, and all transactions were committed.

38
Schedule
• A schedule is defined as an execution sequence or
arrangement of transactions.

• A schedule maintains the order of the operation in each


individual transaction.

• A schedule is the arrangement of transaction operations.

• To run transactions concurrently, we arrange or schedule


their operations in an interleaved fashion.

39
Example of a Schedule
• Transaction T1: r1(X); w1(X); r1(Y); w1(Y); c1
• Transaction T2: r2(X); w2(X); c2

• A schedule S is,
S: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y); c1; c2

40
Example of a Schedule
• Ordering of execution of operations from various
transactions T1, T2, … , Tn is called a schedule S.

non- serial
serial
41
Conflicts
• Two operations conflict if they satisfy ALL three
conditions:
1. they belong to different transactions AND
2. they access the same item AND
3. at least one is a write_item()operation
• Example.:
– S: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y);

conflicts

42

You might also like