Skip to content

Ability to provide custom version check and increment strategies #3644

Open
@juanfiallo

Description

@juanfiallo

Currently when the @Version annotation is used in a Document's field, that field gets used for optimistic locking, this is achieved by providing a Criteria along with the update, which checks that the version being updated matches the current version of the object, when the update succeeds then the version gets auto-incremented. There doesn't seem to be any simple way of overriding this behavior, other than providing a custom MongoTemplate, even then the code path to override becomes pretty significant, since this is pretty deep down into some private classes, it'd basically result in a lot of duplication just to change a couple of lines of code. The code that builds the version aware query lives in org.springframework.data.mongodb.core.EntityOperations$MappedEntity.getQueryForVersion and the version increment code lives in org.springframework.data.mongodb.core.EntityOperations$AdaptibleMappedEntity.incrementVersion. The ask is to be able to provide custom strategies that can be customized instead of the current hardcoded logic, for example something like this:

interface VersionQueryStrategy {
    Query getQueryForVersion(MongoPersistentEntity<?> entity, Object id, Object version);
}

interface VersionIncrementStrategy {
    <T> T incrementVersion(MongoPersistentEntity<?> entity, ConvertingPropertyAccessor<T> propertyAccessor, Number version)
}

Just to provide some context my use case revolves around using a timestamp that I receive externally as the version field, and my desired query would check that the current version is less than the new version, and the increment logic would be completely skipped since the value is managed externally.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions