DB Concurrency ITEC 340 Database I Dr. Ian Barland

Slides:



Advertisements
Similar presentations
1 Chapter 10 Protecting Data Integrity in a Multiuser Environment.
Advertisements

Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
What is a Transaction? A transaction is a logical logic of work A transaction may have one of two outcomes –When a transaction completes successfully,
Allowing Multi-user Access Grant – GRANT ON TO |WITH GRANT OPTION | –GRANT TO | WITH ADMIN OPTION| – can be PUBLIC or a role – can be ALL Revoke – REVOKE.
Data and Database Administration Chapter 12. Outline What is Concurrency Control? Background Serializability  Locking mechanisms.
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
Transaction Management and Concurrency Control
Chapter 15 Transaction Management. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Transaction basics Concurrency.
10 1 Chapter 10 Transaction Management and Concurrency Control Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
What is a Transaction? Logical unit of work
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 COS 346 Day 18.
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Chapter 9 Transaction Management and Concurrency Control
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
9 Chapter 9 Transaction Management and Concurrency Control Hachim Haddouti.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Transaction Management and Concurrency Control
Transaction Management Chapter 9. What is a Transaction? A logical unit of work on a database A logical unit of work on a database An entire program An.
Academic Year 2014 Spring Academic Year 2014 Spring.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 IT420: Database Management and Organization Transactions 31 March 2006 Adina Crăiniceanu
1 Transactions BUAD/American University Transactions.
Allowing Multi-user Access Grant – GRANT ON TO – |WITH GRANT OPTION / WITH ADMIN OPTION| – can be PUBLIC or a role – can be all Revoke – REVOKE ON FROM.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220m.htm
Ch 10: Transaction Management and Concurrent Control.
1 Transactions Chapter Transactions A transaction is: a logical unit of work a sequence of steps to accomplish a single task Can have multiple.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
The Relational Model1 Transaction Processing Units of Work.
Concurrency Chapter 6.2 V3.1 Napier University Dr Gordon Russell.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Transaction Management and Concurrent Control
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
10 1 Chapter 10_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
10 Transaction Management and Concurrency Control MIS 304 Winter 2005.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Transactions and Concurrency Control. 2 What is a Transaction?  Any action that reads from and/or writes to a database may consist of  Simple SELECT.
Transactions in PostgreSQL
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Transaction Properties
Database Processing: David M. Kroenke’s Chapter Nine: Part One
Concurrency.
Chapter 10 Transaction Management and Concurrency Control
Concurrency Control WXES 2103 Database.
Concurrency Unit 4.2 Dr Gordon Russell, Napier University
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
Concurrency Unit 4.2 Dr Gordon Russell, Napier University
Presentation transcript:

DB Concurrency ITEC 340 Database I Dr. Ian Barland (and, access privileges) ITEC 340 Database I Dr. Ian Barland based on notes by Dr. Pittges, Dr. Phillips

Allowing Multi-user Access GRANT select on Students to fred; GRANT select, insert ON Customers to ALL; GRANT ALL on Customers to fred; CREATE ROLE manager_privs; GRANT select, insert, update ON Customers TO manager_privs; GRANT manger_privs TO amy, bob, fred;

grant syntax Grant Revoke Very effective when used with views GRANT <objPriv> ON <object> TO <user> GRANT <sysPriv> ANY TABLE TO <user> <user> can also be PUBLIC, or a role <objPriv> can be ALL <sysPriv> includes all <objPrivs> plus CREATE Revoke REVOKE <objpriv> ON <object> FROM <user> REVOKE <syspriv> FROM <user> Very effective when used with views

Oracle Object Privs ALTER DELETE EXECUTE INDEX INSERT SELECT UPDATE ALL

Problems with Concurrent Access Lost updates Uncommitted Data Inconsistent retrievals Solution Resource Locking: While a user (transaction) holds a lock, no other user can access that resource.

Locking Granularity lock entire database lock one table lock one page Not efficient lock one table Sometimes needed for batch updates Not very efficient lock one page Usually contains multiple rows Locks a unit corresponding to a the working unit of the OS lock one row Most commonly used locking level Very efficient lock one column Locks only certain attributes of a row.

How to specify locking in Oracle Table Lock Command the 'FOR UPDATE' option of SELECT command: Select * From Customer Where CustID = '123456' For update; -- If I can't get lock, keep waiting until I can (“block”) Select * From Customer Where CustID = '123456' For update WAIT 5; -- Try to get lock for 5sec, before throw exception. Select * From Customer Where CustID = '123456' For update NOWAIT; -- If I can’t get lock, immediately throw exception.

