欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot MongoDB 索引沖突分析及解決方法

 更新時(shí)間:2018年11月30日 14:28:45   作者:美碼師  
這篇文章主要介紹了SpringBoot MongoDB 索引沖突分析及解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一、背景

spring-data-mongo 實(shí)現(xiàn)了基于 MongoDB 的 ORM-Mapping 能力,

通過(guò)一些簡(jiǎn)單的注解、Query封裝以及工具類,就可以通過(guò)對(duì)象操作來(lái)實(shí)現(xiàn)集合、文檔的增刪改查;

在 SpringBoot 體系中,spring-data-mongo 是 MongoDB Java 工具庫(kù)的不二之選。

二、問(wèn)題產(chǎn)生

在一次項(xiàng)目問(wèn)題的追蹤中,發(fā)現(xiàn)SpringBoot 應(yīng)用啟動(dòng)失敗,報(bào)錯(cuò)信息如下:

Error creating bean with name 'mongoTemplate' defined in class path resource [org/bootfoo/BootConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.dao.DataIntegrityViolationException: Cannot create index for 'deviceId' in collection 'T_MDevice' with keys '{ "deviceId" : 1}' and options '{ "name" : "deviceId"}'. Index already defined as '{ "v" : 1 , "unique" : true , "key" : { "deviceId" : 1} , "name" : "deviceId" , "ns" : "appdb.T_MDevice"}'.; nested exception is com.mongodb.MongoCommandException: Command failed with error 85: 'exception: Index with name: deviceId already exists with different options' on server 127.0.0.1:27017. The full response is { "createdCollectionAutomatically" : false, "numIndexesBefore" : 6, "errmsg" : "exception: Index with name: deviceId already exists with different options", "code" : 85, "ok" : 0.0 }
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)

...

Caused by: org.springframework.dao.DataIntegrityViolationException: Cannot create index for 'deviceId' in collection 'T_MDevice' with keys '{ "deviceId" : 1}' and options '{ "name" : "deviceId"}'. Index already defined as '{ "v" : 1 , "unique" : true , "key" : { "deviceId" : 1} , "name" : "deviceId" , "ns" : "appdb.T_MDevice"}'.; nested exception is com.mongodb.MongoCommandException: Command failed with error 85: 'exception: Index with name: deviceId already exists with different options' on server 127.0.0.1:27017. The full response is { "createdCollectionAutomatically" : false, "numIndexesBefore" : 6, "errmsg" : "exception: Index with name: deviceId already exists with different options", "code" : 85, "ok" : 0.0 }
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:157)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.<init>(MongoPersistentEntityIndexCreator.java:91)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.<init>(MongoPersistentEntityIndexCreator.java:68)
at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:229)
at org.bootfoo.BootConfiguration.mongoTemplate(BootConfiguration.java:121)
at org.bootfoo.BootConfiguration$$EnhancerBySpringCGLIB$$1963a75.CGLIB$mongoTemplate$2(<generated>)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 58 more

Caused by: com.mongodb.MongoCommandException: Command failed with error 85: 'exception: Index with name: deviceId already exists with different options' on server 127.0.0.1:27017. The full response is { "createdCollectionAutomatically" : false, "numIndexesBefore" : 6, "errmsg" : "exception: Index with name: deviceId already exists with different options", "code" : 85, "ok" : 0.0 }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)

關(guān)鍵信息: org.springframework.dao.DataIntegrityViolationException: Cannot create index

從異常信息上看,出現(xiàn)的是索引沖突( Command failed with error 85 ),spring-data-mongo 組件在程序啟動(dòng)時(shí)會(huì)實(shí)現(xiàn)根據(jù)注解創(chuàng)建索引的功能。

查看業(yè)務(wù)實(shí)體定義:

@Document(collection = "T_MDevice")
public class MDevice {

  @Id
  private String id;

  @Indexed(unique=true)
  private String deviceId;

deviceId 這個(gè)字段上定義了一個(gè)索引, unique=true 表示這是一個(gè)唯一索引。

我們繼續(xù) 查看 MongoDB中表的定義:

db.getCollection('T_MDevice').getIndexes()

>>
[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "appdb.T_MDevice"
  },
  {
    "v" : 1,
    "key" : {
      "deviceId" : 1
    },
    "name" : "deviceId",
    "ns" : "appdb.T_MDevice"
  }
]

