10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: Compon<strong>en</strong>t Overview 188<br />

Locking is a mechanism to control concurr<strong>en</strong>cy. The two most well known locking strategies<br />

are optimistic and pessimistic locking. The following is a short description of these two<br />

strategies from which only pessimistic locking is curr<strong>en</strong>tly supported by <strong>Doctrine</strong>.<br />

Optimistic Locking<br />

The state/version of the object(s) is noted wh<strong>en</strong> the transaction begins. Wh<strong>en</strong> the transaction<br />

finishes the noted state/version of the participating objects is compared to the curr<strong>en</strong>t state/<br />

version. Wh<strong>en</strong> the states/versions differ the objects have be<strong>en</strong> modified by another<br />

transaction and the curr<strong>en</strong>t transaction should fail. This approach is called 'optimistic'<br />

because it is assumed that it is unlikely that several users will participate in transactions on<br />

the same objects at the same time.<br />

Pessimistic Locking<br />

The objects that need to participate in the transaction are locked at the mom<strong>en</strong>t the user<br />

starts the transaction. No other user can start a transaction that operates on these objects<br />

while the locks are active. This <strong>en</strong>sures that the user who starts the transaction can be sure<br />

that no one else modifies the same objects until he has finished his work.<br />

<strong>Doctrine</strong>'s pessimistic offline locking capabilities can be used to control concurr<strong>en</strong>cy during<br />

actions or procedures that take several HTTP request and response cycles and/or a lot of time<br />

to complete.<br />

Examples<br />

The following code snippet demonstrates the use of <strong>Doctrine</strong>'s pessimistic offline locking<br />

capabilities.<br />

At the page where the lock is requested get a locking manager instance:<br />

Listing<br />

10-103<br />

// test.php<br />

// ...<br />

$lockingManager = new <strong>Doctrine</strong>_Locking_Manager_Pessimistic();<br />

Ensure that old locks which timed out are released before we try to acquire our lock 300<br />

seconds = 5 minutes timeout. This can be done by using the releaseAgedLocks()<br />

method.<br />

Listing<br />

10-104<br />

// test.php<br />

// ...<br />

$user = <strong>Doctrine</strong>_Core::getTable('User')->find(1);<br />

try<br />

{<br />

$lockingManager->releaseAgedLocks(300);<br />

$gotLock = $lockingManager->getLock($user, 'jwage');<br />

if ($gotLock)<br />

{<br />

echo "Got lock!";<br />

}<br />

else<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!