27
Advanced Database- Dr. Arasteh 1 Advanced Database Bahman Arasteh (Ph.D, Software Engineering) Department of Software Engineering, Azad University of Tabriz E-mail: [email protected]

Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Embed Size (px)

Citation preview

Page 1: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh1

Advanced Database

Bahman Arasteh (Ph.D, Software Engineering)

Department of Software Engineering,Azad University of Tabriz

E-mail: [email protected]

Page 2: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh2

Textbooks

• "Database System Concepts", Sixth Edition, Abraham Silberschatz, Henry F. Korth, S. Sudarshan

Page 3: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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%

Page 4: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh4

Today’s Goals

Transaction Concept

Page 5: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh5

ContentsTransaction Concept:

Page 6: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 7: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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)

Page 8: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

– …

Page 9: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 10: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 11: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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

Page 12: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 13: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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

Page 14: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 15: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 16: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh16

Transaction State

Page 17: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 18: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh18

Transaction Control

• The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.

Page 19: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh19

Transaction Control

Example: Consider the CUSTOMERS table and the following transaction:

Page 20: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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:

Page 21: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 22: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh22

Transaction Control

• Example: Consider the CUSTOMERS table and the following transaction

Page 23: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh23

Transaction Control

• As a result, delete operation would not impact the table and SELECT statement would produce the following result:

Page 24: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

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.

Page 25: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh25

Transaction Implimatation

• The transactions can also be handled at SQL level.

Page 26: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh26

Transaction Implementation

• The transactions can also be handled in .NET environment using Transactionscope API. .

Page 27: Advanced Database- Dr. Arasteh1 Advanced Database Bahman Arasteh ( Ph.D, Software Engineering ) Department of Software Engineering, Azad University of

Advanced Database- Dr. Arasteh27

End