org.springframework.dao.DataIntegrityViolationException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • We were getting the exception {noformat} org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85} 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.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109) {noformat} I went through the source code and found that we are checking if the index exists after we get exception. Why are we not doing this before creating an index? I think we should check if an index exists before creating an index, otherwise this would always throw exceptions whenever application/server is restarted as the classesSeen map would be empty initially. {code:java} void createIndex(IndexDefinitionHolder indexDefinition) { try { mongoDbFactory.getDb().getCollection(indexDefinition.getCollection()).createIndex(indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()); } catch (MongoException ex) { if (MongoDbErrorCodes.isDataIntegrityViolationCode(ex.getCode())) { *DBObject existingIndex = fetchIndexInformation(indexDefinition);* String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; *if (existingIndex != null) {* message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex); } RuntimeException exceptionToThrow = mongoDbFactory.getExceptionTranslator().translateExceptionIfPossible(ex); throw exceptionToThrow != null ? exceptionToThrow : ex; } } {code}
    via by sunny mittal,
  • We were getting the exception {noformat} org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85} 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.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109) {noformat} I went through the source code and found that we are checking if the index exists after we get exception. Why are we not doing this before creating an index? I think we should check if an index exists before creating an index, otherwise this would always throw exceptions whenever application/server is restarted as the classesSeen map would be empty initially. {code:java} void createIndex(IndexDefinitionHolder indexDefinition) { try { mongoDbFactory.getDb().getCollection(indexDefinition.getCollection()).createIndex(indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()); } catch (MongoException ex) { if (MongoDbErrorCodes.isDataIntegrityViolationCode(ex.getCode())) { *DBObject existingIndex = fetchIndexInformation(indexDefinition);* String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; *if (existingIndex != null) {* message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex); } RuntimeException exceptionToThrow = mongoDbFactory.getExceptionTranslator().translateExceptionIfPossible(ex); throw exceptionToThrow != null ? exceptionToThrow : ex; } } {code}
    via by sunny mittal,
    • org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85} 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.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109)
    No Bugmate found.