{"id":808,"date":"2011-08-17T23:36:13","date_gmt":"2011-08-17T23:36:13","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=808"},"modified":"2011-08-18T09:15:49","modified_gmt":"2011-08-18T09:15:49","slug":"one-less-bug-to-worry-about","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2011\/08\/one-less-bug-to-worry-about\/","title":{"rendered":"One Less Bug To Worry About"},"content":{"rendered":"<p>From <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/information-functions.html#function_last-insert-id\">the MySQL documentation on <code>LAST_INSERT_ID()<\/code> and <code>AUTO_INCREMENT<\/code> columns<\/a>:<\/p>\n<blockquote>\n<p>The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first <code>AUTO_INCREMENT<\/code> value generated for most recent statement affecting an <code>AUTO_INCREMENT<\/code> column by that client. This value cannot be affected by other clients, even if they generate <code>AUTO_INCREMENT<\/code> values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.<\/p>\n<\/blockquote>\n<p>So, I&#039;ve spent years panicking about race conditions in code like that below that apparently won&#039;t exist (at least in my cases):<\/p>\n<pre><code>`tbl`\n------------------------\n`id`  INT AUTO_INCREMENT\n`col` TEXT\n\nINSERT INTO `tbl` (`col`) VALUES(\"lol\");\nSELECT LAST_INSERT_ID(); -- get `id` for inserted row<\/code><\/pre>\n<p>Perhaps you have, too?<\/p>\n<hr \/>\n<p><strong>Update<\/strong><\/p>\n<p>This is <em>not<\/em> explicitly the case for <code>SQL_CALC_FOUND_ROWS<\/code> and <code>FOUND_ROWS()<\/code>, though the following paragraph loosely implies that <code>FOUND_ROWS()<\/code> is guaranteed to be ok after <code>SQL_CALC_FOUND_ROWS<\/code>, at least within the same connection context:<\/p>\n<blockquote>\n<p>The row count available through <code>FOUND_ROWS()<\/code> is transient and not intended to be available past the statement following the <code>SELECT SQL_CALC_FOUND_ROWS<\/code> statement<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>From the MySQL documentation on <code>LAST_INSERT_ID()<\/code> and <code>AUTO_INCREMENT<\/code> columns&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[59,20,12],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/808"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=808"}],"version-history":[{"count":5,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/808\/revisions\/812"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}