Lock Types and Strategies Exclusive vs. Shared Locks (lock type) Exclusive Will not allow another transaction to obtain a lock of any kind. Shared Allows multiple transactions to read the data. Will not allow any other transaction to write the locked rows. Can potentially be upgraded to an exclusive lock Optimistic vs. Pessimistic Locking (locking strategy) Pessimistic (“lock, ‘cause somebody’s almost surely going to interfere”) Rows are locked at point of client retrieval of data Client application will not proceed if locks cannot be obtained Locks are not released until client application submits completed transaction Optimistic (“nobody's likely to interfere w/ me, so minimize locking”) No locking occurs until the transaction is ready for submission by the client program Can use several techniques to avoid business rule violations Check row image before updating (timestamp, sequence number, or compare fields) Update only changed fields Use incremental updates to balances Use conditional transactions with a timer Client refreshes (not typically an option on web applications)

Pessimistic Locking Strategy Steps Request Lock(s) Retry (wait) or Abort Lock(s) Acquired Read ABORT/rollback Ponder Update Commit/Release Lock(s)

Optimistic Locking Strategy Steps Read Ponder Request Lock(s) Retry (wait) or Abort Lock(s) Acquired ABORT/rollback Check for Mutation Mutation Detected Update * * update should include update to timestamp or mutation sequence number. Commit/Release Lock(s)

Transactions Transaction Definition: Example: A logical unit of work that consists of one or more SQL statements that moves the database from one consistent state to another consistent state. Therefore, the transaction must succeed or fail as a unit. Example: 01/01/02 Journal Entry #87543 110 Accounts Receivable – R. Smith(123) 1,045 401 Service Revenue – tax services 645 402 Service Revenue – consulting services 400 (provided services on account) Would cause multiple related inserts and updates

Oracle: No Transaction Defined Begin Update accounts set balance = balance + 1045 where accountnum = ‘110’; Commit; Update accounts set balance = balance + 645 where accountnum = ‘401’; Update accounts set balance = balance + 400 where accountnum = ‘402’; Update customers set BalDue = BalDue + 1045 where custid = 123; Insert into JournalEntries (JENumber, JEDate, Description) values (87543, ‘01-JAN-02’, ‘provided services on account’); Insert into DrCrDetail (JENumber, LineNumber, Account, Amount) values (87543, 1, ‘110’, 1045.00); values (87543, 2, ‘401’, 645.00); values (87543, 3, ‘402’, 400.00); End; General guide: Commit as soon as you can (but no sooner).

Transaction Methods (Oracle) BEGIN [transactName] is optional. COMMIT [TRAN[SACTION] transactName] saves the changes and ends the current transaction. SAVEPOINT [savepointName] optional, creates a point that can be rolled back to ROLLBACK [TO savepointName] ends the current transaction and restores the databases in the Workspace object to the state they were in when the current transaction began. The transactName is ignored – it’s purely for readability in nested commits.

Transaction Methods (Oracle with lock waits) Begin Begin Transaction; Update accounts set balance = balance + 1045 where accountnum = ‘110’; Update accounts set balance = balance + 645 where accountnum = ‘401’; Update accounts set balance = balance + 400 where accountnum = ‘402’; Update customers set BalDue = BalDue + 1045 where custid = 123; Insert into JournalEntries (JENumber, JEDate, Description) values (87543, ‘01-JAN-02’, ‘provided services on account’); Insert into DrCrDetail(JENumber, LineNumber, Account, Amount) values (87543, 1, ‘110’, 1045.00); Insert into DrCrDetail (JENumber, LineNumber, Account, Amount) values (87543, 2, ‘401’, 645.00); values (87543, 3, ‘402’, 400.00); Commit; Exception rollback; End;

Transaction/Locking Problem Deadlock Solutions Prevention e.g.: Two-phase locking with nowait Dectection Detect, then kill and rollback one transaction

Transaction Methods (Oracle with 2 stage locking) Begin Begin Transaction; Select * from accounts where accountnum in (‘110’, ‘401’, ‘402’) for update nowait; Select * from customers where custid = 123 for update nowait; Update accounts set balance = balance + 1045 where accountnum = ‘110’; Update accounts set balance = balance + 645 where accountnum = ‘401’; Update accounts set balance = balance + 400 where accountnum = ‘402’; Update customers set BalDue = BalDue + 1045 where custid = 123; Insert into JournalEntries (JENumber, JEDate, Description) values (87543, ‘01-JAN-02’, ‘provided services on account’); Insert into DrCrDetail(JENumber, LineNumber, Account, Amount) values (87543, 1, ‘110’, 1045.00); Insert into DrCrDetail (JENumber, LineNumber, Account, Amount) values (87543, 2, ‘401’, 645.00); values (87543, 3, ‘402’, 400.00); Commit; Exception rollback; End;

SELECT FOR UPDATE WAIT <seconds> Begin Begin Transaction; Select * from accounts where accountnum in (‘110’, ‘401’, ‘402’) for update wait 3; Select * from customers where custid = 123 for update wait 3; Update accounts set balance = balance + 1045 where accountnum = ‘110’; Update accounts set balance = balance + 645 where accountnum = ‘401’; Update accounts set balance = balance + 400 where accountnum = ‘402’; Update customers set BalDue = BalDue + 1045 where custid = 123; Insert into JournalEntries (JENumber, JEDate, Description) values (87543, ‘01-JAN-02’, ‘provided services on account’); Insert into DrCrDetail(JENumber, LineNumber, Account, Amount) values (87543, 1, ‘110’, 1045.00); Insert into DrCrDetail (JENumber, LineNumber, Account, Amount) values (87543, 2, ‘401’, 645.00); values (87543, 3, ‘402’, 400.00); Commit; Exception rollback; End;