2. Transactional Systems
In this lecture
• Introduction to the concept of transaction
• Properties of a transaction
• Modules of the DBMS responsible of guaranteeing such
properties
2 / 20
3. Transactional Systems
Definition of Transaction
• An atomic logical unit of work
– containing one or more SQL commands
• Example: money transfer between two bank accounts
– Decrement first account
– Increment second account
• The effects of all the SQL statements in a transaction can be:
– either all committed (applied – permanently - to the database)
– or all rolled back (undone from the database)
3 / 20
4. Transactional Systems
Definition of Transaction
• Each transaction is encapsulated within two commands:
– begin transaction (bot)
– end transaction (eot)
• Within a transaction one of the commands below is executed
(exactly once):
– commit work (commit)
– rollback work (abort)
• Transactional System: a system capable of providing the
definition and execution of transactions on behalf of multiple,
concurrent applications.
4 / 20
6. Transactional Systems
Transaction:Example with Alternative
begin transaction ;
update Account
set Balance = Balance + 10 where AccNum = 12202;
update Account
set Balance = Balance − 10 where AccNum = 42177;
select Balance into A from Account
where AccNum = 42177;
i f (A>=0) then commit work
else rollback work ;
end transaction ;
6 / 20
7. Transactional Systems
Transactions in JDBC
• Transaction mode is chosen via a method defined in the
Connection interface
– setAutoCommit(boolean autoCommit)
• con.setAutoCommit(true)
– (Default) ”autocommit”: every single operation is a transaction
• con.setAutoCommit(false)
– Transactions are handled in the program
con.commit()
con.rollback()
• There is no start transaction
7 / 20
9. Transactional Systems
ACID Properties of Transactions
• A transaction is a unit of work enjoying the following properties:
– Atomicity
– Consistency
– Isolation
– Durability
9 / 20
10. Transactional Systems
Atomicity
• A transaction is an atomic transformation from the initial state
to the final state
• Possible behaviors:
1 Commit work: SUCCESS
2 Rollback work or error prior to commit: UNDO
3 Fault after commit: REDO
10 / 20
11. Transactional Systems
Consistency
• The transaction satisfies the integrity constraints
– Integrity constraints provide a mechanism for ensuring that data
conforms to guidelines specified by the database administrator
– The most common types of constraints include:
UNIQUE constraints
NOT NULL constraints
FOREIGN KEY constraints
• Consequence:
– If the initial state is consistent
– Then the final state is also consistent
11 / 20
12. Transactional Systems
Example - Bank transfer
• Assume that after removing 10 from the first account, the
transaction is unable to update the second account.
• Atomicity requires that
– both parts of the transaction complete
– or neither complete.
ù
• Also consistency is violated (with a constraint on the balance
between income/outcome)
12 / 20
13. Transactional Systems
Isolation
• A transaction is not affected by the behavior of other,
concurrent transactions
• Consequence:
– Its intermediate states are not exposed
13 / 20
14. Transactional Systems
Example
• Consider two transactions
1 T1 transfers 10 from Acc1 to Acc2
2 T2 transfers 10 from Acc2 to Acc1
• Combined, there are four actions:
1 T1 subtracts 10 from Acc1 (1)
2 T1 adds 10 to Acc2 (2)
3 T2 subtracts 10 from Acc2 (3)
4 T2 adds 10 to Acc1 (4)
• In this order: Isolation is guaranteed
– If T1 fails on operation (2) and is executed atomically, (1) is undone→
T2 sees correct data
• If execution is interleaved (1)(3)(4)(2) and T1 fails on operation
(2), then after (1) Acc1 contains (Initial value-10); T2 performs
its operations and adds 10 to (Initial value-10); T1 fails on
operation (2) and is undone, but T2 has operated on non-valid
values.
14 / 20
15. Transactional Systems
Durability
• The effect of a transaction that has successfully committed will
last forever
– Independently of any system fault
15 / 20
16. Transactional Systems
Transaction Properties and Mechanisms
• Atomicity
– Abort-rollback-restart
– Recovery management and Commit protocols
• Consistency
– Integrity checking of the DBMS → at query execution time
• Isolation
– Concurrency control
• Durability
– Recovery management
16 / 20
18. Transactional Systems
Transactions and DBMS modules
• Atomicity and durability
– Reliability Manager
• Isolation
– Concurrency Control System
• Consistency
– Integrity Control System at query execution time
18 / 20
19. Transactional Systems
Next Topics...
• Concurrency Control (Isolation)
– Theory
– DBMS adopted method
• Reliability Control (Atomicity and Durability)
– Commit protocols and 2PC
– Logging and recovery on a single DBMS
19 / 20