TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING...

28
TRANSACTION TRANSACTION PROCESSING TECHNIQUES PROCESSING TECHNIQUES BY BY SON NGUYEN SON NGUYEN VIJAY RAO VIJAY RAO

Transcript of TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING...

Page 1: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

TRANSACTION TRANSACTION PROCESSING TECHNIQUESPROCESSING TECHNIQUES

BYBYSON NGUYENSON NGUYENVIJAY RAOVIJAY RAO

Page 2: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

2

Presentation OverviewPresentation Overview TransactionsTransactions

○ Definition○ Definition○ ACIDS Properties○ ACIDS Properties○ Isolation levels○ Isolation levels○ Concurrency & failures issues○ Concurrency & failures issues○ Forms of transaction processing○ Forms of transaction processing○ Example: Airline Reservation System○ Example: Airline Reservation System○ Two types of transaction: Simple and Concurrent○ Two types of transaction: Simple and Concurrent○ Concurrency controls: Optimistic, Pessimistic and 2 Phase Lock○ Concurrency controls: Optimistic, Pessimistic and 2 Phase Lockinging

Failures RecoveryFailures Recovery○ Types of failures○ Types of failures○ Single site recovery: Abort recovery○ Single site recovery: Abort recovery

Distributed TransactionsDistributed Transactions○ Two phase commit○ Two phase commit

SummarySummary

Page 3: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

3

What are Transactions?What are Transactions?►► Can be defined as an indivisible unit of work Can be defined as an indivisible unit of work

comprised of several operations, all or none of comprised of several operations, all or none of

which must be performedwhich must be performed

►► Has ACID propertiesHas ACID properties

◊ ◊ AtomicityAtomicity

◊ ◊ ConsistencyConsistency

◊ ◊ IsolationIsolation

◊ ◊ DurabilityDurability

Page 4: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

4

ACID Properties (cont)ACID Properties (cont)►►AtomicityAtomicity

Operations are treated as a single unit Operations are treated as a single unit (indivisible) (indivisible) one which will either complete fully or not at allone which will either complete fully or not at all

►►ConsistencyConsistencyMust maintain consistency of data before and Must maintain consistency of data before and after a after a transactiontransaction

►►IsolationIsolationTransactions should not affect other transactionsTransactions should not affect other transactions

►►DurabilityDurabilityRequires result of a committed transaction be made Requires result of a committed transaction be made permanentpermanent

Page 5: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

5

Isolation levels of TransactionIsolation levels of Transaction

►► ReadUncommittedReadUncommitted/Dirty Read/Dirty ReadUpdated data not yet committed by a transaction may be Updated data not yet committed by a transaction may be read by other transactionsread by other transactions

►► ReadCommittedReadCommittedOnly committed data can be read by other transactionsOnly committed data can be read by other transactions

►► RepeatableReadRepeatableReadDirty reads and Dirty reads and nonrepeatablenonrepeatable reads cannot occurreads cannot occur

►► SerializableSerializableMost restrictive isolation level. Exclusive access to Most restrictive isolation level. Exclusive access to

read/write dataread/write data

Page 6: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

6

Why are Transactions needed?Why are Transactions needed?To resolve:To resolve:►►Concurrency issuesConcurrency issues

◊ ◊ Race conditionRace condition

◊ ◊ Inconsistent updatesInconsistent updates

►►Failures issuesFailures issues◊ ◊ System failuresSystem failures

◊ ◊ Communication failuresCommunication failures

Page 7: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

7

Forms of transaction processingForms of transaction processing►►Sequential specificationSequential specification

◊ ◊ Transactions are executed in orderTransactions are executed in order

◊ ◊ Do not allow concurrencyDo not allow concurrency

◊ ◊ Allow failuresAllow failures

►►Concurrent specificationConcurrent specification◊ ◊ Allow transactions to happen concurrentlyAllow transactions to happen concurrently

◊ ◊ Record the history and the result values of operations Record the history and the result values of operations executed by each transaction executed by each transaction

◊ ◊ Use transaction identifiers to distinguish concurrent Use transaction identifiers to distinguish concurrent transaction’s operationstransaction’s operations

◊ ◊ Allow failuresAllow failures

Page 8: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

8

Applications that use transactionsApplications that use transactions►►Banking systemsBanking systems►►Airline reservation systemAirline reservation system►►Online shoppingOnline shopping

Page 9: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

9

Airline Reservation example (cont)Airline Reservation example (cont)

►►What happens if the reservation system What happens if the reservation system tries to book the same seat concurrently?tries to book the same seat concurrently?◊ ◊ Two passengers might end up with the same seat Two passengers might end up with the same seat (concurrency issue)(concurrency issue)

►►Invalid booking also happens if:Invalid booking also happens if:◊ ◊ Reservation system reads data from one of its Reservation system reads data from one of its replicated database that has not been updated due to a replicated database that has not been updated due to a network failurenetwork failure

