Database Transactions  Transaction Management and Concurrency Control.

Slides:



Advertisements
Similar presentations
Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.
Advertisements

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.
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,
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Transactions and Locking Rose-Hulman Institute of Technology Curt Clifton.
Module 15: Managing Transactions and Locks. Overview Introduction to Transactions and Locks Managing Transactions SQL Server Locking Managing Locks.
Transaction Management and Concurrency Control
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.
Transaction Management and Concurrency Control Telerik Software Academy Databases.
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
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.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
1 IT420: Database Management and Organization Session Control Managing Multi-user Databases 24 March 2006 Adina Crăiniceanu
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
Transactions and Locks A Quick Reference and Summary BIT 275.
Random Logic l Forum.NET l Transaction Isolation Levels Forum.NET Meeting ● Nov
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
15.1 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. E.g. transaction to transfer.
SQLintersection Understanding Transaction Isolation Levels Randy Knight Wednesday, 3:45-5:00.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
Transaction Processing Concepts Muheet Ahmed Butt.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
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.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.
Does the Optimistic Concurrency resolve your blocking problems Margarita Naumova, SQL Master Academy.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15.
User-defined functions, Procedures, Triggers and Transactions
Database Transactions
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Isolation Levels Understanding Transaction Temper Tantrums
Locks, Blocks, and Deadlocks; Tame the Sibling Rivalry SQL Server Family Management ~ Wolf ~ This template can be used as a starter file for presenting.
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Database Processing: David M. Kroenke’s Chapter Nine: Part One
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
Concurrency Control WXES 2103 Database.
Understanding Transaction Isolation Levels
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Lecture 13: Transactions in SQL
Transaction management
Objectives Define and describe transactions
Locks, Blocks, Deadlocks
Transactions and Concurrency
Sioux Falls, SD | Hosted by (605) SQL
Database Administration
Chapter 14: Transactions
About Wolf DBA for over 19 years At RDX for nearly 9
Isolation Levels Understanding Transaction Temper Tantrums
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
Lecture 11: Transactions in SQL
Presentation transcript:

Database Transactions  Transaction Management and Concurrency Control

Agenda  Transactions  The Magical “ACID” Word  Locks and SQL Server Concurrency  Troubleshooting Locking Problems  Transaction Isolation Levels 2

What is a Transaction?

Transactions  Transaction  Transaction is a sequence of actions (database operations) executed as a whole:  Either all of them complete successfully  Or none of the them  Example of transaction:  A bank transfer from one account into another (withdrawal + deposit)  If either the withdrawal or the deposit fails the whole operation is cancelled 4

Transactions: Lifecycle 5 Rollback Commit ReadWrite Durable starting stateDurable,consistent, ending state Sequence of reads and writes

Transactions Behavior  Transactions guarantee the consistency and the integrity of the database  All changes in a transaction are temporary  Changes are persisted when COMMIT is executed  At any time all changes can be canceled by ROLLBACK  All of the operations are executed as a whole  Either all of them or none of them 6

Transactions: Example Withdraw $ Read savings 2. New savings = current - $ Read checking 4. New checking = current + $ Write savings 6. Write checking 1. Read current balance 2. New balance = current - $ Write new balance 4. Dispense cash Transfer $100

What Can Go Wrong?  Some actions fail to complete  For example, the application software or database server crashes  Interference from another transaction  What will happen if several transfers run for the same account in the same time? 8

ACID Transactions

Transactions Properties  Modern DBMS servers have built-in transaction support  Implement “ACID” transactions  E.g. MS SQL Server, Oracle, MySQL, …  ACID means:  Atomicity  Consistency  Isolation  Durability 10

Atomicity  Atomicity means that  Transactions execute as a whole  DBMS to guarantee that either all of the operations are performed or none of them  Atomicity example:  Transfer funds between bank accounts  Either withdraw + deposit both execute successfully or none of them  In case of failure the DB stays unchanged 11

