Hi,
I am having trouble with index creation using multiple Mongo databases in Spring Data MongoDB 1.0.3.RELEASE. The attached Maven project demonstrates the problem.
There is a bean called Foo and a repository FooRepository. Foo has an @Indexed property prop.
I want my Foo instances to be stored in a database called mongo-index-db1. There are other repositories stored in another database mongo-index-db2 which require a custom mapping converter. I have created separate mongo templates for the two databases and passed them into the <mongo:repositories /> and <mongo:mapping-converter /> tags respectively. Unfortunately, my Foo indexes are created in mongo-index-db2:
This appears to be due to the indexCreationHelper bean created in the MappingMongoConverterParser class receiving the MappingContextEvents for each mongo template. If I comment out the following lines:Code:Mon Jul 30 16:26:04 [initandlisten] connection accepted from 127.0.0.1:60860 #402 Mon Jul 30 16:26:04 [conn402] build index mongo-index-db2.foo { _id: 1 } Mon Jul 30 16:26:04 [conn402] build index done 0 records 0.002 secs Mon Jul 30 16:26:04 [conn402] info: creating collection mongo-index-db2.foo on add index Mon Jul 30 16:26:04 [conn402] build index mongo-index-db2.foo { prop: 1 } Mon Jul 30 16:26:04 [conn402] build index done 0 records 0.002 secs Mon Jul 30 16:26:05 [conn402] end connection 127.0.0.1:60860
then indexes get created everywhere:Code:try { registry.getBeanDefinition(INDEX_HELPER); } catch (NoSuchBeanDefinitionException ignored) { if (!StringUtils.hasText(dbFactoryRef)) { dbFactoryRef = DB_FACTORY; } BeanDefinitionBuilder indexHelperBuilder = BeanDefinitionBuilder .genericBeanDefinition(MongoPersistentEntityIndexCreator.class); indexHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(ctxRef)); indexHelperBuilder.addConstructorArgValue(new RuntimeBeanReference(dbFactoryRef)); registry.registerBeanDefinition(INDEX_HELPER, indexHelperBuilder.getBeanDefinition()); }
which is... not great but better. Everything seems to work with the indexCreationHelper code removed but I'm not really sure what it's doing so something could be going subtly wrong somewhere.Code:Mon Jul 30 16:36:10 [initandlisten] connection accepted from 127.0.0.1:61141 #406 Mon Jul 30 16:36:10 [conn406] build index mongo-index-db1.foo { _id: 1 } Mon Jul 30 16:36:10 [conn406] build index done 0 records 0.001 secs Mon Jul 30 16:36:10 [conn406] info: creating collection mongo-index-db1.foo on add index Mon Jul 30 16:36:10 [conn406] build index mongo-index-db1.foo { prop: 1 } Mon Jul 30 16:36:10 [conn406] build index done 0 records 0.001 secs Mon Jul 30 16:36:11 [initandlisten] connection accepted from 127.0.0.1:61142 #407 Mon Jul 30 16:36:11 [conn407] build index mongo-index-db2.foo { _id: 1 } Mon Jul 30 16:36:11 [conn407] build index done 0 records 0.001 secs Mon Jul 30 16:36:11 [conn407] info: creating collection mongo-index-db2.foo on add index Mon Jul 30 16:36:11 [conn407] build index mongo-index-db2.foo { prop: 1 } Mon Jul 30 16:36:11 [conn407] build index done 0 records 0.001 secs Mon Jul 30 16:36:11 [conn407] end connection 127.0.0.1:61142 Mon Jul 30 16:36:11 [conn406] end connection 127.0.0.1:61141
Is there another way I should be going about this to avoid this problem?
Thanks.


Reply With Quote
