﻿{"id":286,"date":"2014-10-18T17:30:18","date_gmt":"2014-10-18T16:30:18","guid":{"rendered":"http:\/\/www.codereview.co\/?p=286"},"modified":"2014-10-15T14:50:50","modified_gmt":"2014-10-15T13:50:50","slug":"sql-interview-question-5-what-are-pessimistic-and-optimistic-locks","status":"publish","type":"post","link":"http:\/\/www.codereview.co\/index.php\/sql-server-tutorials\/sql-interview-question-5-what-are-pessimistic-and-optimistic-locks\/","title":{"rendered":"Sql Interview Question #6: What are pessimistic and optimistic locks?"},"content":{"rendered":"<p><span style=\"color: #000000;\"><strong>Sql Interview Question<\/strong> <\/span><\/p>\n<p><span style=\"color: #000000;\">What are pessimistic and optimistic locks?<\/span><!--more--><\/p>\n<p><span style=\"color: #000000;\"><strong>Solution<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Optimistic locking <\/strong>is a strategy where you read a record, take note of a version number and check that the version hasn&#8217;t changed before you write the record back. If the record is changed (i.e. a different version to yours), then you abort the transaction and the user can re-start the transaction with the new data and update appropriately.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Pessimistic locking <\/strong>is when you lock the record for your exclusive use until you have finished with it. There are 4 levels of locking in the pessimistic isolation levels from lowest to highest. They are: read uncommitted, read committed, repeatable read, and serializable. At the serializable level (the highest locking and isolation level) it has much better integrity than optimistic locking but requires you to be careful with your application design to avoid deadlocks.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><strong>Thanks for reading this article,<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Next steps :<\/strong><\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><strong>Share this with your colleagues because Sharing is Learning<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Comment below if you need any assistance<\/strong><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Sql Interview Question What are pessimistic and optimistic locks?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[172,3],"tags":[146,145,144,13,12],"_links":{"self":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/286"}],"collection":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":5,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":371,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/286\/revisions\/371"}],"wp:attachment":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}