Skip to content

Commit

Permalink
Add validation to catch constraint violation and ignore gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiyamSanthosh committed Jun 6, 2024
1 parent 4b4d6bf commit 5e00294
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.context.SessionContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
import org.wso2.carbon.identity.application.authentication.framework.exception.PostAuthenticationFailedException;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
Expand Down Expand Up @@ -898,7 +899,17 @@ private void storeSessionData(AuthenticationContext context, String sessionConte
user.getUserStoreDomain(), user.getUserName());
if (StringUtils.isNotEmpty(localUserId) &&
!UserSessionStore.getInstance().isExistingMapping(localUserId, sessionContextKey)) {
UserSessionStore.getInstance().storeUserSessionData(localUserId, sessionContextKey);
try {
UserSessionStore.getInstance().storeUserSessionData(localUserId, sessionContextKey);
} catch (DuplicatedAuthUserException e) {
// If isExistingMapping return false due to a database write latency issue,
// the same user to session mapping will be persisted from the same node handling the
// request. Thus, persisting the user to session mapping can be gracefully ignored here.
if (log.isDebugEnabled()) {
log.debug("Mapping between session Id: " + sessionContextKey + " and user Id: "
+ userId + " is already persisted.");
}
}
}
}
} catch (UserSessionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ public void storeUserSessionData(String userId, String sessionId) throws UserSes
if (log.isDebugEnabled()) {
log.debug("Stored user session data for user " + userId + " with session id: " + sessionId);
}
} catch (SQLIntegrityConstraintViolationException e1) {
IdentityDatabaseUtil.rollbackTransaction(connection);
if (isExistingMapping(userId, sessionId)) {
throw new DuplicatedAuthUserException("Mapping between user Id: " + userId + " and session Id: "
+ sessionId + " already exists in the database.", e1);
} else {
throw new UserSessionException("Error while storing mapping between user Id: " + userId +
" and session Id: " + sessionId, e1);
}
} catch (SQLException e1) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw new UserSessionException("Error while storing mapping between user Id: " + userId +
Expand Down

0 comments on commit 5e00294

Please sign in to comment.