-
Notifications
You must be signed in to change notification settings - Fork 712
Remove the pre-update lock on a user's attribute rows #4624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sadilchamishka
wants to merge
4
commits into
wso2:4.12.x
Choose a base branch
from
sadilchamishka:fix/seekable-claim-update-predicate
base: 4.12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dae7bce
Remove the pre-update lock on a user's attribute update and improve a…
sadilchamishka 9f367a7
Create the new index only when it does not already exist
sadilchamishka 4ac7a0a
Drop the SQL Server cast, which the index makes unnecessary
sadilchamishka 030d306
Reinstate the SQL Server cast
sadilchamishka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...n.user.core/src/test/java/org/wso2/carbon/user/core/jdbc/ClaimUpdatePredicateSQLTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.wso2.carbon.user.core.jdbc; | ||
|
|
||
| import org.testng.annotations.Test; | ||
| import org.wso2.carbon.user.core.util.JDBCRealmUtil; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * Covers the SQL Server claim update path, where a regression is silent: nothing throws and nothing | ||
| * logs, the statement just stops seeking and starts locking the user's whole attribute set again. | ||
| */ | ||
| public class ClaimUpdatePredicateSQLTest { | ||
|
|
||
| @Test | ||
| public void testMSSQLStatementCastsOnlyTheVarcharPredicates() { | ||
|
|
||
| String sql = JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL_SQL; | ||
| for (String column : new String[] { "UM_ATTR_NAME", "UM_PROFILE_ID", "UM_USER_ID" }) { | ||
| assertTrue(sql.contains(column + "=CAST(? AS VARCHAR"), | ||
| column + " is a VARCHAR column and must be compared against a VARCHAR parameter, otherwise the " | ||
| + "conversion lands on the column and the predicate cannot seek: " + sql); | ||
| } | ||
| assertTrue(sql.startsWith("UPDATE UM_USER_ATTRIBUTE SET UM_ATTR_VALUE=? "), | ||
| "UM_ATTR_VALUE is NVARCHAR and is bound as Unicode where the user store asks for it, so casting it " | ||
| + "would silently mangle non-ASCII claim values: " + sql); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMSSQLStatementKeepsTheGenericParameterOrder() { | ||
|
|
||
| String generic = JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_SQL; | ||
| String mssql = JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL_SQL; | ||
| assertEquals(mssql.replaceAll("CAST\\(\\? AS VARCHAR\\(\\d+\\)\\)", "?").replaceAll("\\s+", " ").trim(), | ||
| generic.replaceAll("\\s+", " ").trim(), | ||
| "Both statements are bound by the same code, in a fixed parameter order, so the SQL Server variant " | ||
| + "must differ from the generic one only by the casts."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMSSQLStatementIsRegisteredUnderTheKeyTheLookupUses() { | ||
|
|
||
| assertEquals(JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL, | ||
| JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED + "-mssql", | ||
| "updateProperties looks the statement up as the property name plus the database type, so the two " | ||
| + "must agree - if they drift, the cast is never read and nothing reports it."); | ||
| assertEquals(JDBCRealmUtil.getSQL(new HashMap<>()) | ||
| .get(JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL), | ||
| JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL_SQL, | ||
| "A user store that overrides nothing must pick up the SQL Server variant."); | ||
| assertFalse(JDBCRealmUtil.getSQL(new HashMap<>(Map.of( | ||
| JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED, "UPDATE the store's own SQL"))) | ||
| .containsKey(JDBCRealmConstants.UPDATE_USER_PROPERTY_WITH_ID_OPTIMIZED_MSSQL), | ||
| "The SQL Server variant is looked up first, so it must not be added to a user store that configured " | ||
| + "its own generic statement - that would override what was configured."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTheBatchIsOrderedIndependentlyOfTheCallersMap() { | ||
|
|
||
| Map<String, String> properties = new LinkedHashMap<>(); | ||
| properties.put("http://wso2.org/claims/mobile", "1"); | ||
| properties.put("http://wso2.org/claims/emailaddress", "2"); | ||
| properties.put("http://wso2.org/claims/country", "3"); | ||
|
|
||
| Map<String, String> reversed = new LinkedHashMap<>(); | ||
| properties.entrySet().stream().sorted((a, b) -> b.getKey().compareTo(a.getKey())) | ||
| .forEach(e -> reversed.put(e.getKey(), e.getValue())); | ||
|
|
||
| assertEquals(claimOrder(properties), claimOrder(reversed), | ||
| "Two callers passing the same claims in different orders must produce the same batch order, " | ||
| + "otherwise they can take the same row locks in opposite orders and deadlock."); | ||
| assertEquals(claimOrder(properties), Arrays.asList("http://wso2.org/claims/country", | ||
| "http://wso2.org/claims/emailaddress", "http://wso2.org/claims/mobile")); | ||
| } | ||
|
|
||
| private List<String> claimOrder(Map<String, String> properties) { | ||
|
|
||
| return UniqueIDJDBCUserStoreManager.orderClaimsForBatch(properties).stream() | ||
| .map(Map.Entry::getKey).collect(Collectors.toList()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There can be cases sql connection is configured with parameters
SendStringParametersAsUnicode=truewhen unicode support is provided.Though unicodes are supported for values, the keys will be still not unicode characters. Hence this change avoid the jdbc driver to convert them to nvarchar which then cause to miss the indexes which have been configured according to their data type of varchar.