34
Slide 1 Lectured by, Jesmin Akhter, Assistant professor, IIT, JU PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University

PMIT-6102 Advanced Database Systems

  • Upload
    wylie

  • View
    68

  • Download
    1

Embed Size (px)

DESCRIPTION

PMIT-6102 Advanced Database Systems. By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University. Lecture -09 Introduction to transaction Management. Outline. Introduction to transaction Management Transactions Termination Condition of Transactions Properties of Transaction. - PowerPoint PPT Presentation

Citation preview

Page 1: PMIT-6102 Advanced Database Systems

PMIT-6102Advanced Database Systems

By-Jesmin Akhter

Assistant Professor, IIT, Jahangirnagar University

Page 2: PMIT-6102 Advanced Database Systems

Lecture -09Introduction to transaction

Management

Page 3: PMIT-6102 Advanced Database Systems

Slide 3Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Outline Introduction to transaction

Management Transactions Termination Condition of Transactions Properties of Transaction

Page 4: PMIT-6102 Advanced Database Systems

Slide 4Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Transaction

A transaction is a collection of actions that make consistent transformations of system states while preserving system consistency.

concurrency transparencyfailure transparency

The concept of a transaction is used in database systems as a basic unit of consistent and reliable computing

Database in aconsistentstate

Database may betemporarily in aninconsistent stateduring execution

BeginTransaction

EndTransaction

Execution ofTransaction

Database in aconsistentstate

Fig: A transaction Model

Page 5: PMIT-6102 Advanced Database Systems

Slide 5Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Transaction

A database is in a consistent state if it obeys all of the consistency (integrity) constraints defined over it .

Transaction consistency refers to the actions of concurrent transactions remaining the database in a consistent state.

Reliability refers to both the resiliency of a system to various types of failures and its capability to recover from them.

A resilient system is tolerant of system failures and can continue to provide services even when failures occur.

A recoverable DBMS is one that can get to a consistent state (by moving back to a previous consistent state or forward to a new consistent state) following various types of failures.

Transaction management deals with the problems of always keeping the database in a consistent state even when concurrent accesses and failures occur.

Page 6: PMIT-6102 Advanced Database Systems

Slide 6Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Transaction Example – A Simple SQL Query

In general, a transaction is considered to be made up of a sequence of read and write operations on the database, together with computation steps.

In that sense, a transaction may be thought of as a program with embedded database access queries .

Another definition of a transaction is that it is a single execution of a program.

A single query can also be thought of as a program that can be posed as a transaction.

Example 10.1. Consider the following SQL query for increasing by 10% the budget of the CAD/CAM project.

UPDATE PROJSET BUDGET = BUDGET*1.1WHERE PNAME= "CAD/CAM"

Page 7: PMIT-6102 Advanced Database Systems

Slide 7Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Transaction Example – A Simple SQL Query

Query can be specified, using the embedded SQL notation, as a transaction by giving it a name (e.g., BUDGET UPDATE)

Transaction BUDGET_UPDATE

begin

EXEC SQL UPDATE PROJSET BUDGET = BUDGET1.1WHERE PNAME = “CAD/CAM”

end.

The Begin transaction and end statements delimit a transaction

Page 8: PMIT-6102 Advanced Database Systems

Slide 8Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Example Transaction – SQL Version

Consider an airline reservation example with the relations:FLIGHT(FNO, DATE, SRC, DEST, STSOLD, CAP)CUST(CNAME, ADDR, BAL)FC(FNO, DATE, CNAME,SPECIAL)

FNO is the flight number, DATE denotes the flight date, SRC and DEST indicate the source and destination

for the flight, STSOLD indicates the number of seats that have

been sold on that flight, CAP denotes the passenger capacity on the flight, CNAME indicates the customer name whose address

is stored in ADDR whose account balance is in BAL, and

SPECIAL corresponds to any special requests that the customer may have for a booking.

Page 9: PMIT-6102 Advanced Database Systems

Slide 9Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Example Transaction – SQL Version

A typical reservation application, where a travel agent enters the flight number, the date, and a customer name, and asks for a reservation.

Begin_transaction Reservationbegin

input(flight_no, date, customer_name); (1)

EXEC SQLUPDATEFLIGHT (2)SET STSOLD = STSOLD + 1WHERE FNO = flight_no AND DATE = date;

EXEC SQLINSERT (3)INTO FC(FNO, DATE, CNAME, SPECIAL);VALUES (flight_no, date, customer_name,

null);output(“reservation completed”)

(4)end .

Page 10: PMIT-6102 Advanced Database Systems

Slide 10Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

The first thing that the transaction does [line (1)], is to input the flight number, the date, and the customer name.

Line (2) updates the number of sold seats on the requested flight by one.

Line (3) inserts a tuple into the FC relation. Here we assume that the customer is an old one, so it is not necessary to have an insertion into the CUST relation, creating a record for the client.

