DDC-2973: [GH-949] Add a default lock mode to the EntityManager #3702

Closed
opened 2026-01-22 14:25:49 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 10, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user @doctrinebot:

This issue is created automatically through a Github pull request on behalf of BenMorel:

Url: https://github.com/doctrine/doctrine2/pull/949

Message:

Following this discussion on the mailing list, this is a first draft of a proposal to introduce a default lock mode for all entities loaded through an EntityManager.

At the moment, there is no way to set a lock mode for the following use cases:

  • Proxies obtained through getReference() and then initialized
  • Entities lazy-loaded through traversal of associations

This proposal introduces the idea of a default lock mode, which can be set at runtime when all reads in a transaction should be locking.

It works this way:

$em->beginTransaction();
$em->getConfiguration()->setDefaultLockMode(LockMode::PESSIMISTIC_WRITE);

// load entities from EntityManager, Repositories or DQL, traverse associations, etc.
// all these entities will be loaded with the given lock mode

$em->commit();
$em->getConfiguration()->setDefaultLockMode(null);

I have successfully tested it with the following use cases:

  • EntityManager::find()
  • EntityRepository::findBy()
  • DQL queries
  • Proxies
  • Lazy-loaded collections through OneToMany and ManyToMany associations

Before moving forward and writing proper unit tests, I'm looking for your feedback on this proposal. Is this a concept you would be happy to integrate in Doctrine?

If yes, I have a doubt as regards to where the default lock mode should be set: @Ocramius suggested to set it on the Configuration; this looked reasonable at first glance, and I have implemented it this way for now. It feels a tiny bit wrong though now that I see it, as I feel like the contents of the Configuration should should only be set during bootstrapping, rather than being set and reset in the controllers as in the example above. I might be wrong obviously.

My suggestion would be to move the default lock mode to the EntityManager, so that the code would become:

$em->beginTransaction();
$em->setDefaultLockMode(LockMode::PESSIMISTIC_WRITE);

Happily waiting for your feedback!

Originally created by @doctrinebot on GitHub (Feb 10, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user @doctrinebot: This issue is created automatically through a Github pull request on behalf of BenMorel: Url: https://github.com/doctrine/doctrine2/pull/949 Message: Following [this discussion](https://groups.google.com/forum/#!topic/doctrine-dev/xKGzQcDkilE) on the mailing list, this is a first draft of a proposal to introduce a default lock mode for all entities loaded through an EntityManager. At the moment, there is no way to set a lock mode for the following use cases: - Proxies obtained through `getReference()` and then initialized - Entities lazy-loaded through traversal of associations This proposal introduces the idea of a **default** lock mode, which can be set at runtime when all reads in a transaction should be locking. It works this way: ``` $em->beginTransaction(); $em->getConfiguration()->setDefaultLockMode(LockMode::PESSIMISTIC_WRITE); // load entities from EntityManager, Repositories or DQL, traverse associations, etc. // all these entities will be loaded with the given lock mode $em->commit(); $em->getConfiguration()->setDefaultLockMode(null); ``` I have successfully tested it with the following use cases: - `EntityManager::find()` - `EntityRepository::findBy()` - DQL queries - Proxies - Lazy-loaded collections through OneToMany and ManyToMany associations Before moving forward and writing proper unit tests, I'm looking for your feedback on this proposal. Is this a concept you would be happy to integrate in Doctrine? If yes, I have a doubt as regards to where the default lock mode should be set: @Ocramius suggested to set it on the `Configuration`; this looked reasonable at first glance, and I have implemented it this way for now. It feels a tiny bit wrong though now that I see it, as I feel like the contents of the Configuration should should only be set during bootstrapping, rather than being set and reset in the controllers as in the example above. I might be wrong obviously. My suggestion would be to move the default lock mode to the EntityManager, so that the code would become: ``` $em->beginTransaction(); $em->setDefaultLockMode(LockMode::PESSIMISTIC_WRITE); ``` Happily waiting for your feedback!
admin added the New FeatureDuplicate labels 2026-01-22 14:25:49 +01:00
admin closed this issue 2026-01-22 14:25:49 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 24, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-949] was labeled:
https://github.com/doctrine/doctrine2/pull/949

@doctrinebot commented on GitHub (Jan 24, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-949] was labeled: https://github.com/doctrine/doctrine2/pull/949
Author
Owner

@doctrinebot commented on GitHub (Jan 24, 2015):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-949] was labeled:
https://github.com/doctrine/doctrine2/pull/949

@doctrinebot commented on GitHub (Jan 24, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-949] was labeled: https://github.com/doctrine/doctrine2/pull/949
Author
Owner

@BenMorel commented on GitHub (Mar 7, 2018):

Just discovered this issue which was created by JIRA and is redundant with #949. This issue can be closed.

@BenMorel commented on GitHub (Mar 7, 2018): Just discovered this issue which was created by JIRA and is redundant with #949. This issue can be closed.
Author
Owner

@Ocramius commented on GitHub (Mar 7, 2018):

Thanks @BenMorel

@Ocramius commented on GitHub (Mar 7, 2018): Thanks @BenMorel
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3702