{"record":{"id":"0f26e64297afdc96","repo":"spring-projects/spring-ai","slug":"failed-to-invoke-ensureindex-method","errorCode":null,"errorMessage":"Failed to invoke ensureIndex() method","messagePattern":"Failed to invoke ensureIndex\\(\\) 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":131,"sourceCode":"\t\t\tMethod method = IndexOperations.class.getMethod(\"createIndex\", IndexDefinition.class);\n\t\t\tmethod.invoke(indexOps, index);\n\t\t\tlogger.debug(\"Created index using createIndex() method\");\n\t\t}\n\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":113,"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#L113-L140","documentation":"After locating IndexOperations#ensureIndex via reflection, the Mongo chat memory index creator invokes it. Any ReflectiveOperationException during that invocation (IllegalAccessException, or InvocationTargetException wrapping the real failure) is rethrown as IllegalStateException with this message and the reflective cause attached. The index was found but could not actually be created.","triggerScenarios":"createIndexSafely (called by createMainIndex/createOrUpdateTtlIndex) finds ensureIndex(IndexDefinition) but method.invoke(indexOps, index) throws — e.g. the underlying MongoDB driver rejects the index definition, the method is inaccessible, or the wrapped invocation fails.","commonSituations":"MongoDB server rejecting the index keys/options (invalid field names, collation issues); IndexOperations bound to a database the app cannot write to; incompatible IndexDefinition type for the resolved method; security manager or module access restrictions.","solutions":["Inspect the chained cause (InvocationTargetException target) for the real MongoDB/ driver error.","Verify the MongoDB user has createIndex privileges on the target database/collection.","Check the configured index definition (field names, TTL seconds) is valid for your MongoDB server version.","Align Spring Data MongoDB version with Spring AI's expectations so the correct API path is used."],"exampleFix":"// before\nlogger.info(\"Creating index with keys: \" + index.getIndexKeys()); // opaque\n// after\ntry { indexCreator.createIndexSafely(indexOps, index); } catch (IllegalStateException e) { logger.error(\"Index creation failed: \", e.getCause()); throw e; }","handlingStrategy":"try-catch","validationCode":"// pre-check permissions and connectivity\nmongoTemplate.getDb().runCommand(new Document(\"connectionStatus\", 1));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grant the MongoDB user createIndex privileges on the chat memory collection.","Validate index definitions (keys, TTL) against your MongoDB server version.","Log the cause chain, not just the wrapper message."],"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"}