1

I'm currently trying to update a document using MongoRepository in Spring, where it is connecting to an AWS DocumentDB cluster. I am getting a 301 - Retryable writes are not supported error even though the URL used to connect to DocumentDB includes retryWrites=false, so I don't know how I'm supposed to update documents or if I'm supposed to disable retryWrites from somewhere else in Spring as well.

The URL for the DocumentDB connection looks like this: mongodb://<username>:<password>@mongo-dev-cluster.cluster-xxxxx.eu-west-2.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false

The code for the model, repository and service looks like this:

@Service
public class CarService {
    @Autowired
    private CarRepository carRepository;

    public void update(String id, Car car) {
      // Just saving wouldn't work because there is an indexed key
      car.setId(id);
      carRepository.save(car);
  }
}

@Repository
public interface CarRepository extends
        MongoRepository<Car, String> {
}

@Document
@TypeAlias("car")
public class Car {
    @Id
    private String id;
    @Indexed(unique = true)
    private String carName;
    private String color;
}

The application.properties looks like this:

spring.data.mongodb.username=${DATABASE_USERNAME}
spring.data.mongodb.password=${DATABASE_PASSWORD}
spring.data.mongodb.database=cars-db
spring.data.mongodb.port=27017
spring.data.mongodb.host=mongo-dev-cluster.cluster-xxxxx.eu-west-2.docdb.amazonaws.com

How can I stop this error from happening when updating a document where I want to keep the ID and it's indexed values equal?

smorenoq
  • 21
  • 3

1 Answers1

1

I figured out what I was doing wrong. I was configuring MongoDB in Spring to use host and port, meaning the retryWrites=false was never being read. Instead, I changed my application.properties to use a URI instead of a host and port. It now looks like this:

spring.data.mongodb.database=cars-db
spring.data.mongodb.uri=mongodb://<username>:<password>@mongo-dev-cluster.cluster-xxxxx.eu-west-2.docdb.amazonaws.com:27017/?retryWrites=false`

This means the retryWrites=false property is now being read correctly, whereas before, it was apparently only reading the host name so it wasn't disabling retryable writes.

smorenoq
  • 21
  • 3