發(fā)現(xiàn)數(shù)據(jù)庫(kù)表中同樣存在一個(gè)名為 deviceId的索引,但是并非唯一索引!

三、詳細(xì)分析

為了核實(shí)錯(cuò)誤產(chǎn)生的原因,我們嘗試通過(guò) Mongo Shell去執(zhí)行索引的創(chuàng)建,發(fā)現(xiàn)返回了同樣的錯(cuò)誤。

通過(guò)將數(shù)據(jù)庫(kù)中的索引刪除,或更正為 unique=true 之后可以解決當(dāng)前的問(wèn)題。

從嚴(yán)謹(jǐn)度上看,一個(gè)索引沖突導(dǎo)致 SpringBoot 服務(wù)啟動(dòng)不了,是可以接受的。

但從靈活性來(lái)看,是否有某些方式能 禁用索引的自動(dòng)創(chuàng)建 ,或者僅僅是打印日志呢?

嘗試 google spring data mongodb disable index creation

發(fā)現(xiàn) JIRA-DATAMONGO-1201 在2015年就已經(jīng)提出,至今未解決。

stackoverflow 找到許多 同樣問(wèn)題

但大多數(shù)的解答是不采用索引注解,選擇其他方式對(duì)索引進(jìn)行管理。

這些結(jié)果并不能令人滿意。

嘗試查看 spring-data-mongo 的機(jī)制,定位到 MongoPersistentEntityIndexCreator 類:

初始化方法中,會(huì)根據(jù) MappingContext(實(shí)體映射上下文)中已有的實(shí)體去創(chuàng)建索引

public MongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, MongoDbFactory mongoDbFactory,
      IndexResolver indexResolver) {
    ...
    //根據(jù)已有實(shí)體創(chuàng)建
    for (MongoPersistentEntity<?> entity : mappingContext.getPersistentEntities()) {
      checkForIndexes(entity);
    }
  }

在接收到MappingContextEvent時(shí),創(chuàng)建對(duì)應(yīng)實(shí)體的索引

 public void onApplicationEvent(MappingContextEvent<?, ?> event) {

    if (!event.wasEmittedBy(mappingContext)) {
      return;
    }

    PersistentEntity<?, ?> entity = event.getPersistentEntity();

    // Double check type as Spring infrastructure does not consider nested generics
    if (entity instanceof MongoPersistentEntity) {
      //創(chuàng)建單個(gè)實(shí)體索引
      checkForIndexes((MongoPersistentEntity<?>) entity);
    }
  }

