A transaction is a set of operations that perform a single logical task in a database.
Imagine you're using a banking app to transfer βΉ1000 from Account A to Account B.
This task involves:
1. BEGIN TRANSACTION 2. Deduct βΉ1000 from Account A 3. Add βΉ1000 to Account B 4. COMMIT TRANSACTION
This is one transaction. If any step fails, the whole operation should fail.
Banking transactions require all operations to succeed or fail together
In a transaction, an operation is a single step like reading, writing, updating, or committing data in the database.
Together, multiple operations complete the logical task.
Transaction T1: 1. BEGIN TRANSACTION 2. READ(A) -- Get balance of Account A 3. A = A - 1000 -- Deduct βΉ1000 4. WRITE(A) -- Save new balance of A 5. READ(B) -- Get balance of Account B 6. B = B + 1000 -- Add βΉ1000 7. WRITE(B) -- Save new balance of B 8. COMMIT -- Finalize the transaction
Each READ and WRITE is an operation. Together, they make one complete transaction.
ACID properties are a set of rules that ensure database transactions are processed reliably, even in case of failures or concurrent access.
All operations in a transaction are treated as a single unit β either all succeed or none at all.
When transferring βΉ1000 from Account A to B, if deduction from A fails, the amount must not be added to B.
BEGIN TRANSACTION Deduct βΉ1000 from A Add βΉ1000 to B COMMIT -- If any step fails, rollback both
The database must always remain in a valid state before and after the transaction.
Total money in the system should be the same before and after a transfer.
Before Transaction: A = βΉ5000, B = βΉ3000 β Total = βΉ8000 After Transaction (βΉ1000 A to B): A = βΉ4000, B = βΉ4000 β Total = βΉ8000 β
Each transaction runs independently. Partial results of one transaction are not visible to others.
Two users transferring money simultaneously wonβt interfere with each otherβs balances.
User 1: Transfer βΉ500 A β B User 2: Transfer βΉ1000 C β D -- Isolation ensures these run safely in parallel without data conflict
Once a transaction is committed, the changes are permanent β even if the system crashes.
If a user completes a payment and the app crashes, the payment still goes through.
COMMIT -- Data saved in log or backup -- After power failure: data is restored from logs β
ACID properties ensure database reliability, correctness, and integrity during all transactions.
When multiple transactions run at the same time, it's called concurrent execution.
Imagine many users are booking movie tickets online at the same time. The system processes many transactions in parallel, but must ensure that no one gets the same seat.
Transaction 1: User A books Seat 5A Transaction 2: User B books Seat 7C Transaction 3: User C books Seat 5A β Should be blocked!
Multiple users booking tickets simultaneously - perfect example of concurrency
In a DBMS, a transaction moves through several states to ensure safe and correct execution. The diagram below shows the complete life cycle:
Visual flow of transaction states including commit, abort, and rollback.
State | Meaning | Transition | Example |
---|---|---|---|
Active | Transaction is currently executing (doing read/write) | β Partially Committed (if successful) or β Failed (if error) | User clicks "Recharge βΉ100" |
Partially Committed | Final step executed; about to save changes | β Committed (if successful) or β Failed (if error) | Recharge sent to operator, waiting for response |
Committed | All changes made permanent in the database | β Terminated | "Recharge Successful" message shown |
Failed | Error occurred during transaction | β Aborted | Network failure or recharge error |
Aborted | Transaction rolled back (undone) | β Terminated | Recharge failed and money returned |
Terminated | Transaction completed successfully or aborted safely | End of transaction | Recharge is either complete or failed, system ready for new transaction |
<pre>
formatRecharge Transaction Flow 1. BEGIN TRANSACTION 2. READ(wallet_balance) 3. wallet_balance = wallet_balance - βΉ100 4. WRITE(wallet_balance) 5. Send recharge request If success: 6. COMMIT β Move to COMMITTED β TERMINATED If failure: 6. FAIL β Move to FAILED β ROLLBACK β ABORTED β TERMINATED
A Schedule is the order in which operations (like read, write) from multiple transactions are executed.
T1: read(A) β write(A) β commit T2: read(B) β write(B) β commit
One transaction at a time
T1: read(A) T2: read(B) T1: write(A) T2: write(B)
Multiple transactions interleaved
Database scheduling manages the order of transaction execution
Jab multiple transactions ek hi samay par execute karte hain bina proper control ke, to data galat ho sakta hai. Yeh problem ko kehte hain: Concurrency Issues.
Definition: Jab ek transaction (T2) kisi doosre transaction (T1) ke uncommitted data ko read kar leta hai, to agar T1 fail ho jaye, T2 galat data ke basis pe kaam kar chuka hota hai.
ποΈ Real-World Example: Aapka friend T1 aapke account se βΉ500 nikalta hai, aur aap T2 me balance check karke βΉ1500 dekh lete ho. Par T1 fail ho jata hai aur rollback ho jata hai. Aapne jo balance dekha wo galat tha.
Image: Timeline of Dirty Read Problem between T1 and T2
Definition: Jab do transactions same data ko read karte hain aur update karte hain bina synchronization ke, to ek transaction ka update dusre ke update se chhup jata hai. Isse kehte hain Lost Update.
ποΈ Real-World Example: Alice aur Bob ek product ki quantity update kar rahe hain. Dono ne same original value (7) read ki. Par Bob ne jab last me commit kiya, to Alice ka update (quantity = 6) chhup gaya aur final value ban gayi quantity = 10.
Image: Lost Update between Alice and Bob
Definition: Jab ek transaction data ko read karke uspar operation karta hai, aur beech me doosra transaction usi data ko overwrite kar deta hai, to pehle transaction ka update dusre ko ignore kar deta hai.
ποΈ Real-World Example: Do log ek hi account balance X par kaam kar rahe hain. A ne X ko +15 kiya, par write nahi kiya tha. B ne X ko read kiya, -25 kiya, aur write bhi kar diya. Jab A ne apna write kiya, to B ka change chhup gaya.
Image: Read-Write Conflict β Final value of X becomes 115, Bβs update is lost.
Jab multiple transactions ek hi data ke sath kaam karte hain, to hume dekhna padta hai ki koi transaction galat data pe commit to nahi kar raha. Iske liye schedule ka type define kiya jata hai.
Definition: Ek transaction T2 tabhi commit kare jab usne jo data read kiya hai, wo T1 pehle se commit kar chuka ho.
ποΈ Real-World Example: T1 updates salary and commits, fir T2 reads that salary and commits. Agar T1 fail hota to T2 ne kuch commit nahi kiya hota β so safe.
Definition: Ek transaction T2 read kar leta hai uncommitted data (dirty read), aur commit bhi kar deta hai. Agar T1 fail hua to rollback impossible ho jata hai.
ποΈ Real-World Example: Loan approve hua salary βΉ80,000 ke basis pe, par later pata chala T1 ne wo salary rollback ki thi.
When multiple transactions interact with shared data, their commit order and read/write timing decides if the system can recover properly or not.
Definition: Ek transaction tabhi kisi aur ka data read karta hai jab wo pehle commit ho chuka ho.
π Advantage: No cascading rollback, safe, and consistent.
All transactions (T2, T3) only read after T1 commits β No cascading rollback possible.
Definition: Jab ek transaction dusre ka uncommitted data read karta hai, to agar pehla transaction fail ho gaya, to baaki sab rollback karne padte hain.
π Problem: Chain of rollbacks = cascading abort
T2, T3, T4 all read A before T1 commits β If T1 fails, they must rollback.
A deadlock occurs when two or more transactions wait indefinitely for each other to release resources.
Transaction 1: - Locks Account A - Waits for Account B Transaction 2: - Locks Account B - Waits for Account A Result: Both transactions wait forever!
Perfect example of a Transaction with ACID properties
Demonstrates Atomicity - complete success or complete failure
Shows Isolation - two users can't book same seat
Illustrates Durability - refund processed even after crash
Example of Dirty Read - seeing incomplete transaction
Shows Lost Update - two edits overwrite each other
Concept | Key Point | Real-World Example |
---|---|---|
Transaction | Logical unit of work | Bank transfer, online purchase |
Atomicity | All or nothing | Payment succeeds completely or fails completely |
Consistency | Database remains valid | Account balances always correct |
Isolation | Transactions don't interfere | Multiple users can't book same seat |
Durability | Changes persist | Transaction survives system crash |
Concurrency | Multiple transactions simultaneously | Many users using ATM at same time |
Deadlock | Circular wait condition | Two users waiting for each other's resources |