Consistency  Consistency means that  The database is in a legal state when the transaction begins and when it ends  Only valid data will be written in the DB  Transaction cannot break the rules of the database, e.g. integrity constraints  Primary keys, foreign keys, alternate keys  Consistency example:  Transaction cannot end with a duplicate primary key in a table 12

Isolation  Isolation means that  Multiple transactions running at the same time do not impact each other’s execution  Transactions don’t see other transaction’s uncommitted changes  Isolation level defines how deep transactions isolate from one another  Isolation example:  If two or more people try to buy the last copy of a product, just one of them will succeed. 13

Durability  Durability means that  If a transaction is committed it becomes persistent  Cannot be lost or undone  Ensured by use of database transaction logs  Durability example:  After funds are transferred and committed the power supply at the DB server is lost  Transaction stays persistent (no data is lost) 14

Managing Transactions in SQL

Transactions and SQL  Start a transaction  BEGIN TRANSACTION  Some RDBMS use implicit start, e.g. Oracle  Ending a transaction  COMMIT  Complete a successful transaction and persist all changes made  ROLLBACK  “Undo” changes from an aborted transaction  May be done automatically when failure occurs 16

Transactions in SQL Server: Example  We have a table with bank accounts:  We use a transaction to transfer money from one account into another 17 CREATE TABLE Accounts( Id int NOT NULL PRIMARY KEY, Balance decimal NOT NULL) CREATE PROCEDURE MONEY) AS BEGIN BEGIN TRAN; (example continues)

Transactions in SQL Server: Example (2) 18 UPDATE Accounts SET Balance = Balance WHERE Id IF <> 1 BEGIN ROLLBACK; RAISERROR('Invalid src account!', 16, 1); RETURN; END; UPDATE Accounts SET Balance = Balance WHERE Id IF <> 1 BEGIN ROLLBACK; RAISERROR('Invalid dest account!', 16, 1); RETURN; END; COMMIT; END;

Auto Commit Transactions  Default transaction mode  Every TSQL statement is committed or rolled back on completion  Compile errors result in entire batch not being executed  Run time errors may allow part of the batch to commit run time error - partially executed USE AdventureWorks2012; GO CREATE TABLE TestBatch (Cola INT PRIMARY KEY, Colb CHAR(3)); GO INSERT INTO TestBatch VALUES (1, 'aaa'); INSERT INTO TestBatch VALUES (2, 'bbb'); INSERT INTO TestBatch VALUES (1, 'ccc'); -- Duplicate key error. GO SELECT * FROM TestBatch; -- Returns rows 1 and 2. GO

Implicit Transactions  SQL Server is responsible for opening the transaction  We are responsible for committing or rolling it back  Can be turned on from Connections tab in Server Properties 20 SET IMPLICIT_TRANSACTIONS ON USE AdventureWorks2012 GO UPDATE [Paerson].[Address] SET AddressLine1='SoftUni, Sofia' WHERE AddressID=2 COMMIT – this will write a change to the db SET IMPLICIT_TRANSACTIONS ON USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1='SoftUni, Sofia' WHERE AddressID=2 ROLLBACK – this will not write a change to the db

Explicit Transactions 21  A transaction in which start and end of transaction is explicitly declared  BEGIN TRANSACTION  COMMIT TRANSACTION OR ROLLBACK TRANSACTION  XACT_ABORT ON/OFF – control the rollback behavior SET XACT_ABORT ON – if run time error is generated everything is rolled back USE AdventureWorks2012 GO BEGIN TRANSACTION FundsTransfer GO EXEC HumanResources.DebitAccount '100', 'account1'; EXEC HumanResources.CreditAccount '100', 'account2'; COMMIT TRANSACTION;

Managing Transactions in SQL

Locks and SQL Server Concurrency

Methods of Concurrency Control  Pessimistic  Data is locked to prevent concurrency problems  SQL Server uses locks, causes blocks and who said deadlocks?  Optimistic  SQL Server generates versions for everyone, but the updates… 24