Page 10: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

10

Simple TransactionSimple Transaction

►►Turn Turn AutoCommitAutoCommit offoff►►Execute a bunch of inserts and updatesExecute a bunch of inserts and updates►►CommitCommit►►If error then rollbackIf error then rollback

What happens if multiple threads are What happens if multiple threads are running?running?

Page 11: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

11

Concurrent TransactionsConcurrent Transactions

►► Need a robust transaction processing application Need a robust transaction processing application server with services to manage all the issues with server with services to manage all the issues with concurrent processingconcurrent processing

►► JTA (Java transaction API) provides the API for JTA (Java transaction API) provides the API for managing transactionsmanaging transactions

►► Application servers such as BEA Application servers such as BEA WeblogicWeblogic and IBM and IBM WebsphereWebsphere provide a robust implementation for provide a robust implementation for transaction managementtransaction management

Page 12: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

12

Concurrency ControlConcurrency Control►► Optimistic concurrencyOptimistic concurrency

◊ ◊ Operations are simply carried outOperations are simply carried out

◊ ◊ Synchronization takes place at the end of a transactionSynchronization takes place at the end of a transaction

►► Pessimistic concurrencyPessimistic concurrency◊ ◊ Use Murphy’s law “If something can go wrong, it will”Use Murphy’s law “If something can go wrong, it will”

◊ ◊ Operations are synchronized before they are carried outOperations are synchronized before they are carried out

►► 2 Phase Locking2 Phase Locking◊ ◊ An algorithm to keep transactions in syncAn algorithm to keep transactions in sync

◊ ◊ Acquire/Release locks conceptAcquire/Release locks concept

◊ ◊ Growing phase and Shrinking phaseGrowing phase and Shrinking phase

◊ ◊ Locks must be strong to ensure only one transaction can write atLocks must be strong to ensure only one transaction can write at a a time. Read is not allowed while writing.time. Read is not allowed while writing.

Page 13: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

13

Concurrency Control Concurrency Control –– 2 Phase 2 Phase LockingLocking

Page 14: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

14

Concurrency Control Concurrency Control –– 2 Phase 2 Phase Locking (cont)Locking (cont)

►►Problems with 2 Phase lockingProblems with 2 Phase locking

◊ ◊ DeadlocksDeadlocks

►►Solutions?Solutions?

Page 15: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

15

Single Site RecoverySingle Site Recovery►►Possible types of failures:Possible types of failures:

◊ ◊ Transaction abort Transaction abort

◊ ◊ System crashSystem crash

◊ ◊ Media failuresMedia failures

◊ ◊ CatastropheCatastrophe

►►Possible ways of recoveryPossible ways of recovery◊ ◊ Abort recovery Abort recovery

◊ ◊ Crash recovery Crash recovery

Page 16: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

16

Single Site Recovery Single Site Recovery -- Abort Abort RecoveryRecovery

Two methods to recover from transactionTwo methods to recover from transactionabort:abort:►►UpdateUpdate--inin--placeplace

◊ ◊ Update the database as a transaction runs and undoing Update the database as a transaction runs and undoing updates if aborts.updates if aborts.

◊ ◊ Actions implementation:Actions implementation:

□ Update: record old value in an undo log and update□ Update: record old value in an undo log and update□ Read: simply just read the desired data□ Read: simply just read the desired data□ Commit: discard transaction’s undo log□ Commit: discard transaction’s undo log□ Abort: use undo log to back out updates□ Abort: use undo log to back out updates

Page 17: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

17

Single Site Recovery Single Site Recovery -- Abort Abort Recovery (cont)Recovery (cont)

►►Deferred updateDeferred update◊ ◊ Saving a transaction’s updates while it runs and using Saving a transaction’s updates while it runs and using the saved updates to update the database when the the saved updates to update the database when the transaction commits.transaction commits.

◊ ◊ Actions implementation:Actions implementation:

□ Update: record updated value in an redo log □ Update: record updated value in an redo log □ Read: combine the redo log and the database □ Read: combine the redo log and the database □ Commit: update the database by applying redo log □ Commit: update the database by applying redo log □ Abort: discard the transaction’s redo log□ Abort: discard the transaction’s redo log

►►Which method is better?Which method is better?

Page 18: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

18

Single Site Recovery Single Site Recovery –– Crash Crash RecoveryRecovery

►►Use log to record updates, commits, and Use log to record updates, commits, and abort statusabort status

►►Use log information to tell which updates Use log information to tell which updates belong on disk and which do notbelong on disk and which do not

►►Abort transaction that doesn’t have a Abort transaction that doesn’t have a commit record in the log.commit record in the log.

Page 19: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

19

Distributed TransactionsDistributed Transactions

►► Distributed Transactions include one or more Distributed Transactions include one or more statements that individually or as a group update statements that individually or as a group update data at two distinct nodes in a distributed data at two distinct nodes in a distributed environment.environment.

