Lock, Block, and Two Smoking CPUs Implementing Application Locking with sp_getapplock I was thinking of using a few other titles, “There and Lock again, A DBA’s holiday”
Daniel R. Maenle Junior Database Administrator at ScriptPro Twitter: @DantheSQLMan Email: danmaenle@gmail.com Pharmacy for 8+ years OptumRX and ScriptPro Pharmacy IT 6+ years Junior DBA 2 years in January 2 | Introduction
Agenda Intro to our workflow (Quick Overview) Table Locking Why application locks are needed? Table Locking Switching to sp_getapplock Using sp_getapplock Demos What’s next? Q/A 3 | Agenda
Pharmacy Workflow Involved: Script Drug Insurance POS Jenny brings in prescription RX# 8675309
Our Original Method ScriptPro functionality Using an “Application_lock” table Handled table name, row_id and other lock information (application, timestamp) Insert when using / Delete when done. Queries written to poll this table Debugging from this table 5 | Table Locking
Benefits of Using Table Locking Central point of locking Easy to view locks Simplified debugging What is locked? SELECT * FROM Application_lock Transaction logging 6 | Table Locking
Downsides of Table Locking Single table contention Insert/Update/Delete schema locks Waiting on other queries before next insert/delete Isn’t really a lock! Concurrency Non-requester can delete the lock! What happened to what I was working on? 7 | Table Locking
The Project “Make our locking mechanism better” What caused this request? Blocking Duration to insert and delete was long Wait stats were showing bad indicators of table/schema contention (LCK_M_IX) 8 | Switching to sp_getapplock
The Project (2) “Make our locking mechanism better” (2) Sp_getapplock Keep current functionality No single point of contention Don’t break anything! Sp_getapplock Very promising! 9 | Switching to sp_getapplock
What is sp_getapplock? Built into SQL Server No single point of contention Not a table! Held in memory Session/Transaction MSSQLSERVER likes this 10 | Using sp_getapplock
More on sp_getapplock Why do we need locking? No timestamp or duration Concurrency Immediate resolution Timeout available No timestamp or duration Requires application development to use ACID test needs locking Shared, Update, IntentShared, IntentExclusive, or Exclusive. Value Result 0The lock was successfully granted synchronously. 1The lock was granted successfully after waiting for other incompatible locks to be released. -1The lock request timed out. -2The lock request was canceled. -3The lock request was chosen as a deadlock victim. -999Indicates a parameter validation or other call error. 11 | Using sp_getapplock
Demo of Performance Old Application_lock New style sp_getapplock Stats/duration What locks are being used? New style sp_getapplock Some oddities discovered 12 | Demo’s
DEMOS
What’s Next? To maintain a VIEW instead of a table Old procedures Query performance Lost timestamp Old habits/Troubleshooting Session mode was default Transaction mode difficulties 14 | What’s next?
Future Additions Switching to transactional locking Better management Using tempdb for less TLOG as well Code changes to use APPLOCK_MODE DELETE VIEW Continued performance tweaks 15 | What’s next?
Summary Application locking can be tricky No one best way Pro/Con Table Sp_getapplock has its uses LOCKING Able to be integrated into current code Without much pain Albatross to carry 16 | Summary
Questions? Microsoft Connect: http://bit.ly/2dpGFri Glenn Berry wait stats – http://bit.ly/2cN60te Remus Rusanu - http://bit.ly/2cSbmTp Paul Randal - http://bit.ly/2cuph2L MSSQL Server 2014 Unleashed - http://bit.ly/2cDwo6z Tech NET - http://bit.ly/2dk6FnF Email: danmaenle@gmail.com Twitter: @DantheSQLMan Microsoft Connect: http://bit.ly/2dpGFri 17 | Q/A