The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Siti Izani, 2023-07-03 04:42:49

Database Design (1)

Database Design (1)

DATABASE TRANSACTION MANAGEMENT LEARNING OUTCOMES TRANSACTION Describe transaction system. Identify the transaction processing systems category. Describe the properties of database transaction. State the purpose of concurrency control. Describe the problems of concurrency control. Define the Concurrency control with locking methods. Explain database recovery management. A logical unit of work on a database. The entire series of steps necessary to accomplish a logical unit of work. Successful transactions change the database from one CONSISTENT STATE to another CONSISTENT STATE. Batch transaction processing system. Batch processing systems can save money and labor over time, but they may be costly to design and implement up-front. * An entire program (SPMP) * A portion of a program (Update Student Information) * A single command (SELECT/UPDATE/INSERT/DELETE) TRANSACTION PROCESSING CATEGORY ØBatch processing is a technique for automating and processing multiple transactions as a single group. ØBatch processing helps in handling tasks like payroll, end-of-month reconciliation, or settling trades overnight. 45


TRANSACTION On-line transaction processing system (OLTP) Real-time transaction processing system Atomic Consistent ØOLTP is a class of software programs capable of supporting transactionoriented applications on the Internet. ØIn computing, a transaction is a sequence of discrete information exchanges that are treated as a unit. ØMany everyday acts involve OLTP, including online banking, online shopping and even in-store shopping when the point of sale (POS) terminal is tied to inventory management software. ØReal time systems attempt to guarantee an appropriate response to a stimulus or request quickly enough to affect the conditions that caused the stimulus. ØEach transaction in real-time processing is unique; it is not part of a group of transactions. ØReservation systems •Used in any type of business involved in setting aside a product or server for a customer(E.gairasia, train tickets) •Require an acceptable response time ØLibrary loan system •Used to keep track of borrowed items •Barcodes are scanned on the user's card and the item •Items are often stored in a warehouse 4 PROPERTIES OF A TRANSACTION All parts of the transaction must be completed and committed or it must be aborted and rolled back. Each user is responsible to ensure that they would leave the database in a consistent state. 46


Isolation Durability SQL Statements a Commit / Rollback When a transaction sequence is initiated it must continue through all succeeding SQL statements until: Coordination of simultaneous transaction execution in a multiprocessing database system. Ensure transaction serializability in a multi-user database. Lack of Concurrency Control can create data integrity and consistency problems: The final effects of multiple simultaneous transactions must be the same as if they were executed one right after the other. If a transaction has been committed, the DBMS must ensure that its effects are permanently recorded in the database (even if the system crashes). TRANSACTION MANAGEMENT WITH SQL 1. A Commit Statement is Reached 2. A Rollback Statement is Reached 3. The End of the Program is Reached (Commit) 4. The Program is Abnormally Terminated (Rollback) CONCURRENCY CONTROL * Lost Updates * Read Uncommitted Data * Inconsistent Retrievals TRANSACTION 47


Occurs when one transaction can see intermediate results of another transaction before it has been committed. T4 updates bal-x to RM200 but it aborts, so bal-x should be back at the original value of RM100. T3 has read the new value of bal-x (RM200) and uses the value as the basis of RM10 reduction, giving a new balance of RM190, instead of RM90. Problem avoided by preventing T3 from reading balx until after T4 commits or aborts. 2. Uncommitted Dependency Problem TRANSACTION Lost Update Problem Successfully completed update is overridden by another user. T1 withdrawing RM10 from an account with bal-x, initially RM100. T2 depositing RM100 into the same account. Serially, the final balance would be RM190. Loss of T2’s update avoided by preventing T1 from reading bal-x until after update. 1. 48


Occurs when a transaction reads several values but the second transaction updates some of them during the execution of the first. Sometimes referred to as dirty read or unrepeatable read. T6 is totaling balances of account x (RM100), account y (RM50), and account z (RM25). Meantime, T5 has transferred RM10 from bal-x to bal-z, so T6 now has the wrong result (RM10 too high). Problem avoided by preventing T6 from reading bal-x and bal-z until after T5 completed updates. Locking Time-Stamping Optimistic 3. Inconsistent Analysis Problem CONCURRENCY CONTROL ALGORITHMS A Transaction “locks” a database object to prevent another object from modifying the object Assign a global unique time stamp to each transaction. Assumption that most database operations do not conflict TRANSACTION 49


