'Why does Cosmos sometimes throw PreconditionFailedExceptionand some other times CosmosAccesException

I have a library that is supposed to update an entry in a Cosmos DB database.

My problem appears when two threads try to update the same entry in the same time.

Until know, I thought if two threads try to update the same entry in the same time, one will succeed and the other one will receive a PreconditionFailedException since the _etag has changed (because of the thread that managed to make the update first).

While debugging, I have noticed that in some cases cosmos throws PreconditionFailedException (which is the expected behaviour) and in some other cases it throws a CosmosAccesException which wraps a PreconditionFailedException. WHY DOES THIS HAPPEN?

    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-starter-cosmos</artifactId>
        <version>3.14.0</version>
    </dependency>

public boolean doSmth(Entry myEntry) {  // myEntry already exists in the database
 try {
        myEntry.setExp(someIrelevantValue);
        lockRepository.save(myEntry);
        return true;
    } catch (PreconditionFailedException e) {
        return false;
    } 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source