Professional Documents
Culture Documents
3a. Transaction Processing
3a. Transaction Processing
i. read_item() − reads data item from storage to main memory/(database retrieval, such as SQL
SELECT).
ii. modify_item() − change value of item in the main memory.
iii. write_item() − write the modified value from main memory to storage/(modify database, such as
SQL INSERT, UPDATE, DELETE).
Database access is restricted to read_item() and write_item() operations. Likewise, for all
transactions, read and write forms the basic database operations and also a combination of
SELECT, UPDATE, and INSERT statements
Example from banking database: Transfer of $100 dollars from a checking account to a savings
account.
Note: Each execution of a program is a distinct transaction with different parameters.
Bank transfer program parameters: savings account number, checking account number, transfer
amount.
Basic operations (read and write)
read_item(X):
Reads a database item named X into a program variable. To simplify our notation, we assume that
the program variable is also named X.
read_item(X) command includes the following steps:
i. Find the address of the disk block that contains item X.
ii. Copy that disk block into a buffer in main memory (if that disk
iii. block is not already in some main memory buffer).
iv. Copy item X from the buffer to the program variable named X.
Basic operations (read and write)
write_item(X): Writes the value of program variable X into the database item named X.
write_item(X) command includes the following steps:
i. Find the address of the disk block that contains item X.
ii. Copy that disk block into a buffer in main memory (if that disk block is not already in some
main memory buffer).
iii. Copy item X from the program variable named X into its correct location in the buffer.
iv. Store the updated block from the buffer back to disk (either immediately or at some later
point in time).
Transactions boundaries
Transaction boundaries: Begin transaction and End transaction
Application program may include specification of several transactions separated by Begin and End
transaction boundaries.
Transaction code can be executed several times (in a loop), spawning multiple transactions.
Transactions can end in two states:
i. Commit: transaction successfully completes and its results are committed (made permanent)
ii. Abort: transaction does not complete and none of its actions are reflected in the database
..continuation
Transaction operations
i. Atomicity − This property states that a transaction is an atomic unit of processing, that is, either it is
completed or not performed at all. No partial update should exist.
ii. Consistency − A transaction should take the database from one consistent state to another consistent
state. It should not adversely affect any data item in the database.
iii. Isolation − A transaction should be executed as if it is the only one in the system. There should not be
any interference from the other concurrent transactions that are simultaneously running.
iv. Durability − If a committed transaction brings about a change, that change should be durable in the
database and not lost in case of any failure.
Enforcement of ACID properties:
i. Database constraint system (and application program correctness) responsible for C (introduced in
previous classes)
ii. Concurrency control responsible for I (more in next class)
iii. Recovery system responsible for A and D (more in next class)
Transaction Support In SQL
A single SQL statement is always considered to be atomic:
Either the statement completes execution without error or it fails and leaves the database
unchanged.
No explicit Begin Transaction statement:
Transaction initiation implicit at first SQL statement and at next SQL statement after previous
transaction terminates
Every transaction must have an explicit end statement
COMMIT: the DB must assure that the effects are permanent
ROLLBACK: the DB must assure that the effects are as if the transaction had not yet begun
example
update_proc() {
EXEC SQLWHENEVER SQLERROR GO TO error;
EXEC SQL INSERT
INTO EMPLOYEE
VALUES ('Robert','Smith','991004321',2,35000);
EXEC SQLUPDATE EMPLOYEE
SET SALARY = SALARY * 1.1
WHERE DNO = 2;
EXEC SQLCOMMIT;
return(0);
error: /* continue if error on rollback */
EXEC SQLWHENEVER SQLERROR CONTINUE;
EXEC SQLROLLBACK;
return(1);
}