Lock guarantees exclusive use of data item to the current transaction. Prevents reading Inconsistent Data. Lock Manager is responsible for assigning and policing the locks used by the transaction. Binary Locks – Lock with 2 States All transactions require a Lock and Unlock Operation for Each Object Accessed (Handled by DBMS). Binary lock ables to prevent the following issues: TYPES OF LOCKS: 1. Binary * Locked (1) – No other transaction can use that object * Unlocked (0) – Any transaction can lock and use object * Eliminates Lost Updates * Too Restrictive to Yield Optimal Concurrency Conditions LOCKING 50


Defines how transactions Acquire and Relinquish Locks Transactions acquire all locks it needs until it reaches a locked point. When locked, data is modified and locks are released. 3. Two-Phase Locking (2PL) i. Growing Phase – The transaction acquires all locks (doesn’t unlock any data) ii. Shrinking Phase – The transaction releases locks (doesn’t lock any additional data) Preventing Lost Update Problem using 2PL LOCKING Shared Lock – Concurrent Transactions are granted READ access on the basis of a common lock. Exclusive Lock – Access is reserved for the transaction that locked the object. 3 States: More Efficient Data Access Solution. 2. Shared / Exclusive Locks * Unlocked (Available), * Shared (Read), * Exclusive (Write: Update/Insert/Delete) 51


Preventing Uncommitted Dependency Problem using 2PL Preventing Inconsistent Analysis Problem using 2PL LOCKING 52


Prevention – A transaction requesting a new lock is aborted if there is the possibility of a deadlock – Transaction is rolled back, Locks are released, Transaction is rescheduled. Detection – Periodically test the database for deadlocks. If a deadlock is found, abort/rollback one of the transactions. Avoidance – Requires a transaction to obtain all locks needed before it can execute – requires locks to be obtained in succession. CONTROLLING DEADLOCK DEADLOCK Occur when 2 transactions exist in the following mode: If Ti does not unlock Y, T2 cannot begin. If Tj does not unlock X, T1 cannot continue. Ti & Tj wait indefinitely for each other to unlock data. Deadlocks are only possible if a transaction wants an Exclusive Lock (No Deadlocks on Shared Locks). DEADLOCK Ti = access data item X and Y Tj = Access data items Y and X 53


RECOVERY FACILITIES Database Backup A backup is a duplicate copy of data. A backup protects data from application error and acts as a safeguard against unexpected data loss, by providing a way to restore original data. Backups are divided into physical backups (external resource: DVD and etc.) and logical backups(IMPORT/EXPORT utilities). Backups are useful primarily for two purposes. 1. * Restore a state after a disaster. * Restore small numbers of files after they have been accidentally deleted or corrupted. DBMS should provide the following facilities to assist with recovery: Backup mechanism, which makes periodic backup copies of the database Logging facilities, which keep track of the current state of transactions and database changes (updated transaction). Checkpoint facility, which enables updates to the database in progress (active transaction) to be made permanent. Recovery Manager, which allows DBMS to restore the database to consistent state following a failure. RECOVERY FACILITIES 1. 2. 3. 4. 54


RECOVERY FACILITIES Keeps track of all transactions that update the database: Used for recovery in case of a Rollback. 2. Transaction Log * Record for the beginning of the transaction * Type of operation (insert / update / delete) * Names of objects/tables affected by the transaction * Before and After Values for Updated Fields * Pointers to Previous and Next Transaction Log Entries for the same transaction * The Ending of the Transaction (Commit) A checkpoint record is created containing identifiers of all active transactions. When failure occurs, redo all transactions that were committed since the checkpoint and undo all transactions active at the time of the crash. 3. Checkpointing 55


