Transactions and Locks

Slides:



Advertisements
Similar presentations
CM20145 Concurrency Control
Advertisements

Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
Transactions A process that reads or modifies the DB is called a transaction. It is a unit of execution of database operations. Basic JDBC transaction.
Recovery from Crashes. Transactions A process that reads or modifies the DB is called a transaction. It is a unit of execution of database operations.
Recovery 10/18/05. Implementing atomicity Note, when a transaction commits, the portion of the system implementing durability ensures the transaction’s.
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
ACID A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction, may change the DB from.
Transaction Management and Concurrency Control
Quick Review of May 1 material Concurrent Execution and Serializability –inconsistent concurrent schedules –transaction conflicts serializable == conflict.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
What is a Transaction? Logical unit of work
Chapter 8 : Transaction Management. u Function and importance of transactions. u Properties of transactions. u Concurrency Control – Meaning of serializability.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Transactions and Recovery
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.
Transactions and Locks Lesson 22. Skills Matrix Transaction A transaction is a series of steps that perform a logical unit of work. Transactions must.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 Transactions BUAD/American University Transactions.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Databases Illuminated
Department of Computer Science and Engineering, HKUST 1 More on Isolation.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
Chapter 15 Recovery. Topics in this Chapter Transactions Transaction Recovery System Recovery Media Recovery Two-Phase Commit SQL Facilities.
1 CS 430 Database Theory Winter 2005 Lecture 16: Inside a DBMS.
SCUJoAnne Holliday11–1 Schedule Today: u Transaction concepts. u Read Sections Next u Authorization and security.
Concurrency Control in Database Operating Systems.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
How transactions work A transaction groups a set of Transact-SQL statements so that they are treated as a unit. Either all statements in the group are.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
Transactions and Locks A Quick Reference and Summary BIT 275.
The Relational Model1 Transaction Processing Units of Work.
Chapter 16 Concurrency. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.16-2 Topics in this Chapter Three Concurrency Problems Locking Deadlock.
Section 06 (a)RDBMS (a) Supplement RDBMS Issues 2 HSQ - DATABASES & SQL And Franchise Colleges By MANSHA NAWAZ.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Module 11: Managing Transactions and Locks
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
18 September 2008CIS 340 # 1 Last Covered (almost)(almost) Variety of middleware mechanisms Gain? Enable n-tier architectures while not necessarily using.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
SYSTEMS IMPLEMENTATION TECHNIQUES TRANSACTION PROCESSING DATABASE RECOVERY DATABASE SECURITY CONCURRENCY CONTROL.
Transaction Management
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Isolation Levels Understanding Transaction Temper Tantrums
Batches, Transactions, & Errors
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
Understanding Transaction Isolation Levels
On transactions, and Atomic Operations
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Objectives Define and describe transactions
Transactions and Concurrency
STRUCTURE OF PRESENTATION :
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
About Wolf DBA for over 19 years At RDX for nearly 9
Isolation Levels Understanding Transaction Temper Tantrums
Transactions, Properties of Transactions
Presentation transcript:

Transactions and Locks SQL Server log and “checkpoints” Locks

Transactions Atomicity – smallest grouping of one or more statement that should be considered “all or nothing”

Atomicity Suppose you are Banker and Sally wants to transfer $1000 from checking to savings Update checking set balance = balance – 1000 where account = “sally’ Update savings set balance = balance + 1000 where account = ‘sally’ What if first statement executes and then system dies and second statement doesn’t execute?

Atomicity Ideally, would like way to insure both statements execute – no way Stuff happens Almost as good – either both statements execute or neigher.

Transaction Begin transaction Commit transaction Rollback transaction Set starting point Commit transaction Make transaction permanent, irreversible part of database Rollback transaction “forget that it every happened” Save transaction Establish specific marker allowing us to do only a partial rollback

Begin tran Everything beyond this point that is not eventually committed will be forgotten as far as database is concerned BEGIN TRAN[saction] [<transaction name>| <@transaction variable>]

Commit tran End of a completed transaction. At the point you issue commit, transaction is considered to be “durable” – transaction is permanent and will last even if you have a system failure (as long as you have backup) – can’t undo COMMIT TRAN[saction] [<transaction name>| <@transaction variable>]