MongoPersistentEntityIndexCreator是通過(guò)MongoTemplate引入的,如下:

  public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {

    Assert.notNull(mongoDbFactory);

    this.mongoDbFactory = mongoDbFactory;
    this.exceptionTranslator = mongoDbFactory.getExceptionTranslator();
    this.mongoConverter = mongoConverter == null ? getDefaultMongoConverter(mongoDbFactory) : mongoConverter;
    ...

    // We always have a mapping context in the converter, whether it's a simple one or not
    mappingContext = this.mongoConverter.getMappingContext();
    // We create indexes based on mapping events
    if (null != mappingContext && mappingContext instanceof MongoMappingContext) {
      indexCreator = new MongoPersistentEntityIndexCreator((MongoMappingContext) mappingContext, mongoDbFactory);
      eventPublisher = new MongoMappingEventPublisher(indexCreator);
      if (mappingContext instanceof ApplicationEventPublisherAware) {
        ((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(eventPublisher);
      }
    }
  }


  ...
  //MongoTemplate實(shí)現(xiàn)了 ApplicationContextAware,當(dāng)ApplicationContext被實(shí)例化時(shí)被感知
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    prepareIndexCreator(applicationContext);

    eventPublisher = applicationContext;
    if (mappingContext instanceof ApplicationEventPublisherAware) {
      //MappingContext作為事件來(lái)源,向ApplicationContext發(fā)布
      ((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(eventPublisher);
    }
    resourceLoader = applicationContext;
  }

  ...
  //注入事件監(jiān)聽(tīng)
  private void prepareIndexCreator(ApplicationContext context) {

    String[] indexCreators = context.getBeanNamesForType(MongoPersistentEntityIndexCreator.class);

    for (String creator : indexCreators) {
      MongoPersistentEntityIndexCreator creatorBean = context.getBean(creator, MongoPersistentEntityIndexCreator.class);
      if (creatorBean.isIndexCreatorFor(mappingContext)) {
        return;
      }
    }

    if (context instanceof ConfigurableApplicationContext) {
      //使 IndexCreator 監(jiān)聽(tīng) ApplicationContext的事件
      ((ConfigurableApplicationContext) context).addApplicationListener(indexCreator);
    }
  }

由此可見(jiàn), MongoTemplate 在初始化時(shí),先通過(guò) MongoConverter 帶入 MongoMappingContext,

隨后完成一系列初始化,整個(gè)過(guò)程如下:

  • 實(shí)例化 MongoTemplate;
  • 實(shí)例化 MongoConverter;
  • 實(shí)例化 MongoPersistentEntityIndexCreator;
  • 初始化索引(通過(guò)MappingContext已有實(shí)體);
  • Repository初始化 -> MappingContext 發(fā)布映射事件;
  • ApplicationContext 將事件通知到 IndexCreator;
  • IndexCreator 創(chuàng)建索引

在實(shí)例化過(guò)程中,沒(méi)有任何配置可以阻止索引的創(chuàng)建。

四、解決問(wèn)題

從前面的分析中,可以發(fā)現(xiàn)問(wèn)題關(guān)鍵在 IndexCreator,能否提供一個(gè)自定義的實(shí)現(xiàn)呢,答案是可以的!

實(shí)現(xiàn)的要點(diǎn)如下

  • 實(shí)現(xiàn)一個(gè)IndexCreator,可繼承MongoPersistentEntityIndexCreator,去掉索引的創(chuàng)建功能;
  • 實(shí)例化 MongoConverter和 MongoTemplate時(shí),使用一個(gè)空的 MongoMappingContext對(duì)象避免初始化索引;
  • 將自定義的IndexCreator作為Bean進(jìn)行注冊(cè),這樣在prepareIndexCreator方法執(zhí)行時(shí),原來(lái)的 MongoPersistentEntityIndexCreator不會(huì)監(jiān)聽(tīng)ApplicationContext的事件
  • IndexCreator 實(shí)現(xiàn)了ApplicationContext監(jiān)聽(tīng),接管 MappingEvent事件處理。

實(shí)例化Bean

 @Bean
  public MongoMappingContext mappingContext() {
    return new MongoMappingContext();
  }

  // 使用 MappingContext 實(shí)例化 MongoTemplate
  @Bean
  public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, MongoMappingContext mappingContext) {
    MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
        mappingContext);
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);

    return mongoTemplate;
  }

自定義IndexCreator

  // 自定義IndexCreator實(shí)現(xiàn)
  @Component
  public static class CustomIndexCreator extends MongoPersistentEntityIndexCreator {

    // 構(gòu)造器引用MappingContext
    public CustomIndexCreator(MongoMappingContext mappingContext, MongoDbFactory mongoDbFactory) {
      super(mappingContext, mongoDbFactory);
    }

    public void onApplicationEvent(MappingContextEvent<?, ?> event) {
      PersistentEntity<?, ?> entity = event.getPersistentEntity();

      // 獲得Mongo實(shí)體類
      if (entity instanceof MongoPersistentEntity) {
        System.out.println("Detected MongoEntity " + entity.getName());
        
        //可實(shí)現(xiàn)索引處理..
      }
    }
  }

在這里 CustomIndexCreator繼承了 MongoPersistentEntityIndexCreator ,將自動(dòng)接管MappingContextEvent事件的監(jiān)聽(tīng)。

在業(yè)務(wù)實(shí)現(xiàn)上可以根據(jù)需要完成索引的處理!

小結(jié)

spring-data-mongo 提供了非常大的便利性,但在靈活性支持上仍然不足。上述的方法實(shí)際上有些隱晦,在官方文檔中并未提及這樣的方式。

ORM-Mapping 框架在實(shí)現(xiàn)Schema映射處理時(shí)需要考慮校驗(yàn)級(jí)別,比如 Hibernate便提供了 none/create/update/validation 多種選擇,畢竟這對(duì)開(kāi)發(fā)者來(lái)說(shuō)更加友好。

期待 spring-data-mongo 在后續(xù)的演進(jìn)中能盡快完善 Schema的管理功能!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論