The keyword null in line (3) indicates that the customer has no special requests on this flight.

Finally, line (4) reports the result of the transaction to the agent’s terminal.

Example Transaction – SQL Version

Page 11: PMIT-6102 Advanced Database Systems

Slide 11Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

A transaction always terminates, even when there are failures

If the transaction can complete its task successfully, we say that the transaction commits.

If, on the other hand, a transaction stops without completing its task, we say that it aborts.

Termination Condition of Transactions

Page 12: PMIT-6102 Advanced Database Systems

Slide 12Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

A transaction aborts itself because of a condition that would prevent it from completing its task successfully.

Additionally, the DBMS may abort a transaction due to, for example, deadlocks or other conditions.

When a transaction is aborted, its execution is stopped and all of its already executed actions are undone by returning the database to the state before their execution. This is also known as rollback.

Termination Condition of Transactions

Page 13: PMIT-6102 Advanced Database Systems

Slide 13Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

The importance of commit is twofold: The commit command signals to the DBMS that the

effects of that transaction should now be reflected in the database, thereby making it visible to other transactions that may access the same data items.

Second, the point at which a transaction is committed is a “point of no return.” The results of the committed transaction are now permanently stored in the database and cannot be undone.

Termination Condition of Transactions

Page 14: PMIT-6102 Advanced Database Systems

Slide 14Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

The reservation transaction of Example 10.2 has an implicit assumption about its termination.

It assumes that there will always be a free seat and does not take into consideration the fact that the transaction may fail due to lack of seats.

This is an unrealistic assumption that brings up the issue of termination possibilities of transactions.

Termination Condition of Transactions

Page 15: PMIT-6102 Advanced Database Systems

Slide 15Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Termination Condition of Transactions

Begin_transaction Reservationbegin

input(flight_no, date, customer_name);EXEC SQL SELECT STSOLD,CAP

INTO temp1,temp2FROM FLIGHTWHERE FNO = flight_no AND DATE = date;

if temp1 = temp2 thenoutput(“no free seats”);Abort

else EXEC SQLUPDATEFLIGHT

SET STSOLD = STSOLD + 1WHERE FNO = flight_no AND DATE = date;

EXEC SQLINSERTINTO FC(FNO, DATE, CNAME, SPECIAL);VALUES (flight_no, date, customer_name,

null);Commitoutput(“reservation completed”)

endifend .

Page 16: PMIT-6102 Advanced Database Systems

Slide 16Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Important things: One is, obviously, the fact that if no free seats are

available, the transaction is aborted. The second is the ordering of the output to the

user with respect to the abort and commit commands.

Transactions can be aborted either due to application logic, as is the case here, or due to deadlocks or system failures.

If the transaction is aborted, the user can be notified before the DBMS is instructed to abort it.

However, in case of commit, the user notification has to follow the successful servicing (by the DBMS) of the commit command, for reliability reasons.

Termination Condition of Transactions

Page 17: PMIT-6102 Advanced Database Systems

Slide 17Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Example Transaction – Reads & Writes

Begin_transaction Reservationbegin

input(flight_no, date, customer_name);temp Read(flight_no(date).stsold);if temp = flight(date).cap thenbegin

output(“no free seats”);Abort

endelse begin

Write(flight(date).stsold, temp + 1);Write(flight(date).cname, customer_name);Write(flight(date).special, null);Commit;output(“reservation completed”)

endend.

Page 18: PMIT-6102 Advanced Database Systems

Slide 18Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

The consistency and reliability aspects of transactions are due to four properties:

atomicity, consistency, isolation, and durability. Together, these are commonly referred to as the

ACID properties of transactions.

Properties of Transactions

Page 19: PMIT-6102 Advanced Database Systems

Slide 19Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

ATOMICITY all or nothing

CONSISTENCY no violation of integrity constraints

ISOLATION concurrent changes invisible to other

transactions

DURABILITY committed updates persist

Properties of Transactions

Page 20: PMIT-6102 Advanced Database Systems

Slide 20Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Either all or none of the transaction's operations are performed.

Atomicity requires that if a transaction is interrupted by a failure, its partial results must be undone.

The activity of preserving the transaction's atomicity in presence of transaction aborts due to input errors, system overloads, or deadlocks is called transaction recovery.

The activity of ensuring atomicity in the presence of system crashes is called crash recovery.

Atomicity

Page 21: PMIT-6102 Advanced Database Systems

Slide 21Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

The consistency of a transaction is simply its correctness.

In other words, a transaction is a correct program that maps one consistent database state to another.

Transactions do not violate database integrity constraints.

Consistency

Page 22: PMIT-6102 Advanced Database Systems

Slide 22Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Consistency Degrees Dirty data refers to data values that have been

updated by a transaction prior to its commitment.

In other words, dirty data refer to data items whose values have been modified by a transaction that has not yet committed.

Consider the case where transaction T1 modifies a data item value, which is then read by another transaction T2 before T1 performs a Commit or Abort. In case T1 aborts, T2 has read a value which never exists in the database.