Rollback tran Undo to the beginning of the transaction (Exception is use of save points) ROLLBACK TRAN[saction] [<transaction name>|<save point name> <@transaction variable>|<@savepoint variable>]

Save Tran Bookmark or named place marker in transaction You can rollback to an exact point in the code rather than just rollback to beginning of transaction. Once any rollback occurs, ALL SAVEPOINTS are gone Save tran is not for beginners SAVE TRAN[saction] [<save point name> |<@save point variable>]

How DB Actually Works Figure 14-1 Activity “logged” to transaction log Data in your database is combination of data in physical database file(s) but also transactions that have been committed to log since last CHECKPOINT Checkpoint – periodic operation that forces all “dirty” pages for the database currently in use to be actually written to memory. Dirty pages – log or data pages that have been modified after they were read into cache but the modifications have not been written to disk. All this happens automatically in background

When checkpoints issued Need to read data into cache that is already full yet another reason for still more main memory CHECKPOINT command At normal shutdown of server Unless WITH NOWAIT option used When SIMPLE RECOVERY option used and log become 70 percent full When amount of data in log since last checkpoint (active portion) exceeds size that server could recover in amount of time specified in recovery interval option

FAILURE and RECOVERY Recover happens every time SQL Server starts up. SQL server applies every committed transaction in log since last checkpoint to database file(s). Any changes to log that are not committed are rolled back See figure 14-2

Locks and Concurrency Concurrency – two or more users each trying to interact with the same object at the same time. Concurrency can be critical to the performance of your system The foundation of dealing with concurrency is the process of locking Locks are a mechanism for preventing a process from performing an action on an object that conflicts with something already being done on that object.

Locks Can have many simultaneous reads on an object. Typically only one write on an object that the same time. Process can request “read only access” or “write” access

Lock Manager If initial request for an object is read only, the object is locked for writing until read request is completed. Other read requests are allowed. If write request and no current read requests, then write access granted and everything locked out until write is completed.

Locking Problems Dirty reads Non-repeatable reads Phantoms Lost updates Need to correctly set “transaction isolation level” to prevent these problems

Dirty Reads Consider when a transaction reads a record that is part of another transaction that isn’t completed yet. What happens if transaction rolls back? See table pg 432 This situation cannot happen if you are using SQL serve default for transaction isolation level (called READ COMMITED)

Non-Repeatable Reads A non-repeatable read occurs when you read the same record twice in a transaction, and a separate transaction alters that data in the interim. Easy to confuse with dirty read See table pg 433 Can prevent in two ways Check constraint and monitor for 547 error(?) Reactive approach – check if problem has happened Set our isolation level to be “repeatable read” or “serializable” This could cause as many problems as it fixes – but still an option

Phantoms Records that appear “mysteriously, as if unaffected by an update or delete statement that you have already issued Can happen quite legitimately in normal course of operating your system Example – update to new minimum wage: update employees set hourlyRate = 6.75 where hourlyRate <6.75 alter table employees add ckWage Check (HourlyRate >=6.75) Ckwage may fail (Someone ran an insert while your update was running) Very rare Cure by setting transaction isolation level to “serializable”

Lost Updates Update is successfully written to database but then is overwritten by another transaction. Two transactions read a record First makes change Second make change, losing first update (ATM example or pg 435)

Lockable Resources Database – entire database can be locked Table – entire table can be locked, including ALL data-related objects including ALL data rows, and ALL keys in ALL indexes Extent – entire extent (data or index) is locked (8 pages) Page – all data or index keys on that page Key – lock on particular or series of keys Row – technically row identifier (RID – internal SQL server construct)

Lock Escalation and Lock Effect on Performance Finer granularity (e.g., row vs. table) is good, but as more and more items locked, overhead becomes too much Longer lock in place, higher probability that someone else will want locked item Lock Escalation - when number of locked being maintained reaches threshold, lock is escalated to next higher level Number of locks is critical, not number of users One can single handedly lock a table with massive update, or even lock multiple tables

Lock Modes As important as what-is-being-locked is LOCK MODE Shared locks -Most basic lock Used for read-only access – allows others to read but not update Exclusive Locks – no one else can read or write or lock Update lock- hybred between shared and exclusive Need shared lock until validate “where clause” and then need exclusive lock on rows or table might be faster) that are to be altered. Avoids one for of deadlock

