You are on page 1of 21

INT306 – DBMS

Transaction Processing and Control


Topics
• Database Transactions
Sample Transaction

• Transaction Properties
Atomicity
Consistency
Isolation

INT306 – DBMS
Durability

• Transaction Control Statements


• Locks
• Exercises

2
Transactions
• A transaction comprises a unit of database work
• Transactions take the database from one consistent state to the next

INT306 – Database Management


consistent state.
🞄In Oracle databases a transaction implicitly begins with the first statement that
modifies data.
🞄Issuing either a C O M M I T or R O L L B A C K statement explicitly ends a transaction.

• When you issue a C O M M I T, you are assured that all of your changes have
been successfully saved and that any data integrity checks and rules have
been validated.
• Oracle’s transactional control architecture ensures that consistent data is
provided every time.

3
Transaction Control Statements
• The following are the transaction control statements:
🞄C O M MIT or C O M MIT WO RK

INT306 – Database Management


🞄 ends your transaction and makes any changes permanent (durable).
🞄RO L L B A C K or R O L L B A C K WO RK
🞄 ends your transaction and undoes any uncommitted changes.
🞄S AV E POIN T:
🞄 creates a marked point within a transaction.
🞄RO L L B A C K TO < S AV E P O I N T >:
🞄rolls back the transaction to the marked point without rolling back any of the work that
preceded it.
🞄S E T TRA N S A C TION:
🞄 allows you to set various transaction attributes, such as the transaction’s isolation level
and whether it is read-only or read-write.

4
Transactions Properties
• Transactions in Oracle exhibit all of the required AC I D properties:
🞄Atomicity:

INT306 – Database Management


🞄 Either all of a transaction happens or none of it happens.
🞄C onsistency:
🞄 A transaction takes the database from one consistent state to the next.
🞄Isolation:
🞄The effects of a transaction may not be visible to other transactions until the
transaction has committed.
🞄D urability:
🞄 Once the transaction is committed, it is permanent.

5
Sample Transaction
• In Specialty Imports, Chuck Jones trades in one car and buys another.
Let's look at the sequence of steps and the database action.

INT306 – Database Management


• INSERT INTO si.customer /* chuck jones */
1. Establish Chuck Jones
as a customer.
• INSERT INTO si.saleinv /* new invoice */
2. Create Bill of Sale
• UPDATE si.car SET custname='CHUCK JONES' /* new car */
3. Establish new car
ownership by setting
customer name to • INSERT INTO purchinv /* buy from Chuck */
Chuck • INSERT INTO si.car /* enter old car */

4. Trade in old car • UPDATE si.car SET custname = NULL /* old car */
• INSERT INTO si.tradeinv /* old car */

• All steps are part of the transaction – treated as one entity.


6
Atomicity
• A business transaction can be a series of changes. For example, a series of
D Q L , D M L and D D L statements

INT306 – Database Management


• A transaction is Atomic if it succeeds as a whole (all the components
succeed) or it fails as a whole when one or more of the components fail.
• Atomic transaction cannot partially fail or partially succeed
• There is a certain class of statements in Oracle that are atomic—but only
at the statement level.
🞄D D L statements are implemented in a manner such that:
1. They begin by committing any outstanding work, ending any transaction you might
already have in place.
2. They perform the D D L operation, such as a C R E AT E TAB L E, A LT E R TA B L E etc.…
3. They commit the D D L operation if it was successful or, if not successful, roll back the
D D L operation.

7
Consistency
• A transaction must bring database to a valid state. In other words, a
transaction should not introduce wrong data to the database.

INT306 – Database Management


• Relational database management systems have tools that help to minimize
the chance of having data inconsistencies. For example: the tabular
structure, data types, constraints and other tools.
• Relational databases in a third normal form (no redundancies) help to
prevent update anomalies.

8
Isolation
• Each transaction must be dealt with in isolation even if other transactions
are run concurrently.

INT306 – Database Management


• For example, if multiple users try to update the same data at the same
time, there should be a mechanism that allows the conflict to be resolved.

