PHP code example of team-a / lock
1. Go to this page and download the library: Download team-a/lock library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
team-a / lock example snippets
$serviceManager = $this->getServiceManager();
AbstractDb::setPdoPromise(
function() use ($serviceManager) : \PDO
{
return $serviceManager->getPDO();
}
);
class Point extends AbstractDbExclusive
{
protected function __construct(
int $providerId,
? string $providerPointId,
? string $providerPointEssentialId
)
{
parent::__construct([
$providerId, $providerPointId, $providerPointEssentialId
]);
}
}
/* ... */
$pointLock = new Point($pId, $pPointId, null);
try {
$pointLock->lock();
$this->_db->beginTransaction();
// do smth.
$this->_db->commit();
return true;
} catch (TeamA\Lock\TimeoutException $e) {
return false;
} catch (\Exception $e) {
$this->_db->rollback();
throw $e;
} finally {
$pointLock->releaseIfLocked();
}
class GeoBinding extends AbstractDbExtended
{
public function __construct(int $departureProviderId)
{
parent::__construct(func_get_args());
}
}
/* ... */
$lock = new GeoBinding(static::_getProviderId());
try {
$lock->lockWrite();
$this->_db->beginTransaction();
// do smth.
$this->_db->commit();
return true;
} catch (TeamA\Lock\TimeoutException $e) {
return false;
} catch (\Exception $e) {
$this->_db->rollback();
throw $e;
} finally {
$lock->release();
}
/* ... */
$lock = new GeoBinding(static::_getProviderId());
try {
$lock->lockRead();
$this->_db->beginTransaction();
// do smth. else...