advanced database- dr. arasteh1 advanced database bahman arasteh ( ph.d, software engineering )...
TRANSCRIPT
Advanced Database- Dr. Arasteh1
Advanced Database
Bahman Arasteh (Ph.D, Software Engineering)
Department of Software Engineering,Azad University of Tabriz
E-mail: [email protected]
Advanced Database- Dr. Arasteh2
Textbooks
• "Database System Concepts", Sixth Edition, Abraham Silberschatz, Henry F. Korth, S. Sudarshan
Advanced Database- Dr. Arasteh3
Grading Policy
• Assignments: 20%
• Research Project: 20%
• Final Examination:60%
• Extra Marks:– Draft of a Paper: 5%– Acceptance of a Paper: 10%
Advanced Database- Dr. Arasteh4
Today’s Goals
Transaction Concept
Advanced Database- Dr. Arasteh5
ContentsTransaction Concept:
Advanced Database- Dr. Arasteh6
Transaction Concept
• Definition:
– A transaction is a unit of program execution that accesses and possibly updates various data items.
– A transaction comprises a unit of work performed within a database management system.
– A transaction is a logical unit that is independently executed for data retrieval or updates.
– A series of data manipulation statements that must either fully complete or fully fail, leaving the database in a consistent state.
Advanced Database- Dr. Arasteh7
Transaction Concept
• E.g. Transaction to 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)
Advanced Database- Dr. Arasteh8
Transaction Concept
• Characteristics:
– A transaction is a program or part of a program.
– A transaction includes start and end points.
– A transaction has to either happen in full, or not at all.
– …
Advanced Database- Dr. Arasteh9
Transaction Concept
• Example:• You need to transfer 100 dollar from account A to account B. You
can either do:
1. accountA -= 100;
2. accountB += 100; or
1. accountB += 100;
2. accountA -= 100; • If something goes wrong between the first and the second operation in the
pair you have a problem:
– either 100 dollar have disappeared or they have appeared out of nowhere.
Advanced Database- Dr. Arasteh10
Transaction Concept
• A transaction is a mechanism that allows you to mark a group of operations and execute them in such a way that either they all execute (commit) or the system state will be as if they have not started to execute at all (rollback).
1.beginTransaction;
2.accountB += 100;
3.accountA -= 100;
4.commitTransaction;
• This transaction will either transfer 100 dollar or leave both account in the initial state.
Advanced Database- Dr. Arasteh11
Transaction Properties
– A transaction is a program or part of a program.
– A transaction includes start and end points.
– A transaction has to either happen in full, or not at all.
– Transactions are completed by COMMIT or ROLLBACK SQL statements.
– The ACID acronym defines the properties of a database transaction.
• ACID = Atomicity, Consistency, Isolation, Durability
Advanced Database- Dr. Arasteh12
Transaction Properties
To preserve the integrity of data the database system must ensure:•Atomicity. Either all operations of the transaction are properly reflected in the database or none are.•Consistency. Execution of a transaction in isolation preserves the consistency of the database.•Isolation. Although multiple transactions may execute concurrently, each transaction must be unaware of other concurrently executing transactions. Intermediate transaction results must be hidden from other concurrently executed transactions.
– That is, for every pair of transactions Ti and Tj, it appears to Ti that either Tj, finished execution before Ti started, or Tj started execution after Ti finished.
•Durability. After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures.
Advanced Database- Dr. Arasteh13
Transaction Properties• Transaction to transfer $50 from account A to account B:
1. read(A)2. A := A – 503. write(A)4. read(B)5. B := B + 506. write(B)
• Consistency requirement in above example:– the sum of A and B is unchanged by the execution of the transaction
• In general, consistency requirements include • Explicitly specified integrity constraints such as primary keys and foreign
keys• Implicit integrity constraints
– e.g. sum of balances of all accounts, minus sum of loan amounts must equal value of cash-in-hand
– A transaction must see a consistent database.– During transaction execution the database may be temporarily inconsistent.– When the transaction completes successfully the database must be consistent
• Erroneous transaction logic can lead to inconsistency
Advanced Database- Dr. Arasteh14
Transaction Properties
• Isolation requirement — if between steps 3 and 6, another transaction T2 is allowed to access the partially updated database, it will see an inconsistent database (the sum A + B will be less than it should be).
–
T1 T21.read(A)
2. A := A – 503. write(A)
read(A), read(B), print(A+B)4. read(B)5. B := B + 506. write(B
• Isolation can be ensured trivially by running transactions serially– that is, one after the other.
• However, executing multiple transactions concurrently has significant benefits, as we will see later.
Advanced Database- Dr. Arasteh15
Transaction State
• Active – the initial state; the transaction stays in this state while it is executing
• Partially committed – after the final statement has been executed.• Failed -- after the discovery that normal execution can no longer
proceed.• Aborted – after the transaction has been rolled back and the
database restored to its state prior to the start of the transaction. Two options after it has been aborted:– restart the transaction
• can be done only if no internal logical error– kill the transaction
• Committed – after successful completion.
Advanced Database- Dr. Arasteh16
Transaction State
Advanced Database- Dr. Arasteh17
Transaction Control
• There are following commands used to control transactions:– COMMIT: to save the changes.– ROLLBACK: to rollback the changes.– SAVEPOINT: creates points within groups of transactions in
which to ROLLBACK– SET TRANSACTION: Places a name on a transaction.
• Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only.
Advanced Database- Dr. Arasteh18
Transaction Control
• The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.
Advanced Database- Dr. Arasteh19
Transaction Control
Example: Consider the CUSTOMERS table and the following transaction:
Advanced Database- Dr. Arasteh20
Transaction Control
As a result, two rows from the table would be deleted and SELECT statement would produce the following result:
Advanced Database- Dr. Arasteh21
Transaction Control
• The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database.
• The ROLLBACK command can only be used to undo transactions.
Advanced Database- Dr. Arasteh22
Transaction Control
• Example: Consider the CUSTOMERS table and the following transaction
Advanced Database- Dr. Arasteh23
Transaction Control
• As a result, delete operation would not impact the table and SELECT statement would produce the following result:
Advanced Database- Dr. Arasteh24
Transaction Control
• The SAVEPOINT Command:– A SAVEPOINT is a point in a transaction when you can roll the
transaction back to a certain point without rolling back the entire transaction.
• The RELEASE SAVEPOINT Command:– The RELEASE SAVEPOINT command is used to remove a
SAVEPOINT that you have created.
• The SET TRANSACTION Command:– The SET TRANSACTION command can be used to initiate a
database transaction. – This command is used to specify characteristics for the
transaction that follows.
Advanced Database- Dr. Arasteh25
Transaction Implimatation
• The transactions can also be handled at SQL level.
Advanced Database- Dr. Arasteh26
Transaction Implementation
• The transactions can also be handled in .NET environment using Transactionscope API. .
Advanced Database- Dr. Arasteh27
End