Sofia, Bulgaria | 9-10 October Concurrency Management – ADO.NET 2.0 Presented By: Sahil Malik Presented By: Sahil Malik

Slides:



Advertisements
Similar presentations
Introduction to ReportSmith and Effective Dated Tables
Advertisements

Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Optimistic Methods for Concurrency Control By : H.T. Kung & John T. Robinson Presenters: Munawer Saeed.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Concurrency Control II
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
User Controls, Master Pages, GridView. Content User Controls Styles, Themes, Master Pages Working with Data GridView Muzaffer DOĞAN - Anadolu University2.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Transaction Management
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management Sixth Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Client-server database systems and ODBC l Client-server architecture and components l More on reliability and security l ODBC standard.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
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.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 13 Managing Databases with SQL Server 2000.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Windows Azure Tour Benjamin Day Benjamin Day Consulting, Inc.
Failsafe systems Fail by Failing to be Failsafe. Or to put it simply Don’t worry, nothing can go wrong click go wrong click go wrong click.
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.
1099 Why Use InterBase? Bill Todd The Database Group, Inc.
Sofia, Bulgaria | 9-10 October Advanced Data Access Patterns with ADO.NET 2.0 Julie Lerman The Data Farm Julie Lerman The Data Farm.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
Rui Ribeiro MCITP 2010/10/19 Index optimization … …on low peak periods V ENCONTRO DA COMUNIDADE SQLPORT.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Reactive Database Access in Scala with Slick 3
1 Transactions Chapter Transactions A transaction is: a logical unit of work a sequence of steps to accomplish a single task Can have multiple.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
1 What to do before class starts??? Download the sample database from the k: drive to the u: drive or to your flash drive. The database is named “FormBelmont.accdb”
Views Lesson 7.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
Concurrency Control. Objectives Management of Databases Concurrency Control Database Recovery Database Security Database Administration.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. Adding a new field 1Right click the table name and select design view 2Type the field information at the end.
Transactions and Locks A Quick Reference and Summary BIT 275.
Mauricio Featherman, Ph.D. Washington St. University
DEV383 The ADO.NET DataSet and You Jackie Goldstein General Manager Renaissance Computer Systems
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
DataSets Demystified. Who I am Shawn Wildermuth Senior Consultant with Magenic Technologies ( C# MVP INETA.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
Sofia, Bulgaria | 9-10 October SQL Server Profiler Richard Campbell.
Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Sofia, Bulgaria | 9-10 October SQL Querying Tips & Techniques Richard Campbell.
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.
For more course tutorials visit CIS 336 All iLabs Week 1 to Week 7 Devry University CIS 336: All iLabs Week 1 to Week 7: Devry University.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
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
DAT 390 Advanced ADO.NET Programming Techniques Jackie Goldstein Renaissance Computer Systems
ASP.NET Programming with C# and SQL Server First Edition
Let Me Finish... Isolating Write Operations
Isolation Levels Understanding Transaction Temper Tantrums
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
From 4 Minutes to 8 Seconds in an Hour (or a bit less)
Fundamentals of Databases
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
Transactions and Concurrency
Deadlocks Everything you ever wanted to ask but were too shy
Chapter 11 Managing Databases with SQL Server 2000
Isolation Levels Understanding Transaction Temper Tantrums
Presentation transcript:

Sofia, Bulgaria | 9-10 October Concurrency Management – ADO.NET 2.0 Presented By: Sahil Malik Presented By: Sahil Malik

Sofia, Bulgaria | 9-10 October About Me ●Microsoft MVP (Visual C#), INETA speaker, telerik Technical Evangelist ●Author three books. ●Reviewer on several. ●Available for technical consulting or training for your organization. ●Contact me at (that’s smart”S” with an “S”) ●Microsoft MVP (Visual C#), INETA speaker, telerik Technical Evangelist ●Author three books. ●Reviewer on several. ●Available for technical consulting or training for your organization. ●Contact me at (that’s smart”S” with an “S”)

Sofia, Bulgaria | 9-10 October What is a concurrency conflict?

Sofia, Bulgaria | 9-10 October What is a concurrency conflict?

Sofia, Bulgaria | 9-10 October What is a concurrency conflict?

Sofia, Bulgaria | 9-10 October Database Example PersonIDExpenditure 1$ PersonIDExpenditure 1$ PersonIDExpenditure 1$ Spend $ 20 - Twice

Sofia, Bulgaria | 9-10 October Solution ●Send in only the change – let the database do the addition? ●Good approach – but might not solve every situation. ●Send in only the change – let the database do the addition? ●Good approach – but might not solve every situation.

Sofia, Bulgaria | 9-10 October Concurrency Resolution ●Design the database right. ●Staging Area ●Journaling database ●Always unique primary keys ●Request a number of keys in advance ●Use GUIDs ●Use a Seed Generator Table ●This prevents conflicts, but does not eliminate them. ●Design the database right. ●Staging Area ●Journaling database ●Always unique primary keys ●Request a number of keys in advance ●Use GUIDs ●Use a Seed Generator Table ●This prevents conflicts, but does not eliminate them.

Sofia, Bulgaria | 9-10 October Conflict happens !! ●Pessimistic Locking ●Optimistic Concurrency Control ●Pessimistic Locking ●Optimistic Concurrency Control

Sofia, Bulgaria | 9-10 October Pessimistic Locking

Sofia, Bulgaria | 9-10 October Pessimistic Locking - Database Select Finger From GirlFriends HOLDLOCK Where GirlfriendName = ‘Jane’ Select Finger From GirlFriends HOLDLOCK Where GirlfriendName = ‘Jane’

Sofia, Bulgaria | 9-10 October Pessimistic Locking – ADO.NET ●Isolation Level = RepeatableRead or ●Isolation Level = Serializable ●Or just execute the HOLDLOCK command. ●Isolation Level = RepeatableRead or ●Isolation Level = Serializable ●Or just execute the HOLDLOCK command.

Sofia, Bulgaria | 9-10 October Pessimistic Locking - Advantages ●You can be sure nobody touched your record. ●Apparently simple programming model ●Intuitive approach – but don’t do it !!! ●SERIOUSLY !! DON’T DO IT !! ●But Why? ●You can be sure nobody touched your record. ●Apparently simple programming model ●Intuitive approach – but don’t do it !!! ●SERIOUSLY !! DON’T DO IT !! ●But Why?

Sofia, Bulgaria | 9-10 October Pessimistic Locking - Downsides ●The girl can’t date anybody else – nobody else can access that database row. ●Erick could have died in a war – the program user could have locked a row and left for lunch, who unlocks the row? ●Deadlocks ●A serious hit on performance. ●The girl can’t date anybody else – nobody else can access that database row. ●Erick could have died in a war – the program user could have locked a row and left for lunch, who unlocks the row? ●Deadlocks ●A serious hit on performance.

Sofia, Bulgaria | 9-10 October Pessimistic Locking - Alternatives ●If you must use pessimistic locking, instead try and use – ●Server side cursors. Position the cursor on the row you wish to update, and thus always work with the latest possible value. ●Create an IsLocked column, and have application logic read that column and act accordingly. Similar to checkin/checkout. ●You are technically disconnected this way, but you still have to worry about timeouts, and unexpected application crashes, but at least the performance is better. ●If you must use pessimistic locking, instead try and use – ●Server side cursors. Position the cursor on the row you wish to update, and thus always work with the latest possible value. ●Create an IsLocked column, and have application logic read that column and act accordingly. Similar to checkin/checkout. ●You are technically disconnected this way, but you still have to worry about timeouts, and unexpected application crashes, but at least the performance is better.

Sofia, Bulgaria | 9-10 October Optimistic Concurrency ●Various flavors of optimistic concurrency exist. ●Primary Keys a.k.a. Last In Wins !! ●Primary Keys + Changed Columns ●Primary Keys + All Columns ●Primary Keys + Timestamp/rowversion ●Show me some code man !! ●Various flavors of optimistic concurrency exist. ●Primary Keys a.k.a. Last In Wins !! ●Primary Keys + Changed Columns ●Primary Keys + All Columns ●Primary Keys + Timestamp/rowversion ●Show me some code man !!

Sofia, Bulgaria | 9-10 October Optimistic Concurrency ●Let us examine each concurrency model one by one. ●First we need a database structure. ●Three tables, hierarchically arranged ●Animals ●Pets ●PetBelonging ●Just run Sql\database setup script.sql ●Let us examine each concurrency model one by one. ●First we need a database structure. ●Three tables, hierarchically arranged ●Animals ●Pets ●PetBelonging ●Just run Sql\database setup script.sql

Sofia, Bulgaria | 9-10 October Database Diagram

Sofia, Bulgaria | 9-10 October Code Example #1 ●Create an application using Drag Drop that works with the Pets table. ●Run two instances, execute parallel inserts ●You will see that the primary key is automatically resolved. ●Next run two instances, and execute parallel updates. ●You should get a concurrency violation. ●Demonstrate the various commands being generated by the TableAdapter ●Create an application using Drag Drop that works with the Pets table. ●Run two instances, execute parallel inserts ●You will see that the primary key is automatically resolved. ●Next run two instances, and execute parallel updates. ●You should get a concurrency violation. ●Demonstrate the various commands being generated by the TableAdapter

Sofia, Bulgaria | 9-10 October PetsTableAdapter.InitAdapter ●Demonstrate the DeleteCommand, InsertCommand, UpdateCommand texts in PetsTableAdapter.InitAdapter ●These commands check for the primary key and all columns involved. ●The most database portable concurrency check. ●First in wins !! ●Not too efficient. ●Demonstrate the DeleteCommand, InsertCommand, UpdateCommand texts in PetsTableAdapter.InitAdapter ●These commands check for the primary key and all columns involved. ●The most database portable concurrency check. ●First in wins !! ●Not too efficient.

Sofia, Bulgaria | 9-10 October Optimistic Concurrency – Only PK ●Check only for primary keys. ●Effectively, don’t check for concurrency. ●Check only for primary keys. ●Effectively, don’t check for concurrency.

Sofia, Bulgaria | 9-10 October AnimalIDAnimalTypeAnimalWeight 1Puppy3 lbs AnimalIDAnimalTypeAnimalWeight 1Dog50 lbs AnimalIDAnimalTypeAnimalWeight 1Mutt50 lbs Database

Sofia, Bulgaria | 9-10 October AnimalIDAnimalTypeAnimalWeight 1Dog50 lbs AnimalIDAnimalTypeAnimalWeight 1Mutt50 lbs Database

Sofia, Bulgaria | 9-10 October Optimistic Concurrency – Only PK -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 -- Frans’ update query Update Animals Set AnimalType = 'Mutt', AnimalWeight = '50 lbs' where AnimalID = 1 -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 -- Frans’ update query Update Animals Set AnimalType = 'Mutt', AnimalWeight = '50 lbs' where AnimalID = 1

Sofia, Bulgaria | 9-10 October Optimistic Concurrency – Only PK ●Database portable, but doesn’t do anything to prevent data corruption. ●Good performance (because it doesn’t do anything). ●Database portable, but doesn’t do anything to prevent data corruption. ●Good performance (because it doesn’t do anything).

Sofia, Bulgaria | 9-10 October OC – PK + Changed Columns ●This approach checks the PK and all changed columns. ●It is database portable. ●But it does not guarantee reliability in all situations. ●This approach checks the PK and all changed columns. ●It is database portable. ●But it does not guarantee reliability in all situations.

Sofia, Bulgaria | 9-10 October AnimalIDAnimalTypeAnimalWeight 1Puppy3 lbs AnimalIDAnimalTypeAnimalWeight 1Dog50 lbs 1Puppy3 lbs AnimalIDAnimalTypeAnimalWeight 1Puppy60 lbs 1Puppy3 lbs Database

Sofia, Bulgaria | 9-10 October AnimalIDAnimalTypeAnimalWeight 1Dog50 lbs 1Puppy3 lbs AnimalIDAnimalTypeAnimalWeight 1Puppy60 lbs 1Puppy3 lbs Database

Sofia, Bulgaria | 9-10 October OC –PK + Changed Columns -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' and AnimalType = 'Puppy' -- Frans’ update query, this will now fail. Update Animals Set AnimalWeight = '60 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' and AnimalType = 'Puppy' -- Frans’ update query, this will now fail. Update Animals Set AnimalWeight = '60 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs'

Sofia, Bulgaria | 9-10 October OC – PK + Changed Columns ●Better performance than checking all columns. ●Database portable. ●But this approach has a sinister hole !!! ●Better performance than checking all columns. ●Database portable. ●But this approach has a sinister hole !!!

Sofia, Bulgaria | 9-10 October -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' and AnimalType = 'Puppy' -- Frans’ update query, this will now fail. Update Animals Set AnimalWeight = '60 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' -- Erick’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Erick’s update query Update Animals Set AnimalType = 'Dog', AnimalWeight = '50 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' and AnimalType = 'Puppy' -- Frans’ update query, this will now fail. Update Animals Set AnimalWeight = '60 lbs' where AnimalID = 1 and AnimalWeight = '3 lbs' So what does Frans do next?

Sofia, Bulgaria | 9-10 October -- Frans’ Select query (Second pass) Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ Select query (Second pass) Select AnimalID, AnimalType, AnimalWeight from Animals -- Frans’ update query Update Animals AnimalWeight = '60 lbs' where AnimalID = 1 and AnimalWeight = '50 lbs' -- Sushil’s Select query Select AnimalID, AnimalType, AnimalWeight from Animals -- Sushil’s Update query Update Animals Set AnimalType = 'Monkey' where AnimalID = 1 and AnimalType = 'Dog' -- Pablo’s Select query (Second pass) Select AnimalID, AnimalType, AnimalWeight from Animals

Sofia, Bulgaria | 9-10 October Final Results Erick – Dog 50 lbs Sushil – Monkey 50 lbs Frans – Dog 60 lbs

Sofia, Bulgaria | 9-10 October Final Actual Database Results Pablo – Monkey – 60 lbs

Sofia, Bulgaria | 9-10 October Optimistic Concurrency - Timestamps ●Sql Server specific feature. ●Sql server supports a column type called timestamp. ●Timestamp is actually rowversion ●T-SQL Timestamp = SQL-92 standard’s datetime. ●One day in the future microsoft may decide to deprecate timestamp and ask you to use rowversion instead. ●Sql Server specific feature. ●Sql server supports a column type called timestamp. ●Timestamp is actually rowversion ●T-SQL Timestamp = SQL-92 standard’s datetime. ●One day in the future microsoft may decide to deprecate timestamp and ask you to use rowversion instead.

Sofia, Bulgaria | 9-10 October Code demonstration ●Show how the following work ●UpdateCommand ●DeleteCommand (Beta2 bug) ●InsertCommand ●Show the significance of ●AcceptChangesDuringUpdate ●ContinueUpdateOnError ●Show how the following work ●UpdateCommand ●DeleteCommand (Beta2 bug) ●InsertCommand ●Show the significance of ●AcceptChangesDuringUpdate ●ContinueUpdateOnError

Sofia, Bulgaria | 9-10 October Code Demonstration ●Hierarchical Updates and Hierarchical update concerns. ●Insert Order ●Update Order ●Delete Order ●Concurrency in Hierarchical updates. ●Hierarchical Updates and Hierarchical update concerns. ●Insert Order ●Update Order ●Delete Order ●Concurrency in Hierarchical updates.

Sofia, Bulgaria | 9-10 October End Notes ●Thank you !! ●Questions? ●Contact me – ● ●Thank you !! ●Questions? ●Contact me – ●