Deadlock Suppose two update queries running in shared mode. Query A completes query and is ready for physical update – wants to exclusive to exclusive lock, but can’t as query B still has shared lock Query B finishes query and now needs to do physical update, but can’t as query A still has shared. IMPASSE!! Update lock solves this as it prevents other update locks from being established.

Intent Locks Placeholder You have a lock on a row, when prevents someone locking the containing page, extent, table. Only need to examine intent locks at table level and not check every row or page

Intent Locks Intent shared lock Intent exclusive lock Shared lock has or is going to be established at some lower point in hierarchy Applies only to pages and tables Intent exclusive lock Shared with intent exclusive lock Intention to establish shared lock at some lower level that will eventually become modify lock

Schema Locks Schema modification lock (sch-M) No query or other CREATE, ALTER, DROP statements can execute during duration of this lock Schema stability lock (SCH-S) Prevents SCH-M

Bulk Update Lock Variation of table lock Table locked from any other normal activity but still allows multiple bulk insert operations

Lock Compatibility See table page 438

Optimizer Hints Locks generally automatic and should be kept that way – however … Are ways to optimize ADVANCED TOPIC Often abused by “experienced” sql server developers

Determining Locks using Management Studio Management will show you locks using process ID or object using activity monitor Figure 14-3

Isolation Level Transactions and locks are inextricably linked By default, and lock that is data modification related will, once created, be held for the duration of the transaction. LONG transactions will lock out other processes FOUR different isolation levels you can set: Read committed (default) Read uncommitted Repeatable read Serializable

syntax SET TRANSACTION ISOLATION LEVEL <read committed | read uncommitted | repeatable read | seriablizable>

READ COMMITTED Default Any shared locks you create will be automatically released as soon as the statement that created them is complete. Sql server does not wait until the end of the transaction Actions (update, delete, insert) - lock will be held for the duration of the transaction, in case you need to rollback. Dirty reads prevented, but non-repeatable reads and phantoms can still occur.

Read Uncommitted Most dangerous of all isolation levels, but has highest performance in terms of speed. Tells SQL server not to set locks and not to honor and locks. Use with reporting - Management wants to run regular reports that preclude data entry because of locks held by reports Run reports with read uncommitted – but exact values are probably meaningless Get same results by using NOLOCK optimizer hint with your query – but using isolation level is simplier for entire report.

Repeatable Read Extra level of concurrency protection by preventing both dirty reads and non-repeatable reads but holding shared locks until end of transaction can hurt productivity

Serializable Any update, delete, or insert in a transaction must not “meet” any where clause in that transaction. Prevents all forms of concurrency issues except for a lost update. But concurrency and consistency are opposites – this can REALLY SLOW THINGS DOWN Stick to default unless really important reasons for doing otherwise.

Deadlocks (“A 1205”) Error number 1205 One lock can do what it needs in order to clear because a second lock is holding that resource and vice versa (could be more than two, or twenty…) SQL server chooses a “deadlock victim” – that transaction is rollback and is notified of what happened with a 1205.

Detecting a Deadlock Every 5 seconds sql server checks all current transactions for what locks they are waiting on but haven’t yet been granted. If it rechecks after another 5 seconds and finds a previous lock request still pending, it recursively checks all open transactions for a circular chain of lock requests If circular chain found, then deadlock victim chosen.

Avoiding Deadlocks Rules of thumb to reduce/eliminate deadlocks: Use your objects in the same order Keep transactions as short as possible and in one batch Use lowest transaction isolation level necessary DO NOT allow open-ended interruptions (user interactions!, batch separations) within same transaction In controlled environments, use BOUND CONNECTIONS (see below)

Same Objects, Same Order This rule easy to implement with little cost and generates good results. Every query, procedure, trigger Example page 444

Short Transactions The longer a transaction is open, the more it touches, and the higher the probability of locking something else out.

Lowest Transaction Isolation duh

No Open-Ended Transactions Don’t hold locks while waiting for user Input! (example pg 445) Someone in service department (or some boss who insists) wants to use an update screen to view data Then goes on to view a work order Then forgets and goes to lunch (I have seen this happen!) Not just user input, but any process that may have an open ended wait