Auto index creation support appears broken because the CouchbaseMappingContext @Bean is initialized before the CouchbaseTemplate has had a chance to register the CouchbasePersistentEntityIndexCreator as an ApplicationListener.

Bean dependencies:

  • org.springframework.boot.autoconfigure.data.couchbase.CouchbaseClientFactoryDependentConfiguration#couchbaseTemplate requires MappingCouchbaseConverter
  • org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataConfiguration#couchbaseMappingConverter requires CouchbaseMappingContext
  • org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataConfiguration#couchbaseMappingContext

How the beans are initialized:

CouchbaseMappingContext implements InitializingBean which calls CouchbaseMappingContext#initalize to then iterate over the supplied (from factory method) initialEntitySet and calls CouchbaseMappingContext#addPersistentEntity on each one, which ends up publishing a MappingContextEvent for each entity found.

CouchbaseTemplate creates a CouchbasePersistentEntityIndexCreator in its constructor, CouchbaseTemplate also implements ApplicationContextAware and when setApplicationContext() is called ends up adding the CouchbasePersistentEntityIndexCreator instance to the ApplicationContext as a ApplicationListener.

CouchbasePersistentEntityIndexCreator implements ApplicationListener and listens for MappingContextEvent's, and handles the automatic index creation logic.

Possible solution:

I think ideally the correct solution will involve moving the CouchbasePersistentEntityIndexCreator instance creation OUT of the CouchbaseTemplate and into an appropriate @Configuration class.

But I have a feeling that marking the CouchbaseMappingContext @Bean as @Lazy might be the more practical solution? I haven't tested this theory so..

Comment From: aaronjwhiteside

@daschl involving you here because I know you from TestContainers and you were super helpful there and I also saw your name in the source for Spring Data Couchbase..

If not you would you be able to rope in the right Couchbase people?

Comment From: snicoll

@aaronjwhiteside have you considered reporting that against Spring Data Couchbase? It's not apparent to me if a change in Spring Boot is required, in Spring Data Couchbase, or both. @daschl is working on the latter so let's hear from him first.

Comment From: aaronjwhiteside

@snicoll so that thought did occur to me, I checked the Spring Data Couchbase repo and saw tests for the automatic index creation, and then checked the spring boot autoconfigure repo and saw no such tests

I only came across this issue because I was defining a custom CouchbaseTemplate and writing tests for it myself that needed indices to function, that lead down the rabbit hole of debugging why they were not being created correctly.

My assumption here is that it's something specific to how Spring Boot has done the auto configuration vs the somewhat manual effort required to make vanilla Spring Data Couchbase work. But I admit I haven't really gone very deep in finding out exactly how Spring Data Couchbase makes this work correctly.

Comment From: daschl

@aaronjwhiteside could this be related to https://github.com/spring-projects/spring-data-couchbase/pull/295 ?

Comment From: mikereiche

Please refer to https://jira.spring.io/browse/DATACOUCH-550

Comment From: snicoll

I am going to close this one now and we can reconsider if the changes that are being discussed in https://github.com/spring-projects/spring-data-couchbase/pull/298 require a change in Spring Boot.