Skip to content

Docs for Optimistic Locking exceptions are inaccurate #5007

Open
@tearfur

Description

@tearfur

From the docs, in the example for Optimistic Locking:

  1. Try to update the previously loaded document that still has version = 0. The operation fails with an OptimisticLockingFailureException, as the current version is 1.

This is inaccurate. In this specific case, where the version number of the document to be saved is (current version - 1), the actual exception thrown in practice is something like this:

com.mongodb.MongoWriteException: Write operation error on server localhost:27017. Write error: WriteError{code=11000, message='E11000 duplicate key error collection: database.collection index: _id_ dup key: { _id: "key" }', details={}}.
	at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1123)
	at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:498)
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:481)
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:475)
	at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocument$17(MongoTemplate.java:1564)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:607)
	at org.springframework.data.mongodb.core.MongoTemplate.insertDocument(MongoTemplate.java:1558)
	at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:1356)

Version: 4.5.0

If you need a repro repo, please let me know.

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