Midterm Lesson 4: Data Concurrency THEORY OF DATABASE SYSTEMS Data Concurrency y Features the event wherein many users

can access a single data at the same time. y But when concurrency is enabled, the database must ensure that both official data source and replicated data values are consistent. Concurrency Issues y Lost or Buried Updates o Occur when two or more transactions select the same row and then update the row based on the value originally selected. o Each transaction is unaware of other transactions. o The last update overwrites updates made by the other transactions, which results in lost data y Uncommitted Dependency (Dirty Read) o Occurs when a second transaction selects a row that is being updated by another transaction. o The second transaction is reading data that has not been committed yet and may be changed by the transaction updating the row. y Inconsistent Analysis o Occurs when a second transaction accesses the same row several times and reads different data each time. o In inconsistent analysis, the data read by the second transaction was committed by the transaction that made the change. o Also, inconsistent analysis involves multiple reads (two or more) of the same row and each time the information is changed by another transaction; thus, the term nonrepeatable read. y Phantom Reads o Occur when an insert or delete action is performed against a row that belongs to a range of rows being read by a transaction. o The transaction's first read of the range of rows shows a row that no longer exists in the second or succeeding read, as a result of a deletion by a different transaction. Similarly, as the result of an insert by a different transaction, the transaction's second or succeeding read shows a row that did not exist in the original read. Concurrency Controls y Pessimistic Control o A row is available to the users when the record is being fetched and stays with the user until it is updated within the database. y Optimistic Control o A row cannot be available to other users while the data is currently being updated. o During updating, the database examined the row in the database to determine whether or not any change has been made. o An attempt to update a record that has already been changed can be flagged as concurrency violation y Last In Wins o Any row can never be available to users while the data is currently being updated but there is no effort made to compare updates with the original record. o The record would simply be written out. The potential effect would be overwriting any changes that are being made by other concurrent users since the last refresh of the record. Solving Concurrency y Locking Tables LOCK TABLES inventory WRITE; o Problems  Speed Degradation  Deadlock y Relative Update UPDATE inventory SET quantity = quantity - 3 WHERE item = 'shirt