{"id":1723,"date":"2013-07-07T15:50:23","date_gmt":"2013-07-07T22:50:23","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/bobb\/?p=1723"},"modified":"2013-07-07T15:50:23","modified_gmt":"2013-07-07T22:50:23","slug":"a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/","title":{"rendered":"A more complex discussion of user transactions and memory-optimized tables"},"content":{"rendered":"<p>In the last blog post, I tried out a simple example to show the difference between the three transaction isolation levels supported by memory-optimized tables. I only used user-transactions in T-SQL, and only showed the behavior of transactions consisting of SELECT statements, when INSERT\/UPDATE\/DELETE activity was taking place in other sessions running concurrently. But of course, things are not always *that* simple, and there are differences regarding:<br \/>\n1. Transactions that do concurrent INSERT\/UPDATE\/DELETE in multiple sessions<br \/>\n2. Transactions running in or using compiled stored procedures (which must be defined as atomic and declare their isolation level)<br \/>\n3. Autocommit transactions (i.e. single atomic SQL statements)<\/p>\n<p>Today, I&#8217;ll expand the discussion to cover some additional cases.<\/p>\n<p>One thing I did mention in the <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/a-gentle-introduction-to-transaction-isolation-levels-and-hekaton-tables\/\" class=\"broken_link\">previous post<\/a> is WHEN a user transaction fails. The <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/a-gentle-introduction-to-transaction-isolation-levels-and-hekaton-tables\/\" class=\"broken_link\">previous post<\/a> originally mentioned (as a simplification) that transactions with memory-optimized tables &#8220;&#8230;fail the user transaction at commit time, if there\u2019s a conflict detected. SQL Server\u2019s SNAPSHOT against &#8220;traditional&#8221; tables fail during user transactions at the statement where the conflict is detected.&#8221;<\/p>\n<p>That&#8217;s not always true with concurrent *actions* in a user transaction. User transactions can fail at different points in a transactions lifetime, that is:<\/p>\n<p>1. Normal Processing phase &#8211; during the lifetime of a transaction<br \/>\n2. Preparation phase &#8211; transaction decides whether it can commit or must abort<\/p>\n<p>According to the definition in the whitepaper &#8220;<a href=\"http:\/\/vldb.org\/pvldb\/vol5\/p298_per-akelarson_vldb2012.pdf\">High-Performance Concurrency Control Mechanisms for Main-Memory Databases<\/a>&#8221; (sections 2 and 3), there are a total of 3 transaction processing phases:<\/p>\n<p>Tx created<br \/>\n1. Normal Processing<br \/>\n2. Preparation<br \/>\n3. Postprocessing<br \/>\nTx terminated<\/p>\n<p>The preparation phase concludes with writes to the transaction log, if the transaction commits. The postprocessing phase consists of timestamp fix up, whether the transaction commits or aborts. It&#8217;s also good to remember that, in a T-SQL user transaction, the transaction begins with the first statement after &#8220;BEGIN TRANSACTION&#8221; that touches data, rather than during the T-SQL &#8220;BEGIN TRANSACTION&#8221; statement.<\/p>\n<p>All of the conditions I looked at last week failed in the preparation phase.<\/p>\n<p>It&#8217;s obvious that, if two concurrent INSERTS with the same primary key happen, one of them must fail. If the later insert happens during a transaction, that transaction fails at commit time (in the preparation phase), rather than normal processing time.<\/p>\n<p>begin tran insert dbo.t1 with (snapshot) values(5, &#8216;Fred&#8217;)<br \/>\n&#8212; insert row with same primary key in another session and commit other session (insert, commit in other session works)<br \/>\ncommit &#8212; Msg 41325, Level 16, State 0, Line 66 The current transaction failed to commit due to a serializable validation failure.<\/p>\n<p>However, WRITE conflicts (transaction updates or deletes a row already updated\/deleted by another session AFTER the transaction starts) fail at normal processing time. This is what we&#8217;re used to with conflict detection in SNAPSHOT isolation level with &#8220;ordinary&#8221; SQL Server tables. The previous post is rephrased from the simplified original &#8220;&#8230;fail at commit time&#8221; to &#8220;&#8230;CAN fail at commit time&#8221; to indicate that write conflicts fail the way that we&#8217;re &#8220;used to&#8221;.<\/p>\n<p>begin tran<br \/>\nselect * from dbo.t2 with (snapshot) &#8212; starts the transaction<br \/>\n&#8212; update row 2 in another transaction and commit other session<br \/>\nupdate dbo.t1 with (snapshot) set c2 = &#8216;Sam&#8217; where c1 = 2<br \/>\n&#8212; Msg 41302, Level 16, State 110, Line 120<br \/>\n&#8212; The current transaction attempted to update a record that has been updated since this transaction started. The transaction was aborted.<br \/>\n&#8212; Msg 3998, Level 16, State 1, Line 120<br \/>\n&#8212; Uncommittable transaction is detected at the end of the batch. The transaction is rolled back.<br \/>\ncommit &#8212; transaction already rolled back (above)<\/p>\n<p>begin tran<br \/>\nselect * from dbo.t2 with (snapshot) &#8212; starts the transaction<br \/>\n&#8212; update row 2 in another transaction and commit<br \/>\ndelete dbo.t1 with (snapshot) where c1 = 2<br \/>\n&#8212; Msg 41302, Level 16, State 111, Line 130<br \/>\n&#8212; The current transaction attempted to update a record that has been updated since this transaction started. The transaction was aborted.<br \/>\n&#8212; Msg 3998, Level 16, State 1, Line 130<br \/>\n&#8212; Uncommittable transaction is detected at the end of the batch. The transaction is rolled back.<br \/>\n&#8212; The statement has been terminated.<br \/>\ncommit &#8212; transaction already rolled back (above)<\/p>\n<p>Just to confirm what I mentioned before, the following shows that the transaction starts when the first data is touched, rather than at BEGIN TRANSACTION. If I remove the SELECT from (unrelated) table dbo.t2 and update and commit row 2 in another session before accessing data at all, the transaction is consistent (you&#8217;re just updated already updated\/committed data after the transaction starts) and the UPDATE succeeds.<\/p>\n<p>begin tran<br \/>\n&#8212; update row 2 in another transaction and commit<br \/>\nupdate dbo.t1 with (snapshot) set c2 = &#8216;Sam&#8217; where c1 = 2 &#8212; tx starts here, 2 is already updated\/commited to new value<br \/>\ncommit &#8212; OK<\/p>\n<p>In traditional SNAPSHOT isolation, 2 INSERTers with the same primary key or 2 UPDATE\/DELETEers when both sessions are in transactions cause the transaction with the last action to wait, but with multi-version memory-optimized tables, we don&#8217;t want to wait, and rather than wait transactions can take dependencies on other transactions, continuing but assuming the transactions &#8220;before&#8221; them will commit. These dependencies are resolved by waiting, if needed, during the preparation phase, rather than doing normal processing. A best practice with memory-optimized tables is to keep transactions short, to minimize waits for transaction dependencies. Refer to the <a href=\"http:\/\/vldb.org\/pvldb\/vol5\/p298_per-akelarson_vldb2012.pdf\">whitepaper<\/a> for a description of conditions that can cause transaction dependencies.<\/p>\n<p>There are places where reading or writing inside a transaction can cause other sessions to wait, however. In repeatable read isolation level and above, but not in snapshot isolation level, a SELECT statement on a memory-optimized table WITH A T-SQL STATEMENT within a T-SQL user transaction will cause another session attempting to DROP the table to wait.<\/p>\n<p>begin tran<br \/>\nselect * from dbo.t1 with (snapshot)<br \/>\n&#8212; drop table t1 in the other session, it works<br \/>\nselect * from dbo.t1 with (snapshot) &#8212; invalid object name dbo.t1<br \/>\ncommit &#8212; OK<\/p>\n<p>begin tran<br \/>\nselect * from dbo.t1 with (repeatableread)<br \/>\n&#8212; drop table t1 in the other session, this waits<br \/>\nselect * from dbo.t1 with (repeatableread) &#8212; OK<br \/>\ncommit &#8212; OK, (and drop works after this tx committed)<\/p>\n<p>However, reads from within a repeatable read isolation level COMPILED PROCEDURE and writes (INSERT\/UPDATE\/DELETE) from within a COMPILED PROCEDURE at any isolation level allow a DROP TABLE from another session statement to work, and cause a validation error at preparation time. Writes in a T-SQL statement at any isolation level cause the same validation error.<\/p>\n<p>begin tran<br \/>\ninsert into dbo.t1 with (snapshot) values(5, &#8216;Fred&#8217;)<br \/>\n&#8212; drop table t1 in the other session &#8211; the drop statement hangs<br \/>\ncommit &#8212; OK, (and drop works after this tx committed)<br \/>\ngo<\/p>\n<p>create procedure dbo.insert_t1(@c1 int, @c2 varchar(10))<br \/>\nwith native_compilation, schemabinding, execute as owner<br \/>\nas begin atomic with (transaction isolation level=snapshot, language=N&#8217;us_english&#8217;)<br \/>\ninsert into dbo.t1 values(@c1, @c2)<br \/>\nend<br \/>\ngo<\/p>\n<p>begin tran<br \/>\nexecute dbo.insert_t1 5, &#8216;Fred&#8217;<br \/>\n&#8212; drop procedure dbo.insert_t1 and dbo.insert_t1 and table t1 in the other session (this works without hanging\/waiting)<br \/>\n&#8212;<br \/>\n&#8212; note that we must drop the procedure (and any other procedures that reference table t1)<br \/>\n&#8212; first, because compiled procedures are schemabound.<br \/>\n&#8212; attempting to drop the table without dropping the procedure would fail with a schemabinding error.<br \/>\ncommit &#8212; Msg 41305, Level 16, State 0, Line 211 The current transaction failed to commit due to a repeatable read validation failure.<\/p>\n<p>So we&#8217;ve succeeded in muddying the waters a bit by illustrating that:<br \/>\n1. With memory-optimized tables, transactions can fail during normal procession as well as the preparation phase<br \/>\n2. Transactional behavior can cause different behavior in other sessions depending on whether we use a compiled procedure or use a T-SQL statement.<\/p>\n<p>With memory-optimized tables, these are not your mother&#8217;s\/father&#8217;s traditional transactions. AND&#8230;we haven&#8217;t covered all the differences and edge-cases yet, either.<\/p>\n<p>Cheers, Bob<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last blog post, I tried out a simple example to show the difference between the three transaction isolation levels supported by memory-optimized tables. I only used user-transactions in T-SQL, and only showed the behavior of transactions consisting of SELECT statements, when INSERT\/UPDATE\/DELETE activity was taking place in other sessions running concurrently. But of [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,41],"tags":[],"class_list":["post-1723","post","type-post","status-publish","format-standard","hentry","category-hekaton","category-sql-server-2014"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"In the last blog post, I tried out a simple example to show the difference between the three transaction isolation levels supported by memory-optimized tables. I only used user-transactions in T-SQL, and only showed the behavior of transactions consisting of SELECT statements, when INSERT\/UPDATE\/DELETE activity was taking place in other sessions running concurrently. But of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2013-07-07T22:50:23+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/\",\"name\":\"A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2013-07-07T22:50:23+00:00\",\"dateModified\":\"2013-07-07T22:50:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hekaton\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/hekaton\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"A more complex discussion of user transactions and memory-optimized tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/","og_locale":"en_US","og_type":"article","og_title":"A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin","og_description":"In the last blog post, I tried out a simple example to show the difference between the three transaction isolation levels supported by memory-optimized tables. I only used user-transactions in T-SQL, and only showed the behavior of transactions consisting of SELECT statements, when INSERT\/UPDATE\/DELETE activity was taking place in other sessions running concurrently. But of [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/","og_site_name":"Bob Beauchemin","article_published_time":"2013-07-07T22:50:23+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/","name":"A more complex discussion of user transactions and memory-optimized tables - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2013-07-07T22:50:23+00:00","dateModified":"2013-07-07T22:50:23+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/a-more-complex-discussion-of-user-transactions-and-memory-optimized-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Hekaton","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/hekaton\/"},{"@type":"ListItem","position":3,"name":"A more complex discussion of user transactions and memory-optimized tables"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=1723"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1723\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=1723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=1723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=1723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}