{"record":{"id":"ea7b4fdbf5aaec7c","repo":"spring-projects/spring-security","slug":"n-users-found-with-name-x-expected-1","errorCode":null,"errorMessage":"[N] users found with name 'X', expected 1","messagePattern":"\\[N\\] users found with name 'X', expected 1","errorType":"exception","errorClass":"IncorrectResultSizeDataAccessException","httpStatus":500,"severity":"error","filePath":"core/src/main/java/org/springframework/security/provisioning/JdbcUserDetailsManager.java","lineNumber":355,"sourceCode":"\t\tcontext.setAuthentication(authentication);\n\t\tthis.securityContextHolderStrategy.setContext(context);\n\t\tthis.userCache.removeUserFromCache(username);\n\t}\n\n\tprotected Authentication createNewAuthentication(Authentication currentAuth, @Nullable String newPassword) {\n\t\tUserDetails user = loadUserByUsername(currentAuth.getName());\n\t\tUsernamePasswordAuthenticationToken newAuthentication = UsernamePasswordAuthenticationToken.authenticated(user,\n\t\t\t\tnull, user.getAuthorities());\n\t\tnewAuthentication.setDetails(currentAuth.getDetails());\n\t\treturn newAuthentication;\n\t}\n\n\t@Override\n\tpublic boolean userExists(String username) {\n\t\t@SuppressWarnings(\"ConstantConditions\")\n\t\tInteger usersCount = requireJdbcTemplate().queryForObject(this.userExistsSql, Integer.class, username);\n\t\tif (usersCount == null || usersCount > 1) {\n\t\t\tthrow new IncorrectResultSizeDataAccessException(\n\t\t\t\t\t\"[\" + usersCount + \"] users found with name '\" + username + \"', expected 1\", 1);\n\t\t}\n\t\treturn usersCount == 1;\n\t}\n\n\t@Override\n\tpublic List<String> findAllGroups() {\n\t\t// @formatter:off\n\t\treturn requireJdbcTemplate().queryForList(this.findAllGroupsSql, String.class)\n\t\t\t.stream()\n\t\t\t.filter(Objects::nonNull)\n\t\t\t.collect(Collectors.toList());\n\t\t// @formatter:on\n\t}\n\n\t@Override\n\tpublic List<String> findUsersInGroup(String groupName) {\n\t\tAssert.hasText(groupName, \"groupName should have text\");","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/core/src/main/java/org/springframework/security/provisioning/JdbcUserDetailsManager.java#L337-L373","documentation":"JdbcUserDetailsManager.userExists expects exactly zero or one row for the username; if the count query returns null or more than 1, it throws IncorrectResultSizeDataAccessException with the message '[N] users found with name X, expected 1'. This guards the invariant that usernames are unique in the users table, since duplicate rows make 'does this user exist' and subsequent loads ambiguous.","triggerScenarios":"The users table contains duplicate rows for the same username (missing unique constraint on the username column); running against a schema where users is duplicated by joins or seeding scripts inserted the user twice; query configured via setUsersByUsernameQuery returning multiple rows per user.","commonSituations":"Manual data migration or fixtures inserting the same username twice; schemas missing PRIMARY KEY/UNIQUE on username; multi-tenant tables queried without tenant filtering in the custom query.","solutions":["Add a UNIQUE constraint (or PRIMARY KEY) on the username column of the users table","Deduplicate existing rows: DELETE duplicates keeping one row per username","Ensure the configured users-by-username query cannot return more than one row per user (add filters/limits as appropriate)","Catch IncorrectResultSizeDataAccessException around userExists if duplicates are legitimately possible in your environment"],"exampleFix":"// before\n-- no constraint, duplicate usernames possible\nCREATE TABLE users(username VARCHAR(50), ...);\n// after\nCREATE TABLE users(username VARCHAR(50) NOT NULL PRIMARY KEY, ...);","handlingStrategy":"try-catch","validationCode":"Integer count = jdbcTemplate.queryForObject(\n    \"SELECT COUNT(*) FROM users WHERE username = ?\", Integer.class, username);\nif (count != null && count > 1) {\n    throw new IllegalStateException(\"Duplicate username rows in users table: \" + username);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return manager.userExists(username);\n} catch (IncorrectResultSizeDataAccessException e) {\n    // duplicates detected: trigger data-cleanup/ops alert\n    return false;\n}","preventionTips":["Enforce a UNIQUE/PK constraint on the users.username column","Deduplicate and validate seed/migration scripts before running them","Keep the custom users-by-username query returning at most one row per user","Monitor for IncorrectResultSizeDataAccessException as a data-integrity signal"],"tags":["jdbc","duplicate-data","database","uniqueness"],"backgroundTag":"database-query-failed","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}