If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Tip: Using SELECT GET_LOCK locking

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-17-03, 23:17
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
Lightbulb Tip: Using SELECT GET_LOCK locking

Of course you know about LOCK TABLE as a way of preventing multiple access to the same table (e.g. when performing a series of updates). But if your applications are co-operating with one another there is a much more efficient way.

Code:
function db_lock($lockname, $timeout="1")
{
$result = FALSE;
$query  = "SELECT GET_LOCK(\"$lockname\", $timeout)";
$q 	= safe_query($query);  
// ("SAFE_QUERY" IS MY FUNCTION TO EXECUTE AN SQL-QUERY.)
if ($q)
{
	if ($row = mysql_fetch_array($q, MYSQL_NUM))
		$result = ($row[0] == 1);

	mysql_free_result($q);
};
return $result;
}

function db_unlock($lockname)
{
$result = FALSE;
$query  = "SELECT RELEASE_LOCK(\"$lockname\")";
$q 	= safe_query($query);
if ($q)
{
	if ($row = mysql_fetch_array($q, MYSQL_NUM))
			$result = ($row[0] == 1);

	mysql_free_result($q);
};
return $result;
}
These functions lock and unlock an arbitrary string-value. They do it very efficiently. Your applications agree upon a convention that they'll use in building the strings for different things, when they'll try to lock and unlock them and so forth. This allows you to create much higher throughput because lock-requests that don't have to block one another, don't.
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On