I have a Java class "Class A" whose object is being saved in a mongoDB using reactiveMongoTemplate.save operation in a reactive way.
public class A {
// field 1
// field 2
..
// field n
}
This had been working perfectly fine and populated a lot of data to the corresponding collection.
Now I decided to introduce indexes to the collection, and hence added this annotation to the class
@CompoundIndex(name ="collection_index_name", def = "{'field1':1, 'field1':-1}", unique=true)
Now after adding this annotation, I see my mongoDB writer( a process which writes data to above mongodb collection ) getting stuck for long duration (15-20mins) or not processing anything further.
On debugging it, I see that the control reaches till the point I have ReaciveMongoTemplate.save()
operation. But after the save reactive method is executed, I just get the below warning, and no writes happen to the collection.
Automatic index creation will be disabled by default as of Spring Data MongoDB 3.x.
Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.
> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
>
> IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
> IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
> resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------
As soon as I remove the above annotation or replace with a fresh new empty collection, and again run the same code, I see entries being saved immediately.
Is there any explanation for this behavior? Has this something to do with adding index to a collection, after a lot of data was already populated inside the collection?