2. Add methods to platforms that can add necessary READ/WRITE Lock query additions like FOR UPDATE/SHARED which afaik are supported by all rmdbs in some way.
3. Add a query hint "lockMode" which takes a Lock constant.
4. Add a query hint "lockVersion" which takes an integer or timestamp versioning value.
5. Change DQL Parser to apply lock mode if set, and if lock_mode = optimistic, add a where clause for the only top-level class (more not supported? Reread Evans DDD / Aggregates)
6. Change the $em->find() method to the following signature:

LockModes

Constraints

LockMode::PESSIMISTIC_READ/WRITE works similarly for versioned as well as non-versioned entities. However, lock() after read only works for versioned entities.

API spec

$query->setLockMode(LockMode::OPTIMISTIC)

Effects:

Throw OptimisticLockException if any of the entities fetched (or fetch-joined) by the query are not versioned.

Otherwise proceed normally, SQL is not modified.

$query->setLockMode(LockMode::PESSIMISTIC_READ/WRITE)

Effects:

Throw TransactionRequiredException if there is no running transaction.

Modify the SQL with an appropriate locking clause (i.e. FOR UPDATE) that can be platform-specific, to acquire a pessimistic lock on all read entities.

Throw PessimisticLockException if lock(s) could not be obtained.

$em->find($entity, LockMode::OPTIMISTIC) (or findBy et al)

Effects:

Throw OptimisticLockException if entity is not versioned.

Otherwise proceeed normally.

$em->find($entity, LockMode::OPTIMISTIC, $version) (or findBy et al)

Effects:

Throw OptimisticLockException if entity is not versioned.

find() entity normally, no SQL modification.

Throw OptimisticLockException if there is a version mismatch ($version != $entity->version)

Otherwise proceed normally.

$em->find($entity, LockMode::PESSIMISTIC_READ/WRITE)

Effects:

Throw TransactionRequiredException if there is no active transaction.

Modify the SQL to incude an appropriate platform-specific pessimistic lock (i.e. FOR UPDATE)

throw PessimisticLockException If lock could not be obtained

Otherwise proceed normally

Refresh entity with lock when entity with id exists in identity map already

$em->refresh($entity, LockMode::OPTIMISTIC)

Effects:

Throw OptimisticLockException if entity is not versioned.

Otherwise proceed normally. (What about cascades here? Need to take that into account probably)

$em->refresh($entity, LockMode::PESSIMISTIC_READ/WRITE)

Effects:

Throw TransactionRequiredException if there is no active transaction.

Ensure the SQL used for refreshing is modified accordingly with the platform-specific pessimistic locking clause.

Throw PessimisticLockException if the lock could not be obtained.

Otherwise proceed normally.

$em->lock($entity, LockMode::OPTIMISTIC)

Effects:

Throw OptimisticLockException if entity is not versioned.

Otherwise do nothing (NOOP).

$em->lock($entity, LockMode::PESSIMISTIC_READ/WRITE)

Effects:

Throw TransactionRequiredException if there is no active transaction.

Throw PessimisticLockException if entity is not versioned (this is always a "lock after read")

Issue straight, minimal locking SQL (we probably must include the version column in the select), platform-specific. Note: Probably get the SQL from the persisters to account for different inheritance mapping strategies. The last part of the SQL, the locking clause, is taken from the platforms.

Throw PessimisticLockException if the lock could not be obtained.

Compare the newly read version with the old version. If they dont match throw PessimisticLockException (this means the entity was changed since it was read).

Guilherme Blanco
added a comment - 09/Dec/09 3:33 AM We should be aware that PHP 5.3 now uses mysqlnd driver internally.
This means that queries like LOCK are applied via unbuffered queries, which may compromise subsequent data changes on DB table.
We need to do some testing before effectively apply any type of approach here.

Benjamin Eberlei
added a comment - 02/May/10 9:20 AM The latest version of lock-support is available here:
http://github.com/beberlei/doctrine2/tree/lock-support
The Lock Support is now tested using Gearman Job Server allowing to have functional scenarios where waiting for lock releases is necessary, see:
http://www.whitewashing.de/blog/articles/129
Refresh() is still missing, I am not sure if this should be included in the 2.0 version (use-case is very slim).

Great work so far. I think we can skip refresh() support for now, so post-2.0 if at all.

@"Should we support something like lock timeouts via a query hint?"

I think setHint(Query::HINT_LOCK_TIMEOUT, 0) would be good. That way we keep the possibility open for later enhancements regarding other timeout values (i.e. if features change on databases) without requiring public API changes. However, it should be clearly documented that this is a hint and not a guarantee and it should be documented which database vendors interpret the timeout in which way I think.
As a start, only having timeout = 0 => NOWAIT would be enough.

Roman S. Borschel
added a comment - 19/May/10 6:38 AM Great work so far. I think we can skip refresh() support for now, so post-2.0 if at all.
@"Should we support something like lock timeouts via a query hint?"
I think setHint(Query::HINT_LOCK_TIMEOUT, 0) would be good. That way we keep the possibility open for later enhancements regarding other timeout values (i.e. if features change on databases) without requiring public API changes. However, it should be clearly documented that this is a hint and not a guarantee and it should be documented which database vendors interpret the timeout in which way I think.
As a start, only having timeout = 0 => NOWAIT would be enough.