<?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; functional-index</title>
	<atom:link href="http://www.informatik-blog.net/tag/functional-index/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.informatik-blog.net</link>
	<description>Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 19 Dec 2011 13:01:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.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>
		<item>
		<title>Aufgepasst: E-Mail</title>
		<link>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/</link>
		<comments>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 20:31:41 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[e-mail]]></category>
		<category><![CDATA[functional-index]]></category>
		<category><![CDATA[lower]]></category>
		<category><![CDATA[strtolower]]></category>
		<category><![CDATA[trim]]></category>
		<category><![CDATA[unique-constraint]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=97</guid>
		<description><![CDATA[Ein Projekt wird realisiert und dabei wird eines viel zu oft vergessen: Die richtige Handhabung der E-Mailadressen. Üblicherweise erstellt der Initiator des Projektes eine Tabelle mit der Spalte &#8220;mail&#8221;, setzt sie auf unique und gibt sich damit zufrieden. Doch aufgepasst: Je nach Einstellungen des Datenbanksystems oder der Verbindung ist die unique-Spalte case-sensitive. Das bedeutet sie [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ein Projekt wird realisiert und dabei wird eines viel zu oft vergessen: Die richtige Handhabung der E-Mailadressen. Üblicherweise erstellt der Initiator des Projektes eine Tabelle mit der Spalte &#8220;mail&#8221;, setzt sie auf unique und gibt sich damit zufrieden. <span id="more-97"></span> Doch aufgepasst: Je nach Einstellungen des Datenbanksystems oder der Verbindung ist die unique-Spalte case-sensitive. Das bedeutet sie unterscheidet zwischen Gross- und Kleinschreibung. Dann wird es möglich, sich mit ein- und derselben E-Mailadresse mehrmals zu registrieren. Zum Beispiel:</p>
<p><strong>&#8220;user@example.com&#8221; und &#8220;User@example.com&#8221;</strong></p>
<p>Ausserdem entfernen die meisten MTAs überschüssige Leerzeichen am Anfang und am Ende der E-Mailadresse.  MTAs (Mail Transfer Agent) sind Programme, die nach dem Aufruf von mail() eine E-Mail im Hintergrund versenden. Ein Beispiel für diesen Fehler:</p>
<p><strong>&#8220;user@example.com&#8221; und &#8221; user@example.com&#8221;</strong></p>
<p>Für die Datenbank sind das nicht die gleichen Strings &#8211; für manche MTAs schon. Daraus ergibt sich, vorallem in Kombination beider Fehler, eine imense Anzahl an Möglichkeiten sich mit der gleichen E-Mailadresse zu registrieren.</p>
<p>Nun gibt es grundsätzlich 2 Varianten dem entgegenzutreten. Die erste wäre folgende Funktion:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> address<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mail</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$revised</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mail</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$revised</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bei diesem (durch PHP gesicherten) Beispiel muss einfach sichergestellt werden, dass die Funktion überall dort Verwendung findet wo eine E-Mailadresse entgegengenommen wird. Das zweite Beispiel wäre ein sogenannter &#8220;functional index&#8221; im DBS. Das sind Indizes die einer gewissen Regel (Funktion) obliegen:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> name_vom_index <span style="color: #993333; font-weight: bold;">ON</span> tabelle<span style="color: #66cc66;">&#40;</span>lower<span style="color: #66cc66;">&#40;</span>trim<span style="color: #66cc66;">&#40;</span>spalte<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Ich bin mir nicht sicher ob MySQL letzteres unterstützt. PostgreSQL tut es zumindest.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/22/aufgepasst-e-mail/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

