PHP Classes
elePHPant
Icontem

AJAX Locking: Lock server side resources from AJAX requests

Recommend this page to a friend!
  Info   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2007-07-27 (9 years ago) RSS 2.0 feedStarStarStar 56%Total: 2,512 This week: 2All time: 1,481 This week: 591Up
Version License PHP version Categories
ajax_locking 0.4.1The PHP License4.2.0Language, AJAX
Description Author

This package can be used lock server side resources to prevent race conditions that may occur when two users want to access the same resource simultaneously.

It uses AJAX to lock and unlock resources from Javascript code running on the browser, thus without reloading the current page.

The package comes with several drivers that can use different containers to manage the resource lock information. Currently it supports two drivers for SQL databases and shared memory as containers.

Innovation Award
PHP Programming Innovation award nominee
November 2006
Number 3


Prize: One book of choice by SAMS
AJAX applications may trigger multiple requests to the Web server that can occur at the same time.

If different requests attempt to access and change the same server side resources at the same time, this may lead to race conditions that may cause undesired effects.

This class provides a solution that can be used to prevent this problem. It can lock server side resources that are being manipulated upon browser side events that trigger AJAX requests.

Manuel Lemos
Picture of Fabio Ambrosanio
Name: Fabio Ambrosanio <contact>
Classes: 4 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 3x

Details
AJAX_Locking is a framework in AJAX to manage locking of object in a web application.

The idea is "stolen" from David Perelman-Hall's column appeared in DDJ issue on 8th September 2006
http://www.ddj.com/showArticle.jhtml?articleID=192700218

The idea is to use AJAX to lock, unlock and get status of records/objects, avoiding use of locks on db (or transactions).

AJAX_Locking is a class that extends HTML_AJAX_Server and implements itself the features to lock, unlock and get status via a "driver":
the default driver (AJAX_Locking_Driver_SharedMemory) uses PEAR's package System_SharedMemory as repository of locks,
you can write your own driver to achieve the same purpose, using a db or the filesystem.

To use AJAX_Locking you have to write a "server" page like the following, that instantiate the Ajax remote object:

server.php
<?php
require_once ('AJAX_Locking/AJAX_Locking.php');

require_once('AJAX_Locking/Driver/SharedMemory.php');
$driver = new AJAX_Locking_Driver_SharedMemory(10, 'file');
$server = new AJAX_Locking($driver);
$server->handleRequest();
?>

and in your client page you have to use HTML_Ajax framework to call lock, unlock and status methods provided from server.
AJAX_Locking provides its own javascript library to help to write client-side management:

test.php
<?php
$user='fabamb';
$type='test';
$id  =1;
?>

<html>
<head>
<script type = 'text/javascript' src = "server.php?client=Locking,all&stub=all"></script>
<script type = "text/javascript">
var lockingCallbacks = { 
	count: 0, 
	lock: function(result) {
		if (result === true) {
			h.status();
		} else {
			h.stop();
			HTML_AJAX_Util.setInnerHTML('status', 'Errore durante il lock');
		}
	},                   
	unlock: function(result) {
		if (result === true) {
			h.status();
		} else {
			h.stop();
			HTML_AJAX_Util.setInnerHTML('status', 'Errore durante l\'unlock');
		}
	},                   
	status: function(result) {
		HTML_AJAX_Util.setInnerHTML('count', this.count++);
		var values = result.split('~');

		switch (values[0]) {
			case 'owned':
				html = '<img src=owned.png align=absmiddle>In fase di modifica';

				enableButton('editButton', false);
				enableButton('saveButton', true);
				break;

			case 'locked':
				html = '<img src=locked.png align=absmiddle>Bloccato da ' + values[1];

				enableButton('editButton', false);
				enableButton('saveButton', false);
				break;

			case 'unlocked':
				html = '<img src=unlocked.png align=absmiddle>';

				enableButton('editButton', true);
				enableButton('saveButton', false);
				break;

			case 'timeout':
				h.stop();

				html = '<img src=timeout.png align=absmiddle>Fase di modifica scaduta';
				enableButton('editButton', false);
				enableButton('saveButton', false);
				break;
		}

		HTML_AJAX_Util.setInnerHTML('status', html);
	}
}

function enableButton(id, enabled) {
	var button = document.getElementById(id);
	
	if (button)	button.disabled = !enabled;
}

var h = new AJAX_Locking_Handler(lockingCallbacks, '<?=$user?>', '<?=$type?>', <?=$id?>);
</script>
</head>

<body onload = 'h.start();'>
<div id = "count">
</div>

User: <?= $user ?><br>
Tipo <?= $type ?><br>
Id: <?= $id ?><br>
Status: <span id = "status"></span>

<br>
<button id = "editButton" type = "button" onClick = "h.lock()">Edit</button>
<button id = "saveButton" type = "button" onClick = "h.unlock()" disabled>Save</button>
<button id = "statusButton" type = "button" onClick = "h.status()">Status</button>

<br>
</body>
</html>
  Files folder image Files  
File Role Description
Files folder imageDriver (4 files)
Files folder imagejs (1 file)
Files folder imagetests (7 files)
Accessible without login Plain text file README.txt Doc. README
Plain text file AJAX_Locking.php Class PEAR HTML_AJAX class

  Files folder image Files  /  Driver  
File Role Description
  Plain text file Driver.php Class Base class for AJAX_Locking drivers
  Plain text file File.php Class Driver based on plain filesystem
  Plain text file MDB2.php Class Driver based on PEAR's MBD2 package
  Plain text file SharedMemory.php Class Driver based on PEAR's System_SharedMemory package

  Files folder image Files  /  js  
File Role Description
  Accessible without login Plain text file Locking.js Data Javascript library to handle AJAX calls

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Image file locked.png Icon locked image
  Accessible without login Image file owned.png Icon owned image
  Accessible without login Plain text file server.php Aux. Example AJAX server
  Accessible without login HTML file test.html Data Test/Example
  Accessible without login Image file timeoutpng Icon timeout image
  Accessible without login Image file unlocked.png Icon unlocked image
  Accessible without login Plain text file _test.php Example Test/example

 Version Control Unique User Downloads Download Rankings  
 0%
Total:2,512
This week:2
All time:1,481
This week:591Up
 User Ratings  
 
 All time
Utility:75%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:54%StarStarStar
Examples:58%StarStarStar
Tests:-
Videos:-
Overall:56%StarStarStar
Rank:1524