{"id":280,"date":"2011-06-21T19:53:15","date_gmt":"2011-06-21T19:53:15","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=280"},"modified":"2011-06-21T19:54:18","modified_gmt":"2011-06-21T19:54:18","slug":"coldfusion-session-locking-quick","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/coldfusion-session-locking-quick\/","title":{"rendered":"ColdFusion Session Locking &#8211; Keep it quick!"},"content":{"rendered":"<p>Lately I&#39;ve been noticing, while examining chunks of ColdFusion code out there, that many folks don&#39;t give much thought to what goes on inside their <a class=\"target-blank\" href=\"http:\/\/help.adobe.com\/en_US\/ColdFusion\/9.0\/CFMLRef\/WSc3ff6d0ea77859461172e0811cbec22c24-7f5d.html\"><em>&lt;cflock&gt;<\/em><\/a> tags. In the case where you&#39;re locking a session scope, so you can update session variables for example, the amount of code executed inside the lock should be as short as possible.<\/p>\n<p>As an example, take a look at this unnecessarily long lock:<\/p>\n<p><code>&lt;cflock scope=&quot;session&quot; type=&quot;readonly&quot; timeout=&quot;5&quot;&gt;<br \/>\n\t&nbsp;&nbsp; &lt;cfset variables.calc = 0&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfloop from=&quot;1&quot; to=&quot;100000&quot; index=&quot;i&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;cfset variables.calc = variables.calc + i&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;\/cfloop&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfset session.calc = calc&gt;<br \/>\n\t&lt;\/cflock&gt; <\/code><\/p>\n<p>Why anyone would store the results of the calculation above in a session variable is beyond me! But, it&#39;s a great example of what not to do.<\/p>\n<p>The session lock above is held while the <em>&lt;cfloop&gt;<\/em> of 100,000 iterations is run.<\/p>\n<p>This code is better executed as below:<\/p>\n<p><code>&lt;cfset variables.calc = 0&gt;<br \/>\n\t&lt;cfloop from=&quot;1&quot; to=&quot;100000&quot; index=&quot;i&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfset variables.calc = variables.calc + i&gt;<br \/>\n\t&lt;\/cfloop&gt;<br \/>\n\t&lt;cflock scope=&quot;session&quot; type=&quot;readonly&quot; timeout=&quot;5&quot;&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfset session.calc = calc&gt;<br \/>\n\t&lt;\/cflock&gt;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>The same rule would apply to any web development language that offers session management.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lately I&#39;ve been noticing, while examining chunks of ColdFusion code out there, that many folks don&#39;t give much thought to what goes on inside their&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[34,41],"tags":[99],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/280"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"predecessor-version":[{"id":283,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/280\/revisions\/283"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}