{"id":8269,"date":"2015-09-03T12:19:16","date_gmt":"2015-09-03T12:19:16","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/locking-objects-in-sql-server\/"},"modified":"2016-07-28T10:56:56","modified_gmt":"2016-07-28T10:56:56","slug":"locking-objects-in-sql-server","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/locking-objects-in-sql-server\/","title":{"rendered":"Locking objects in SQL Server"},"content":{"rendered":"<p>As my colleague James said in a recent <a href=\"https:\/\/www.simple-talk.com\/blogs\/2015\/06\/29\/locking-objects-in-sql-source-control\/\">blog post<\/a>:<\/p>\n<p>&#8220;If you share a database with other developers, chances are you&#8217;ve encountered a situation like this:<\/p>\n<p>1. Alice starts editing an object.<\/p>\n<p>2. Meanwhile, not realizing Alice is working on the object, Bob unwittingly edits and saves the same object.<\/p>\n<p>3. Alice, not realizing Bob was working on the same thing, saves her changes.<\/p>\n<p>Presto: Bob&#8217;s work is gone. And what&#8217;s more, Bob and Alice might not even notice. The error could make it all the way to production.&#8221;<\/p>\n<p>It&#8217;s a familiar story. Two developers are working on a database, they&#8217;re using version control, and one of them unwittingly overwrites the others&#8217; changes. I&#8217;ve heard first how frustrating this can be &#8211; we spoke to one developer who lost a week&#8217;s work when their database was deployed without critical changes that had been accidentally overwritten. Ouch.<\/p>\n<p><strong>A bit of background:<\/strong> In 2013, we released Source Control for Oracle 2, which included a feature to let users lock objects on Oracle databases so that other users can&#8217;t edit them. A while later, we released a free beta of <a href=\"https:\/\/www.simple-talk.com\/blogs\/2015\/02\/26\/the-sql-lock-product-story\/?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_campaign=sqlsourcecontrol&amp;utm_content=SQLSourceControlv4_aburrows\">SQL Lock<\/a>, an add-in for Management Studio, to try and solve the same problem for SQL Server development teams. We&#8217;ve recently retired the SQL Lock beta and have rolled the feature into <a href=\"http:\/\/www.red-gate.com\/products\/sql-development\/sql-source-control\/?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_campaign=sqlsourcecontrol&amp;utm_content=SQLSourceControlv4_aburrows\">SQL Source Control<\/a>. In case you&#8217;re not familiar with the latter, SQL Source Control is an add-in for SQL Server Management Studio that links your database to your version control system.<\/p>\n<p>So, how do you use object locking? Simple. Once you&#8217;ve set it up and you&#8217;re ready to work on an object, just right-click it in the Object Explorer and select <strong>Lock this object<\/strong>.<\/p>\n<p> <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/lock-this-object.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6913\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/lock-this-object.png\" width=\"445\" height=\"308\" alt=\"lock-this-object.png\" \/><\/a><\/p>\n<p>When you&#8217;ve locked it, the object is marked with a padlock icon in the Object Explorer.<\/p>\n<p><a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/Capture1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6916\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/Capture1.jpg\" width=\"266\" height=\"166\" alt=\"Capture1.jpg\" \/><\/a><\/p>\n<p>If someone else tries to save changes to the object, they&#8217;ll see an error explaining that it&#8217;s locked by you.<\/p>\n<p>Of course, if you take a week off work or get hit by a bus, your team&#8217;s going to need to work on your locked objects without you. For that reason, anyone can unlock an object, even if they didn&#8217;t lock it themselves. This way the team can continue working on the object without any delays. We see the feature as not being a way to enforce rules, but something to help you to not tread on each other&#8217;s toes.<\/p>\n<p>If you go to the Object locking tab you can see all of the objects which have been locked on that server &#8211; select the ones which you&#8217;d like to unlock and click <strong>Unlock selected objects<\/strong>.<\/p>\n<p> <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/Locking-tab-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6926\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2015\/08\/Locking-tab-2.jpg\" width=\"710\" height=\"209\" alt=\"Locking-tab-2.jpg\" \/><\/a><\/p>\n<p>All the locking is done on the server, so it works independently of your version control system.<\/p>\n<p>If you&#8217;re more of a video person than a blog person then you can watch this walk-through video of the locking feature by Microsoft MVP Alessandro Alpi:<\/p>\n<\/p>\n<p>If you&#8217;d like to try object locking out you&#8217;ll need to install the latest version of SQL Source Control, which has just been released &#8211; hurrah!. <a href=\"http:\/\/www.red-gate.com\/dynamic\/products\/sql-development\/sql-source-control\/download?utm_source=simpletalk&amp;utm_medium=publink&amp;utm_campaign=sqlsourcecontrol&amp;utm_content=SQLSourceControlv4_aburrows\">Download it here<\/a> and feel comforted that your work is in safe hands. Oh, and let me know what you think &#8211; if you have any feedback I&#8217;d love to hear it.<\/p>\n<p><strong>What&#8217;s next for SQL Source Control? <\/strong>Over the next 6 months you&#8217;ll see a lot of changes and improvements to SQL Source Control. We&#8217;ll be posting here soon about what&#8217;s coming up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As my colleague James said in a recent blog post: &#8220;If you share a database with other developers, chances are you&#8217;ve encountered a situation like this: 1. Alice starts editing an object. 2. Meanwhile, not realizing Alice is working on the object, Bob unwittingly edits and saves the same object. 3. Alice, not realizing Bob&#8230;&hellip;<\/p>\n","protected":false},"author":8857,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-8269","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/8857"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=8269"}],"version-history":[{"count":30,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8269\/revisions"}],"predecessor-version":[{"id":42511,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8269\/revisions\/42511"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8269"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}