Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locking one row, must not locks all the table. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions lib/Doctrine/Locking/Manager/Pessimistic.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author Roman Borschel <[email protected]>
* @author Pierre Minnieur <[email protected]>
* @author Konsta Vesterinen <[email protected]>
* @author Florian Zumkeller-Quast <[email protected]>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @since 1.0
* @version $Revision: 7490 $
Expand Down Expand Up @@ -89,6 +90,36 @@ public function __construct(Doctrine_Connection $conn)
}
}


/**
* Create and return a unique key for the the given record
*
* @author Florian Zumkeller-Quast <[email protected]>
*
* @since 2010-07-02
*
* @param Doctrine_Record $record The record to create the object key for
*
* @return string The object key for the given record
*/
private function getObjectKey(Doctrine_Record $record)
{
$fields = $record->getTable()->getIdentifier();

// Check if its a composite PK
if(is_array($fields) === false) {
$fields = array($fields);
}

$key = '';
foreach($fields as $field) {
$value = $record->get($field);
$key = sprintf('%s|%s:%s', $key, $field, base64_encode($value));
}

return sha1($key);
}

/**
* Obtains a lock on a {@link Doctrine_Record}
*
Expand All @@ -101,16 +132,11 @@ public function __construct(Doctrine_Connection $conn)
public function getLock(Doctrine_Record $record, $userIdent)
{
$objectType = $record->getTable()->getComponentName();
$key = $record->getTable()->getIdentifier();
$key = $this->getObjectKey($record);

$gotLock = false;
$time = time();

if (is_array($key)) {
// Composite key
$key = implode('|', $key);
}

try {
$dbh = $this->conn->getDbh();
$this->conn->beginTransaction();
Expand Down Expand Up @@ -170,12 +196,7 @@ public function getLock(Doctrine_Record $record, $userIdent)
public function releaseLock(Doctrine_Record $record, $userIdent)
{
$objectType = $record->getTable()->getComponentName();
$key = $record->getTable()->getIdentifier();

if (is_array($key)) {
// Composite key
$key = implode('|', $key);
}
$key = $this->getObjectKey($record);

try {
$dbh = $this->conn->getDbh();
Expand Down Expand Up @@ -206,11 +227,6 @@ public function releaseLock(Doctrine_Record $record, $userIdent)
*/
private function _getLockingUserIdent($objectType, $key)
{
if (is_array($key)) {
// Composite key
$key = implode('|', $key);
}

try {
$dbh = $this->conn->getDbh();
$stmt = $dbh->prepare('SELECT user_ident FROM ' . $this->_lockTable
Expand Down Expand Up @@ -241,7 +257,7 @@ private function _getLockingUserIdent($objectType, $key)
public function getLockOwner($lockedRecord)
{
$objectType = $lockedRecord->getTable()->getComponentName();
$key = $lockedRecord->getTable()->getIdentifier();
$key = $this->getObjectKey($lockedRecord);
return $this->_getLockingUserIdent($objectType, $key);
}

Expand Down