{"id":917,"date":"2005-08-25T06:55:00","date_gmt":"2005-08-25T06:55:00","guid":{"rendered":"\/blogs\/bobb\/post\/SQLCLR-transaction-nesting-and-TransactionScope.aspx"},"modified":"2005-08-25T06:55:00","modified_gmt":"2005-08-25T06:55:00","slug":"sqlclr-transaction-nesting-and-transactionscope","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/","title":{"rendered":"SQLCLR, transaction nesting, and TransactionScope"},"content":{"rendered":"<p>\nAhh&#8230;enough shameless self-promotion (for a while), let&#39;s talk transactions.\n<\/p>\n<p>\nI worked in the past with folks who like to compose transactions. SQL Server supports nesting of transactions and named savepoints but not autonomous transactions. So\n<\/p>\n<p>\nCREATE PROCEDURE X<br \/>\nAS<br \/>\nBEGIN TRAN<br \/>\n&#8212; work here<br \/>\nCOMMIT\n<\/p>\n<p>\ncalling it standalone means the work is in a transaction. Calling it from procedure Y:\n<\/p>\n<p>\nCREATE PROCEDURE Y<br \/>\nAS<br \/>\nBEGIN TRAN<br \/>\n&#8212; other work here<br \/>\nEXECUTE X<br \/>\nCOMMIT\n<\/p>\n<p>\ndoesn&#39;t start an autonomous transaction, the BEGIN TRAN in X merely ups @@TRANCOUNT by 1. Interesting things happen when you roll back X while its being called by Y.\n<\/p>\n<p>\nI&#39;d like to emulate this behavior in SQLCLR, i.e. have a procedure that acts like X, and can be used standalone or composed. I can do something akin to T-SQL (and get the interesting rollback behavior with a slightly different error number) using the BeginTransaction method on the context SqlConnection. I&#39;d heard awhile ago that System.Transactions used inside of SQLCLR would &quot;always do the right thing&quot;. AND because of the way promotable transactions work, it would compose a context SqlConnection in the SAME local transaction. A la T-SQL or SqlConnection.BeginTransaction().\n<\/p>\n<p>\nIt doesn&#39;t do this. If I have a SQLCLR proc that looks like this (condensed version):\n<\/p>\n<p>\npublic static void X {<br \/>\nusing (TransactionScope ts = new TransactionScope())<br \/>\nusing (SqlConnection conn = new SqlConnection(&quot;Context connection=true&quot;))<br \/>\n{<br \/>\n&nbsp; conn.Open();<br \/>\n&nbsp; ts.Complete();<br \/>\n}<br \/>\n}\n<\/p>\n<p>\nIf SQLCLR X is used standalone, all well and good, local transaction. If SQLCLR X is called from procedure Y (above) then SqlConnection.Open() starts a *distributed* transaction. Apparently it&nbsp;HAS to be this way,&nbsp;at least for now,&nbsp;because of how TransactionScope works.\n<\/p>\n<p>\nIf you WANT a distributed transaction composed with your outer transaction (your SqlConnection is calling to another instance for example),&nbsp;USE TransactionScope, if you DON&#39;T want one, use SqlConnection.BeginTransaction. It won&#39;t act any different from T-SQL (except you do get a different error number) if you roll back inside an inner transaction. But you get a nesting *local* transaction with BeginTransaction.\n<\/p>\n<p>\nBTW just is case you wondered if SQLCLR X proc could do this:\n<\/p>\n<p>\nusing (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))\n<\/p>\n<p>\nand go for that elusive &quot;autonomous transaction&quot;, don&#39;t do it. You&#39;ll get a message saying &quot;no autonomous transaction. Because SQL Server doesn&#39;t support autonomous on a single connection. SQLCLR or not. There is the two connection case, but that&#39;s a story for another day.\n<\/p>\n<p>\nThis was as compressed a blog entry as I could make it, but was still quite long. Any questions?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ahh&#8230;enough shameless self-promotion (for a while), let&#39;s talk transactions. I worked in the past with folks who like to compose transactions. SQL Server supports nesting of transactions and named savepoints but not autonomous transactions. So CREATE PROCEDURE X AS BEGIN TRAN &#8212; work here COMMIT calling it standalone means the work is in a transaction. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,38],"tags":[],"class_list":["post-917","post","type-post","status-publish","format-standard","hentry","category-data-access","category-sqlclr"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQLCLR, transaction nesting, and TransactionScope - 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\/sqlclr-transaction-nesting-and-transactionscope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLCLR, transaction nesting, and TransactionScope - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Ahh&#8230;enough shameless self-promotion (for a while), let&#039;s talk transactions. I worked in the past with folks who like to compose transactions. SQL Server supports nesting of transactions and named savepoints but not autonomous transactions. So CREATE PROCEDURE X AS BEGIN TRAN &#8212; work here COMMIT calling it standalone means the work is in a transaction. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2005-08-25T06:55:00+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=\"2 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\/sqlclr-transaction-nesting-and-transactionscope\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/\",\"name\":\"SQLCLR, transaction nesting, and TransactionScope - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2005-08-25T06:55:00+00:00\",\"dateModified\":\"2005-08-25T06:55:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Access\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQLCLR, transaction nesting, and TransactionScope\"}]},{\"@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":"SQLCLR, transaction nesting, and TransactionScope - 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\/sqlclr-transaction-nesting-and-transactionscope\/","og_locale":"en_US","og_type":"article","og_title":"SQLCLR, transaction nesting, and TransactionScope - Bob Beauchemin","og_description":"Ahh&#8230;enough shameless self-promotion (for a while), let&#39;s talk transactions. I worked in the past with folks who like to compose transactions. SQL Server supports nesting of transactions and named savepoints but not autonomous transactions. So CREATE PROCEDURE X AS BEGIN TRAN &#8212; work here COMMIT calling it standalone means the work is in a transaction. [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/","og_site_name":"Bob Beauchemin","article_published_time":"2005-08-25T06:55:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/","name":"SQLCLR, transaction nesting, and TransactionScope - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2005-08-25T06:55:00+00:00","dateModified":"2005-08-25T06:55:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sqlclr-transaction-nesting-and-transactionscope\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Data Access","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/"},{"@type":"ListItem","position":3,"name":"SQLCLR, transaction nesting, and TransactionScope"}]},{"@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\/917","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=917"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}