9
Durability
• Is the contract between the database and the user that states that a
transaction is stored in the database when it is completed no matter what
happens (even if the system fails).

INT306 – Database Management


10
L ocks
• Whenever an I N S E RT, U P DAT E or D E L E T E statement is issued, the data
is not immediately sent to the table but, rather, is temporarily stored in the

INT306 – Database Management


user's buffer.
• When the user issues a S E L E C T statement to view the data, data from the
table is combined with the data from the buffer before presenting it to the
user, and the user thinks that the data has been inserted.
• Other users, however, do not see the data. Locks prevent users from
destroying each other's data when they are simultaneously making
changes. So locks are very important.
• There are two statements which release the locks:
• COMMIT
• ROLLBACK

11
L ocks
• Locks are acquired when any process accesses a piece of data where there is a
chance that another concurrent process will also need this same piece of data at
the same time. By locking the piece of data, we ensure that we can take action on

INT306 – Database Management


that data the way we expect.
• Blocking occurs when two processes need access to same piece of data
concurrently so one process locks the data and the other one needs to wait for the
other one to complete and release the lock. As soon as the first process is complete,
the blocked process resumes operation. The blocking chain is like a queue: once the
blocking process is complete, the next processes can continue.
• Deadlock occurs when one process is blocked and waiting for a second process to
complete its work and release locks. At the same time, the second process is
blocked and waiting for the first process to release the lock.

12
Transactions Atomicity
• A transaction is an atomic block of D M L s
that must be saved

INT306 – Database Management


• You can not save part of the transaction to
the database. It will either be saved as a
whole or none of the modifications will be
saved

• A transaction is defined as all the D M L


statements that are placed between two
CO M M I Ts or between the First statement in
the session and the following C O M M I T

• The S E L E C T statement in line 26 should


return the below result

13
Transactions Atomicity
• R O L L B A C K cancels a transaction. It is like
an Undo button; it undoes changes made

INT306 – Database Management


since the previous C O M M I T or the beginning
of the session, whichever comes first

• Run the select statement from the previous


slide and the result should look like this

There is no guarantee that the database will retrieve the data in


the same order that you inserted it. That is why you need to use
O R D E R B Y in your select statement if you need an order

14
Transactions Atomicity This part was excluded
from the transaction

• S AV E P O I N T is used as a marker, and we can


R O L L B A C K to the save point if you are still in

INT306 – Database Management


the same transaction.

• Below is the result of the S E L E C T statement.


You can see that the U P DAT E was not
committed while the I N S E RT was committed

15
Exercise 1
• See Exercise 1

INT306 – Database Management


• Examine the code and, without running the transactions, answer the
following:

• What is the result of the implementing each of these transactions?

• Then run the transactions and check your answer.

16
Exercise 2
• For this exercise you need to:
1. Create a bank_account table

INT306 – Database Management


2. Create a transaction_history table
3. Insert two (2) rows into the bank account table.
a) The first row is your checking account, and
b) The second is your savings account
4. Make a transaction that:
a) Transfers $500 from the checking account to the savings account, and
b) Adds this transaction to the transaction history table

17
Transactions Related Phenomena
• Dirty Reads

INT306 – Database Management


• Non-Repeated Reads

• Phantom Reads

18
Transactions ISOLATION L E V E L s
Isolation Level Permitted actions

INT306 – Database Management


Least Read Uncommitted Allows Dirty Reads, Non-Repeated Reads, and Phantom
isolated
Reads
Read Committed Allows Non-Repeated Reads, and Phantom Reads

Repeatable Reads Allows Phantom Reads

Most
isolated
Serializable Does not allow any of the three reads

The four isolation levels were defined in the S Q L standard.


However, not all the database implement them. And databases have different
mechanisms to implement them.

19
Practice transactions with different isolation level
• Implement the four scenarios in the Practice Transactions with Isolation
Level tutorial

INT306 – Database Management


20
Questions?

21 INT306 – Database Management

You might also like