What Are Locks and What is Locking? 25  Lock – internal memory structure that “tells” us what we all do with the resources inside the system  Locking – mechanism to protect the resources and guarantee consistent data

Intent Used for: Preventing incompatible locks Duration: End of the transaction Shared (S) Used for: Reading Duration: Released almost immediately (depends on the isolation level) Update (U) Used for: Preparing to modify Duration: End of the transaction or until converted to exclusive (X) Exclusive (X) Used for: Modifying Duration: End of the transaction Common Lock Types 26

Lock Compatibility  Not all locks are compatible with other locks 27 LockSharedUpdateExclusive Shared (S)  X Update (U)  XX Exclusive (X) XXX

Databas e Table Page Row Lock Hierarchy 28

Let’s Update a Row! What Do We Need? 29 Header Row USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1='SoftUni, Sofia' WHERE AddressID=2 IX S X

Methods to View Locking Information 30 Dynamic Management Views SQL Server Profiler or Extended Events Performance monitor or Activity Monitor

Troubleshooting Locking Problems

Locking and Blocking Locking and blocking are often confused!  Locking  The action of taking and potentially holding locks  Used to implement concurrency control  Blocking is result of locking!  One process needs to wait for another process to release locked resources  In a multiuser environment, there is always, always blocking!  Only a problem if it lasts too long 32

Lock Escalation >= 5000 IX Header Row S IX X X X X S X

 Switch the escalation level (per table)  AUTO – Partition-level escalation if the table is partitioned  TABLE – Always table-level escalation  DISABLE – Do not escalate until absolutely necessary  Just disable it (that’s not Nike’s “Just do it!”)  Trace flag 1211 – disables lock escalation on server level  Trace flag 1224 – disables lock escalation if 40% of the memory used is consumed Controlling Lock Escalation 34 SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}

What Are Deadlocks?  Who is victim?  Cost for Rollback  Deadlock priority – SET DEADLOCK_PRIORITY 35 Task A Task B Resource 1 Resource 2

Resolve Blocking  Keep the transactions as short as possible  No user interactions required in the middle of the transaction  Use indexes (proper ones)  Consider a server to offload some of the workloads  Choose proper isolation level 36

Locks and SQL Server Concurrency Live Demo

Transaction Isolation Levels Live Demo

Read Uncommitted SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) 39  Suggestion:  Better offload the reads or go with optimistic level concurrency! Transaction 1 Transaction 2 SELEC T UPDATE eXclusive lock Dirty read

Repeatable Read SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 40  No non-repeatable reads possible (updates during Transaction 1)  Phantom records still possible (inserts during Transaction 1) Transaction 1 S(hared ) lock SELECT UPDATE Transaction 2

Serializable  Even phantom records are not possible!  Highest pessimistic level of isolation, lowest level of concurrency 41 Transaction 1 S(hared) lock SELECT INSERT Transaction 2 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

Optimistic Concurrency 42  Based on row versioning  (stored inside tempdb ’s version store area)  No dirty, non-repeatable reads or phantom records  Every single modification is versioned even if not used  Adds 14 bytes per row  Readers do not block writers and writers do not block readers  Writers can and will block writers, this can cause conflicts!

Read Committed and Snapshot Isolation Levels  RCSI – Read Committed Snapshot Isolation Level  Statement level versioning  Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON  Snapshot Isolation Level  Transaction level versioning  Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON  Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT 43 V1 V2 Transaction 1 Transaction 2 Select in RCSI Select Select in SI

Concurrency Phenomenon in Isolation Levels 44 Level of Isolation Dirty Reads Repeatable Reads Phantom Reads Read uncommitted yesyesyes Read committed noyesyes Repeatable read nonoyes SerializablenonoNo Read Committed Snapshot nonoyes Snapshotnonono

Transaction Isolation Levels