Any changes made to the table contents are not saved on disk until you close the database, close the program you are using, or use the COMMIT command. If the database is open and a power outage or some other interruption occurs before you issue the COMMIT command, your changes will be lost and only the original table contents will be retained. The syntax for the COMMIT command is: The COMMIT command permanently saves all changes—such as rows added, attributes modified, and rows deleted—made to any table in the database. Therefore, if you intend to make your changes to a table permanent, it is a good idea to save those changes by using: COMMIT [WORK] COMMIT; LEARNING OUTCOMES COMMIT Perform transaction of a given database using SQL statements Use START TRANSACTION and COMMIT statements TRANSACTIONAL CONTROL LANGUAGE (TCL) 56


ROLLBACK The rollback command is used to restore table contents. If you have not yet used the COMMIT command to store the changes permanently in the database, you can restore the database to its previous condition with the ROLLBACK command. ROLLBACK undoes any changes since the last COMMIT command and brings the data back to the values that existed before the changes were made. To restore the data to their “pre-change” condition, type: COMMIT and ROLLBACK work only with data manipulation commands that are used to add, modify, or delete table rows. For example, assume that you perform these actions: The ROLLBACK command will undo only the results of the INSERT and UPDATE commands. All data definition commands (CREATE TABLE) are automatically committed to the data dictionary and cannot be rolled back. ROLLBACK; and then press the Enter key. 1. CREATE a table called SALES. 2. INSERT 10 rows in the SALES table. 3. UPDATE two rows in the SALES table. 4. Execute the ROLLBACK command. 57


EXAMPLE OF TCL START TRANSACTION; INSERT INTO STUDENT(MAT_NO, NAME, AGE, GENDER, IC, DEPT_CODE) VALUES ('18DDTF1014', ‘Sarah’, 21, 'F', '010222046586', 'JKM'); COMMIT; STUDENT Example 1 Example 2 START TRANSACTION; UPDATE STUDENT SET IC='991231015042' WHERE MAT_NO='18DDTF1012'; COMMIT; Example 3 START TRANSACTION; DELETE FROM STUDENT WHERE MAT_NO='18DDTF1012'; COMMIT; 58


EXAMPLE OF TCL START TRANSACTION; INSERT INTO DEPARTMENT(DEPT_CODE, DEPT_NAME) VALUES ('JRKV', ‘Jabatan Rekabentuk dan Komunikasi Visual'); ROLLBACK; DEPARTMENT Example 1 Example 2 START TRANSACTION; UPDATE DEPARTMENT SET DEPT_CODE='Information Technology' WHERE DEPT_CODE='JTMK'; ROLLBACK; Example 3 START TRANSACTION; DELETE FROM DEPARTMENT WHERE DEPT_CODE='JP'; ROLLBACK; 59


CREATE a database named GREENCOMP CREATE a table called STAFF. Begin a transaction. INSERT 4 rows in the STAFF table. Save all transactions. Increase all salaries by 20% using UPDATE. DELETE female staff from the table. Execute the ROLLBACK command. Based on the STAFF table, answer all the following questions: 1. 2. 3. 4. 5. 6. 7. 8. EXERCISE STAFF Instruction: 60


REFERENCES 61 Arumilli, U. (2014, October 29). MSSQL concurrency control and locking – MSSQL Interview questions with answers. Udayarumilli.com. http://udayarumilli .com/sql-server-concurrency-control-interview-questions/ Coronel, C. & Morris, S. (2018). Database Systems: Design, Implementation, & Management 13th Edition. Cengage Learning, Inc. Elmasri, R., & Navathe, S. (2017). Fundamentals of database systems (Vol. 7). Pearson. Jaypipes (2015, January 1). Understanding reservations, concurrency, and locking in Nova. Joinfu. http://www.joinfu.com/2015/01/understandingreservations-concurrency-locking-in-nova/ Sqlbak (2021, September 11). Full backup. SQL Server Backup Academy. https:// sqlbak.com/academy/full-backup Sqlbak (2021, September 11). Transaction log. SQL Server Backup Academy. https://sqlbak.com/academy/trnsaction-log W2schools (2021, September 11). SQL data yypes for MySQL, SQL Server, and MS Access. W3schools. https://www.w3schools.com/sql/sql_datatypes.asp


Click to View FlipBook Version