<?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>The Developer Day &#187; locking</title>
	<atom:link href="http://www.thedeveloperday.com/tag/locking/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thedeveloperday.com</link>
	<description>Staying Curious</description>
	<lastBuildDate>Tue, 27 Jul 2010 17:33:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Anti Asynchronous AJAX calls and PHP sessions</title>
		<link>http://www.thedeveloperday.com/anti-asynchronous-ajax-calls-and-php-sessions/</link>
		<comments>http://www.thedeveloperday.com/anti-asynchronous-ajax-calls-and-php-sessions/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 11:53:25 +0000</pubDate>
		<dc:creator>Žilvinas Šaltys</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[locking]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.thedeveloperday.com/anti-asynchronous-ajax-calls-and-php-sessions/</guid>
		<description><![CDATA[Few days ago we found a strange bug in one of our applications we are developing. We were creating a certain feature that made up to 2 &#8211; 6 asynchronous ajax calls when certain pages were hit. Those ajax requests contact various services providers and it takes a different amount of time for each one of them [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago we found a strange bug in one of our applications we are developing. We were creating a certain feature that made up to 2 &#8211; 6 asynchronous ajax calls when certain pages were hit. Those ajax requests contact various services providers and it takes a different amount of time for each one of them to respond. We found it odd that none of the calls were actually asynchronous and they were starting to execute one after another. For example we fire 3 asynchronous ajax calls:</p>
<ul>
<li>ajax call 1 takes 2sec.</li>
<li>ajax call 2 takes 3 sec. but responds after 5 sec. only</li>
<li>ajax call 3 takes 4 sec. but responds after 9 sec. only</li>
</ul>
<p>It didn&#8217;t took long to figure out that it had something to do with our php handler scripts that are accepting these ajax calls. We found out that the session_start() function call did not allow these scrips to run in paralel. It soon became clear that two php scripts can&#8217;t use the same session in paralel because without locking they could both do some changes to session and one script would override other script&#8217;s changes.</p>
<p>Turns out there is a function for exactly these kinds of situations in PHP and it&#8217;s named <a href="http://php.net/session_write_close" title="Write session data and end session">session_write_close()</a>; It&#8217;s purpose is to w<span class="dc-title">rite session data and end session. After it is done writing it releases the lock for other scripts to use the session. So now our php handler scripts initialize the session to check that the user is logged in, writes the session changes and ends it, contacts services providers and then starts the session again to make the final session changes. That way one script doesn&#8217;t hold the session locked for it&#8217;s whole execution time.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thedeveloperday.com/anti-asynchronous-ajax-calls-and-php-sessions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