Page 23: PMIT-6102 Advanced Database Systems

Slide 23Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Consistency Degrees

Based on the concept of dirty data, the four levels are defined as follows:

Degree 0 Transaction T does not overwrite dirty data of other

transactions Degree 1

T does not overwrite dirty data of other transactions T does not commit any writes before EOT

Page 24: PMIT-6102 Advanced Database Systems

Slide 24Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Consistency Degrees (cont’d)

Degree 2 T does not overwrite dirty data of other

transactions T does not commit any writes before EOT T does not read dirty data from other

transactions Degree 3

T does not overwrite dirty data of other transactions

T does not commit any writes before EOT T does not read dirty data from other

transactions Other transactions do not dirty any data read by

T before T completes.

Page 25: PMIT-6102 Advanced Database Systems

Slide 25Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation is the property of transactions that requires each transaction to see a consistent database at all times.

An executing transaction cannot reveal its results to other concurrent transactions before its commitment.

solves the lost updates problem. Necessary to avoid cascading aborts. There are a number of reasons for insisting on

isolation. If two concurrent transactions access a data item

that is being updated by one of them, it is not possible to guarantee that the second will read the correct value.

Isolation

Page 26: PMIT-6102 Advanced Database Systems

Slide 26Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation Example Assume that the value of x before they start

executing is 50. Consider the following two transactions:

T1: Read(x) T2: Read(x)x x1 x x+1Write(x) Write(x)Commit Commit

Possible execution sequence:

T1: Read(x)T1: x x1T1: Write(x)T1: CommitT2: Read(x) T2: x x+1 T2: Write(x)T2: Commit

Page 27: PMIT-6102 Advanced Database Systems

Slide 27Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

In this case, there are no problems if transactions T1 and T2 are executed one after the other and transaction T2 reads 51 as the value of x.

the value of x will have 52 as its value at the end of execution of these two transactions.

Isolation Example

Page 28: PMIT-6102 Advanced Database Systems

Slide 28Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

However, since transactions are executing concurrently, the following execution sequence is also possible:

T1: Read(x)T1: x x+1T2: Read(x)T1: Write(x)T2: x x+1T2: Write(x)T1: CommitT2: Commit

Isolation Example

In this case, transaction T2 reads 50 as the value of x. This is incorrect since T2 reads x while its value is being changed from 50 to 51.

Furthermore, the value of x is 51 at the end of execution of T1 and T2 since T2’s Write will overwrite T1’s Write.

Page 29: PMIT-6102 Advanced Database Systems

Slide 29Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation

Show the issue of isolation is directly related to database consistency and concurrency control

Possible execution sequence:

T1: Read(x)T1: x x1T1: Write(x)T1: CommitT2: Read(x) T2: x x+1 T2: Write(x)T2: Commit

T1:Read(x)T1: x x+1T2:Read(x)T1:

Write(x)T2:x x+1T2:Write(x)T1:

CommitT2:CommitFig A Fig B

Page 30: PMIT-6102 Advanced Database Systems

Slide 30Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation Ensuring isolation by not permitting

incomplete results to be seen by other transactions,

as fig A shows, solves the lost updates problem. This type of isolation has been called cursor stability.

In Fig B, the second execution sequence resulted in the effects of T1 being lost.

A second reason for isolation is cascading aborts. If a transaction permits others to see its incomplete

results before committing and then decides to abort, any transaction that has read its incomplete values

will have to abort as well. This chain can easily grow and impose/force

considerable overhead/ tranperancy on the DBMS.

Page 31: PMIT-6102 Advanced Database Systems

Slide 31Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation As we move up the hierarchy of consistency

levels, there is more isolation among transactions.

Degree 0 provides very little isolation other than preventing lost updates.

Since transactions commit write operations before the entire transaction is completed (and committed),

if an abort occurs after some writes are committed to disk, the updates to data items that have been committed will need to be undone.

Since at this level other transactions are allowed to read the dirty data, it may be necessary to abort them as well.

Page 32: PMIT-6102 Advanced Database Systems

Slide 32Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Isolation Degree 2 consistency avoids cascading aborts. Degree 3 provides full isolation

which forces one of the conflicting transactions to wait until the other one terminates.

Such execution sequences are called strict. It is obvious that the issue of isolation is directly

related to database consistency and is therefore the topic of concurrency control.

Page 33: PMIT-6102 Advanced Database Systems

Slide 33Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Durability refers to that property of transactions which ensures that once a transaction commits, the system must guarantee that the results of its operations will never be lost, in spite of subsequent failures.

The durability property brings forth the issue of database recovery, that is, how to recover the database to a consistent state where all the committed actions are reflected.

Durability

Page 34: PMIT-6102 Advanced Database Systems

Slide 34Lectured by, Jesmin Akhter, Assistant professor, IIT, JU

Thank you