DDC-2930: Pessimistic locking using $em->find('Entity', $id, LockMode::PESSIMISTIC_WRITE) and $em->lock($entiy, LockMode::PESSIMISTIC_WRITE) differs in (not)refreshing entity state from DB #3645

Open
opened 2026-01-22 14:24:38 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 23, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user jkavalik:

When using pessimistic locking on MySQL(InnoDB tables) there is difference which surprised me:

$em->find('Entity', $id, LockMode::PESSIMISTIC_WRITE);

will reload entity state from DB even when entity was already managed in cache (implemented in DDC-2929)
So I supposed that

$em->lock($entiy, LockMode::PESSIMISTIC_WRITE);

will work alike even when in this case entity is sure already loaded and managed
But it is not the case, actual query log showed only

SELECT 1 FROM table WHERE id FOR UPDATE

If this difference is intended it would be nice to mention it in http://docs.doctrine-project.org/en/latest/reference/transactions-and-concurrency.html

As a workaround I use

$em->refresh($entiy);
Originally created by @doctrinebot on GitHub (Jan 23, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user jkavalik: When using pessimistic locking on MySQL(InnoDB tables) there is difference which surprised me: ``` $em->find('Entity', $id, LockMode::PESSIMISTIC_WRITE); ``` will reload entity state from DB even when entity was already managed in cache (implemented in [DDC-2929](http://www.doctrine-project.org/jira/browse/DDC-2929)) So I supposed that ``` $em->lock($entiy, LockMode::PESSIMISTIC_WRITE); ``` will work alike even when in this case entity is sure already loaded and managed But it is not the case, actual query log showed only ``` SELECT 1 FROM table WHERE id FOR UPDATE ``` If this difference is intended it would be nice to mention it in http://docs.doctrine-project.org/en/latest/reference/transactions-and-concurrency.html As a workaround I use ``` $em->refresh($entiy); ```
admin added the Bug label 2026-01-22 14:24:38 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3645