<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Der Informatikblog &#187; race-condition</title>
	<atom:link href="http://www.informatik-blog.net/tag/race-condition/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.informatik-blog.net</link>
	<description>Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 28 Jun 2010 19:43:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>SQL Race-Conditions</title>
		<link>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/</link>
		<comments>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 11:23:31 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[functional-index]]></category>
		<category><![CDATA[race-condition]]></category>
		<category><![CDATA[unique-constraint]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=162</guid>
		<description><![CDATA[Jeder kennt es, keiner will es: Race-Conditions. Ein Benutzer registriert sich und versendet versehentlich zweimal hintereinander das gleiche Formular. Wer jetzt zuerst zählt: SELECT COUNT&#40;*&#41; AS total FROM tabelle WHERE mail = 'user@example.com'; und danach gleich einfügt: INSERT INTO tabelle &#40;mail&#41; VALUES &#40;'user@example.com'&#41;; läuft Gefahr Opfer einer Race-Condition zu werden. Was ist das und was [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Jeder kennt es, keiner will es: Race-Conditions. Ein Benutzer registriert sich und versendet versehentlich zweimal hintereinander das gleiche Formular. <span id="more-162"></span> Wer jetzt zuerst zählt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> total <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> mail <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'user@example.com'</span>;</pre></div></div>

<p>und danach gleich einfügt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>mail<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user@example.com'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>läuft Gefahr Opfer einer Race-Condition zu werden.</p>
<p><strong>Was ist das und was passiert genau?</strong></p>
<p>Das ist eigentlich ganz einfach erklärt. Wenn wir zweimal das gleiche Formular versenden, durchläuft der Server zweimal die gleiche Datei. Ist der zeitliche Unterschied zwischen den beiden Aufrufen genug klein, ergibt sich bei beiden bei der ersten Zählung 0 und es wird danach auch bei beiden eingefügt. Da Spalten wie &#8220;mail&#8221; aber üblicherweise einen &#8220;unique-constraint&#8221; oder eben einen &#8220;unique-functional-index&#8221; besitzen, kommt es zum schlimmsten: Der spätere der beiden Aufrufe schlägt fehl und wird mit einem Fehler quittiert: Der Datensatz ist bereits vorhanden.</p>
<p><strong>Aber abgebrochene Aufrufe werden vom Server ja auch abgebrochen?</strong></p>
<p>Mit den Standardeinstellungen schon. Oftmals entscheiden sich Programmierer aber dazu &#8220;ignore_user_abort&#8221; zu benutzen was ja auch seinen guten Grund hat. Beispiel Bilderupload: Dort wird üblicherweise ein SQL-Query versandt und danach die Datei im Dateisystem entsprechend untergebracht. (Das ganze geht auch umgekehrt) Wenn der Benutzer jetzt zwischen einer der beiden Aktionen seinen Aufruf abbricht, haben wir einen losen Datensatz in der Datenbank oder im Dateisystem. Die Integrität wäre verloren. Ein weiteres Beispiel wäre eine Tabelle in der alle Besuche eines Mitglieds bei einem anderen Mitglied gespeichert werden natürlich mit einem &#8220;unique-constraint&#8221; über from_id und dest_id:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> tabelle <span style="color: #993333; font-weight: bold;">SET</span> visited <span style="color: #66cc66;">=</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> from_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> dest_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>Wer sich jetzt nur auf die betroffenen Zeilen verlässt und danach gleich einfügt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>from_id<span style="color: #66cc66;">,</span> dest_id<span style="color: #66cc66;">,</span> visited<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>hat wieder eine wunderschöne und noch viel wahrscheinlich eintreffendere Race-Condition erstellt. Bei diesem Beispiel reicht es sogar aus ein Mitglied, welches noch nie besucht wurde, zweimal schnell hintereinander (Doppelklick) zu besuchen was ja nun wirklich nicht realitätsfremd ist.</p>
<p><strong>Was kann ich dagegen tun?</strong></p>
<p>Das ist jetzt eben der trickreiche Teil des ganzen. Er nennt sich &#8220;INSERT INTO &#8230; SELECT&#8221; mit einem Subquery und sieht aus wie folgt:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>mail<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'user@example.com'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> mail <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'user@example.com'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>und für das zweite Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tabelle <span style="color: #66cc66;">&#40;</span>from_id<span style="color: #66cc66;">,</span> dest_id<span style="color: #66cc66;">,</span> visited<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tabelle <span style="color: #993333; font-weight: bold;">WHERE</span> from_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> dest_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>Das war es auch schon. Wenn wir jetzt noch sauberkeitshalber die betroffenen Zeilen abfragen, können wir eine schön formatierte Meldung über den (Miss-)Erfolg ausgeben.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/23/sql-race-conditions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
