Transactionsالمعاملات
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Transaction Concept
- Transaction State
- Concurrent Executions
- Serializability
أقسام الفصل
- 1Transaction conceptمفهوم المعاملة
- 2ACID propertiesخصائص ACID
- 3Transaction statesحالات المعاملة
- 4Concurrent executions and schedulesالتنفيذ المتزامن والجداول الزمنية
- 5Serializability and conflicting instructionsقابلية التسلسل والتعليمات المتعارضة
- 6Precedence graph and testing for conflict serializabilityمخطط الأسبقية واختبار قابلية التسلسل
- 7Transaction definition in SQLتعريف المعاملة في SQL
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Transaction conceptمفهوم المعاملة
A set of logical operations to access and update the various data items of a database.
Transfer $50 from account A to account B
- 1
read(A)
- 2
A := A − 50
- 3
write(A)
- 4
read(B)
- 5
B := B + 50
- 6
write(B)
- Two main issues: failures of various kinds (hardware failures, system crashes) and concurrent execution of multiple transactions.
ACID propertiesخصائص ACID
| Property | Meaning (slides 4–8) | Responsible (slide 9) |
|---|---|---|
| Atomicity | All or nothing: the entire transaction takes place at once or doesn't happen at all | Transaction Manager |
| Consistency | From one consistent state to another (e.g., A + B stays 300) | Application programmer |
| Isolation | Transactions occur independently, without interference | Concurrency Control Manager |
| Durability | Completed updates persist despite software or hardware failures | Recovery Manager |
ACID in the $50 transferACID في مثال التحويل
ACID properties
ماذا يحدث في كل حالة؟
Atomicity
A = 200, B = 100. The transaction fails after step 3 (A = 150 written) and before step 6.
Money is "lost" and the database is inconsistent. **Updates of a partially executed transaction must not be reflected in the database** — "All or nothing". Abort: changes not visible; Commit: changes visible.
Consistency
What must hold before and after the transfer?
The total is maintained: **200 + 100 = 300** before, **150 + 150 = 300** after — from one consistent state to another.
Isolation
Between steps 3 and 6, T2 runs read(A), read(B), print(A+B).
T2 sees an **inconsistent database**: the sum A + B is less than it should be. Running transactions **serially** ensures isolation, but concurrency has significant benefits.
Durability
The user has been told the transfer is done, then the system crashes.
The updates **must persist** even with software or hardware failures — they are written to disk.
Transaction statesحالات المعاملة
The initial state; stays here while executing.
After the final statement has been executed.
After discovering that normal execution can no longer proceed.
After the transaction has been rolled back and the database restored to its state prior to the start. Then: restart (only if no internal logical error) or kill.
After successful completion.
- Diagram (slide 11): active → partially committed → committed; active → failed; partially committed → failed; failed → aborted.
Concurrent executionsالتنفيذ المتزامن
Advantages (slide 12)
- Increased processor and disk utilization, leading to better transaction throughput — e.g., one transaction uses the CPU while another reads from or writes to the disk.
- Reduced average response time: short transactions need not wait behind long ones.
Mechanisms to achieve isolation: control the interaction among concurrent transactions so they do not destroy the consistency of the database.
Schedulesالجداول الزمنية
A sequence of instructions specifying the chronological order in which instructions of concurrent transactions are executed: it must consist of all instructions of those transactions and preserve the order within each transaction. A completed transaction ends with commit; a failed one with abort.
Schedule 1 (serial)
- T1: read(A), A := A − 50, write(A), read(B), B := B + 50, write(B), commit
- then T2: read(A), temp := A * 0.1, A := A − temp, write(A), read(B), B := B + temp, write(B), commit
Schedule 3 (not serial)
- T1: read(A), A := A − 50, write(A)
- T2: read(A), temp := A * 0.1, A := A − temp, write(A)
- T1: read(B), B := B + 50, write(B), commit
- T2: read(B), B := B + temp, write(B), commit
- Equivalent to Schedule 1
- Schedule 2 is serial with T2 followed by T1. In schedules 1, 2 and 3, the sum A + B is preserved.
Serializability and conflicting instructionsقابلية التسلسل والتعارض
A (possibly concurrent) schedule that is equivalent to a serial schedule (each transaction preserves consistency, so serial execution does). Forms: conflict serializability and view serializability.
Two operations conflict if ALL hold (slide 18)
- They belong to different transactions
- They operate on the same data item
- At least one of them is a write operation
S can be transformed into S' by a series of swaps of non-conflicting instructions → S and S' are conflict equivalent; S is conflict serializable if it is conflict equivalent to a serial schedule.
Conflict serializable or not?أمثلة قابلية التسلسل
Serializability and conflicting instructions
Schedule 3
- Can be transformed into Schedule 6 (serial, T2 follows T1) by swaps of non-conflicting instructions
- Conflict serializable
T3 / T4 (slide 21)
- T3: read(Q) … write(Q); T4: write(Q) in between
- Cannot swap to obtain < T3, T4 > or < T4, T3 >
- Not conflict serializable
Precedence graphمخطط الأسبقية
A directed graph whose vertices are the transactions; an arc from Ti to Tj if the two transactions conflict and Ti accessed the data item earlier; the arc may be labelled with the item.
A schedule is conflict serializable if and only if its precedence graph is acyclic. The serializability order is then a topological sorting of the graph.
- Slide 22's example has arcs T1 → T2 and T2 → T1 — a cycle.
- Cycle detection takes order n² time (n vertices); better algorithms take order n + e (e edges).
Transaction definition in SQLالمعاملة في SQL
- In SQL, a transaction begins implicitly.
- Commit work commits the current transaction and begins a new one; Rollback work causes the current transaction to abort.
- By default, every SQL statement also commits implicitly if it executes successfully; this can be turned off, e.g., JDBC connection.setAutoCommit(false).
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Transaction conceptمفهوم المعاملة
- ACID propertiesخصائص ACID
- Transaction statesحالات المعاملة
- Concurrent executions and schedulesالتنفيذ المتزامن والجداول الزمنية
- Serializability and conflicting instructionsقابلية التسلسل والتعليمات المتعارضة
- Precedence graph and testing for conflict serializabilityمخطط الأسبقية واختبار قابلية التسلسل
- Transaction definition in SQLتعريف المعاملة في SQL