{"record":{"id":"3fe784243fcb67c8","repo":"spring-projects/spring-ai","slug":"failed-to-invoke-createindex-method","errorCode":null,"errorMessage":"Failed to invoke createIndex() method","messagePattern":"Failed to invoke createIndex\\(\\) method","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java","lineNumber":135,"sourceCode":"\t\tcatch (NoSuchMethodException createIndexNotFound) {\n\t\t\t// Fall back to old API (Spring Data MongoDB 4.2.x - 4.4.x)\n\t\t\ttry {\n\t\t\t\tMethod method = IndexOperations.class.getMethod(\"ensureIndex\", IndexDefinition.class);\n\t\t\t\tmethod.invoke(indexOps, index);\n\t\t\t\tlogger.debug(\"Created index using ensureIndex() method\");\n\t\t\t}\n\t\t\tcatch (NoSuchMethodException ensureIndexNotFound) {\n\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\t\"Neither createIndex() nor ensureIndex() method found on IndexOperations. \"\n\t\t\t\t\t\t\t\t+ \"This may indicate an unsupported Spring Data MongoDB version.\",\n\t\t\t\t\t\tensureIndexNotFound);\n\t\t\t}\n\t\t\tcatch (ReflectiveOperationException ex) {\n\t\t\t\tthrow new IllegalStateException(\"Failed to invoke ensureIndex() method\", ex);\n\t\t\t}\n\t\t}\n\t\tcatch (ReflectiveOperationException ex) {\n\t\t\tthrow new IllegalStateException(\"Failed to invoke createIndex() method\", ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":117,"sourceCodeEnd":140,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java#L117-L140","documentation":"This is the first branch of the same reflective index creation: when createIndex(IndexDefinition) is looked up successfully on IndexOperations but invoking it throws a ReflectiveOperationException, the Mongo chat memory auto-configuration wraps it in an IllegalStateException with this message. The real failure is in the chained cause.","triggerScenarios":"createIndexSafely resolves Method createIndex(IndexDefinition) but method.invoke(indexOps, index) throws IllegalAccessException or InvocationTargetException while called from createMainIndex or createOrUpdateTtlIndex at startup.","commonSituations":"MongoDB rejecting the index specification (bad keys, unsupported options); insufficient write permissions on the collection; driver/server version mismatch on index options like partialFilterExpression or expireAfterSeconds.","solutions":["Read the wrapped cause exception to find the actual MongoDB error.","Confirm the index definition (field, direction, TTL options) is valid for your server version.","Ensure the database user can create indexes on the chat memory collection.","Align spring-data-mongodb and mongodb-driver versions with the Spring AI BOM."],"exampleFix":"// before\ncatch (ReflectiveOperationException ex) { throw new IllegalStateException(\"Failed to invoke createIndex() method\", ex); } // cause hidden\n// after\ncatch (ReflectiveOperationException ex) { logger.error(\"createIndex failed: {}\", ex.getCause()); throw new IllegalStateException(\"Failed to invoke createIndex() method\", ex); }","handlingStrategy":"try-catch","validationCode":"// pre-check: collection is writable and driver/server compatible\nmongoTemplate.collectionExists(\"ai_chat_memory\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate index key names and options before deploy.","Ensure DB user has index-creation permissions.","Keep mongodb driver and spring-data-mongodb versions aligned via the BOM."],"tags":["mongodb","reflection","index","spring-data"],"backgroundTag":"database-write-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}