[PR #949] Add a default lock mode to the EntityManager #8949

Closed
opened 2026-01-22 16:02:28 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/949

State: closed
Merged: No


Following this discussion on the mailing list, this proposal introduces 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:

$transaction = $em->createTransaction()
    ->withDefaultLockMode(LockMode::PESSIMISTIC_WRITE)
    ->begin();

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

$transaction->commit();

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

Happily waiting for your feedback!

**Original Pull Request:** https://github.com/doctrine/orm/pull/949 **State:** closed **Merged:** No --- Following [this discussion](https://groups.google.com/forum/#!topic/doctrine-dev/xKGzQcDkilE) on the mailing list, this proposal introduces 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: ``` $transaction = $em->createTransaction() ->withDefaultLockMode(LockMode::PESSIMISTIC_WRITE) ->begin(); // load entities from EntityManager, Repositories or DQL, traverse associations, etc. // all these entities will be loaded with the given lock mode $transaction->commit(); ``` 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 Happily waiting for your feedback!
admin added the pull-request label 2026-01-22 16:02:28 +01:00
admin closed this issue 2026-01-22 16:02:28 +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#8949