►► Example of distributed transactions in OracleExample of distributed transactions in OracleUPDATE [email protected] SET loc = 'REDWOOD UPDATE [email protected] SET loc = 'REDWOOD

SHORES' WHERE deptno = 10; SHORES' WHERE deptno = 10; UPDATE scott.emp SET deptno = 11 WHERE deptno = 10;UPDATE scott.emp SET deptno = 11 WHERE deptno = 10;UPDATE [email protected] SET room = 1225 UPDATE [email protected] SET room = 1225

WHERE room = 1163; WHERE room = 1163; COMMIT; COMMIT;

Page 20: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

20

Distributed Transactions (Contd.)Distributed Transactions (Contd.)

Page 21: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

21

Distributed Transactions Distributed Transactions –– 2 Phase 2 Phase CommitCommit

►►A distributed transaction involves altering A distributed transaction involves altering data across multiple databases.data across multiple databases.

►►Databases can be geographically located Databases can be geographically located like database 1 in SFO and 2 in NYClike database 1 in SFO and 2 in NYC

►►This protocol ensures integrity of data This protocol ensures integrity of data ►►This consists of 2 phases managed by a This consists of 2 phases managed by a

global coglobal co--ordinatorordinator Prepare PhasePrepare Phase Commit PhaseCommit Phase

Page 22: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

22

2 Phase Commit (Contd.)2 Phase Commit (Contd.)

►► The global coordinator sends a message to check The global coordinator sends a message to check for preparedness to commit by all participantsfor preparedness to commit by all participants

►► Once all participants have acknowledged it issues Once all participants have acknowledged it issues a Commit or rollback.a Commit or rollback.

Page 23: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

23

2 Phase Commit 2 Phase Commit -- FailureFailure

►► Failure during Prepare PhaseFailure during Prepare Phase

►► Failure during Commit PhaseFailure during Commit Phase

Page 24: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

24

Real Life ExamplesReal Life Examples

►► Transactions in J2EETransactions in J2EEJava Transaction API (JTA)Java Transaction API (JTA)

►► Enterprise Java BeansEnterprise Java BeansRobust and scalable component in J2EE architecture Robust and scalable component in J2EE architecture that can participate in transactions.that can participate in transactions.

►► Transaction Attributes for EJBTransaction Attributes for EJBDeclarative Transaction ManagementDeclarative Transaction ManagementRequired, Required, RequiresNewRequiresNew, Mandatory, Supports, , Mandatory, Supports, NotSupportedNotSupported and Neverand Never

Page 25: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

25

Nested TransactionsNested Transactions

►►Transactions within transactionsTransactions within transactions►►SubSub--transactions can commit but outer transactions can commit but outer

transaction could rollback…?transaction could rollback…?►►Complex Issues!Complex Issues!►►Not supported by J2EE and hence JDBC!Not supported by J2EE and hence JDBC!►►Only flat transactions are supportedOnly flat transactions are supported

Page 26: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

26

SummarySummary

►►Transactions are importantTransactions are important►►Transactions in a distributed environment Transactions in a distributed environment

are much more complex compare to a single are much more complex compare to a single site transactionssite transactions

►►Must handle transactions appropriatelyMust handle transactions appropriately►►Failed to recovery a transaction might cause Failed to recovery a transaction might cause

a lot of troublesa lot of troubles

Page 27: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

27

BibliographyBibliography►► http://www.javaworld.com/javaworld/jwhttp://www.javaworld.com/javaworld/jw--0707--2000/jw2000/jw--07140714--

transaction.htmltransaction.html►► http://www.mssqlcity.com/Articles/General/TIL.htmlhttp://www.mssqlcity.com/Articles/General/TIL.html►► http://www.cse.scu.edu/~jholliday/transproc.htmlhttp://www.cse.scu.edu/~jholliday/transproc.html►► http://www.ietf.org/rfc/rfc2371.txthttp://www.ietf.org/rfc/rfc2371.txt►► http://www.cs.umb.edu/cs634/ora9idocs/server.920/a9652http://www.cs.umb.edu/cs634/ora9idocs/server.920/a9652

1/ds_txns.htm1/ds_txns.htm►► http://www.cogs.susx.ac.uk/users/ianw/teach/disthttp://www.cogs.susx.ac.uk/users/ianw/teach/dist--sys/dssys/ds--

node246.htmlnode246.html►► Distributed Systems: Principles and Paradigms by Distributed Systems: Principles and Paradigms by

TanenbaumTanenbaum and Van Steenand Van Steen

Page 28: TRANSACTION PROCESSING TECHNIQUESstamp/CS249/projects/Chapter13.pdf · TRANSACTION PROCESSING TECHNIQUES BY ... A distributed transaction involves altering ... Failed to recovery

28

Q&AQ&A

►►Q&AQ&A