Transactions and Concurrency¶
Transactions¶
As per the documentation, MongoDB write operations are "atomic on the level of a single document".
Even when updating multiple documents within a single write operation, though the modification of each document is atomic, the operation as a whole is not and other operations may interleave.
As stated in the FAQ, "MongoDB does not support multi-document transactions" and neither does Doctrine MongoDB ODM.
Limitation¶
At the moment, Doctrine MongoDB ODM does not provide any native strategy to emulate multi-document transactions.
Workaround¶
To work around this limitation, one can utilize two phase commits.
Concurrency¶
Doctrine MongoDB ODM offers native support for pessimistic and optimistic locking strategies. This allows for very fine-grained control over what kind of locking is required for documents in your application.
Optimistic Locking¶
Approach¶
Doctrine has integrated support for automatic optimistic locking
via a version
field. Any document that should be
protected against concurrent modifications during long-running
business transactions gets a version
field that is either a simple
number (mapping type: int
) or a date (mapping type: date
).
When changes to the document are persisted,
the expected version and version increment are incorporated into the update criteria and modifiers, respectively.
If this results in no document being modified by the update (i.e. expected version did not match),
a LockException
is thrown, which indicates that the document was already modified by another query.
Note
Document Configuration¶
The following example designates a version field using the int
type:
- PHP
<?php /** @Version @Field(type="int") */ private $version;
- XML
<field fieldName="version" version="true" type="int" />
- YAML
version: type: int version: true
Alternatively, the date
type may be used:
- PHP
<?php /** @Version @Field(type="date") */ private $version;
- XML
<field fieldName="version" version="true" type="date" />
- YAML
version: type: date version: true
Choosing the Field Type¶
When using the date
type in a high-concurrency environment, it is still possible to create multiple documents
with the same version and cause a conflict. This can be avoided by using the int
type.
Usage¶
When a version conflict is encountered during
DocumentManager#flush()
, a LockException
is thrown.
This exception can be caught and handled. Potential responses to a
LockException
are to present the conflict to the user or
to refresh or reload objects and then retry the update.
With PHP promoting a share-nothing architecture,
the worst case scenario for a delay between rendering an update form (with existing document data)
and modifying the document after a form submission may be your application's session timeout.
If the document is changed within that time frame by some other request,
it may be preferable to encounter a LockException
when retrieving the document instead of executing the update.
You can specify the expected version of a document during a query with DocumentManager#find()
:
<?php
use Doctrine\ODM\MongoDB\LockMode;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\DocumentManager;
$theDocumentId = 1;
$expectedVersion = 184;
/* @var $dm DocumentManager */
try {
$document = $dm->find('User', $theDocumentId, LockMode::OPTIMISTIC, $expectedVersion);
// do the work
$dm->flush();
} catch(LockException $e) {
echo "Sorry, but someone else has already changed this document. Please apply the changes again!";
}
Alternatively, an expected version may be specified for an existing document with DocumentManager#lock()
:
<?php
use Doctrine\ODM\MongoDB\LockMode;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\DocumentManager;
$theDocumentId = 1;
$expectedVersion = 184;
/* @var $dm DocumentManager */
$document = $dm->find('User', $theDocumentId);
try {
// assert version
$dm->lock($document, LockMode::OPTIMISTIC, $expectedVersion);
} catch(LockException $e) {
echo "Sorry, but someone else has already changed this document. Please apply the changes again!";
}
Important Implementation Notes¶
You can easily get the optimistic locking workflow wrong if you compare the wrong versions.
Workflow¶
Say you have Alice and Bob editing a hypothetical blog post:
- Alice reads the headline of the blog post being "Foo", at optimistic lock version 1 (GET Request)
- Bob reads the headline of the blog post being "Foo", at optimistic lock version 1 (GET Request)
- Bob updates the headline to "Bar", upgrading the optimistic lock version to 2 (POST Request of a Form)
- Alice updates the headline to "Baz", ... (POST Request of a Form)
At the last stage of this scenario the blog post has to be read again from the database before Alice's headline can be applied. At this point you will want to check if the blog post is still at version 1 (which it is not in this scenario).
In order to correctly utilize optimistic locking, you must add the version as hidden form field or, for more security, session attribute. Otherwise, you cannot verify that the version at the time of update is the same as what was originally read from the database when Alice performed her original GET request for the blog post. Without correlating the version across form submissions, the application could lose updates.
Example Code¶
The form (GET Request):
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
/* @var $dm DocumentManager */
$post = $dm->find('BlogPost', 123456);
echo '<input type="hidden" name="id" value="' . $post->getId() . '" />';
echo '<input type="hidden" name="version" value="' . $post->getCurrentVersion() . '" />';
And the change headline action (POST Request):
<?php
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\LockMode;
/* @var $dm DocumentManager */
$postId = (int)$_POST['id'];
$postVersion = (int)$_POST['version'];
$post = $dm->find('BlogPost', $postId, LockMode::OPTIMISTIC, $postVersion);
Pessimistic Locking¶
Doctrine MongoDB ODM also supports pessimistic locking via a configurable lock
field.
This functionality is implemented entirely by Doctrine; MongoDB has no native support for pessimistic locking.
Document Configuration¶
Pessimistic locking requires a document to designate a lock field using the int
type:
- PHP
<?php /** @Lock @Field(type="int") */ private $lock;
- XML
<field fieldName="lock" lock="true" type="int" />
- YAML
lock: type: int lock: true
Lock Modes¶
Doctrine MongoDB ODM currently supports two pessimistic lock modes:
- Pessimistic Write
(
\Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_WRITE
): locks the underlying document for concurrent read and write operations. - Pessimistic Read (
\Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_READ
): locks other concurrent requests that attempt to update or lock documents in write mode.
Usage¶
You can use pessimistic locks in two different scenarios:
- Using
DocumentManager#find($className, $id, \Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_WRITE)
orDocumentManager#find($className, $id, \Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_READ)
- Using
DocumentManager#lock($document, \Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_WRITE)
orDocumentManager#lock($document, \Doctrine\ODM\MongoDB\LockMode::PESSIMISTIC_READ)
Warning