Skip to content

Commit e5e6bb3

Browse files
Fix update query not committing issue in RoleDAO
1 parent c88c85a commit e5e6bb3

File tree

1 file changed

+41
-39
lines changed
  • components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao

1 file changed

+41
-39
lines changed

components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAOImpl.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@
9292

9393
import javax.xml.namespace.QName;
9494

95-
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.
96-
ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION_ID;
95+
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION_ID;
9796
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.APPLICATION;
9897
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.CONSOLE_ORG_SCOPE_PREFIX;
9998
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.CONSOLE_SCOPE_PREFIX;
@@ -1024,52 +1023,55 @@ public void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String shar
10241023
String mainRoleTenantDomain, String sharedRoleTenantDomain)
10251024
throws IdentityRoleManagementException {
10261025

1027-
String mainRoleName = getRoleNameByID(mainRoleUUID, mainRoleTenantDomain);
10281026
int mainRoleTenantId = IdentityTenantUtil.getTenantId(mainRoleTenantDomain);
10291027

10301028
String sharedRoleName = getRoleNameByID(sharedRoleUUID, sharedRoleTenantDomain);
10311029
int sharedRoleTenantId = IdentityTenantUtil.getTenantId(sharedRoleTenantDomain);
10321030

10331031
int mainRoleUMId = 0;
10341032
int sharedRoleUMId = 0;
1035-
try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
1036-
try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID_BY_UUID)) {
1037-
stmt.setString(RoleConstants.RoleTableColumns.UM_UUID, mainRoleUUID);
1038-
ResultSet resultSet = stmt.executeQuery();
1039-
while (resultSet.next()) {
1040-
mainRoleUMId = resultSet.getInt(1);
1041-
}
1042-
} catch (SQLException e) {
1043-
String message = "Error while resolving id of role name: %s in the tenantDomain: %s.";
1044-
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
1045-
String.format(message, mainRoleName, mainRoleTenantDomain), e);
1046-
}
1047-
1048-
try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID_BY_UUID)) {
1049-
stmt.setString(RoleConstants.RoleTableColumns.UM_UUID, sharedRoleUUID);
1050-
ResultSet resultSet = stmt.executeQuery();
1051-
while (resultSet.next()) {
1052-
sharedRoleUMId = resultSet.getInt(1);
1053-
}
1054-
} catch (SQLException e) {
1055-
String message = "Error while resolving id of role name: %s in the tenantDomain: %s.";
1056-
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
1057-
String.format(message, sharedRoleName, sharedRoleTenantDomain), e);
1058-
}
1033+
try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(true)) {
10591034

1060-
if (mainRoleUMId == 0 || sharedRoleUMId == 0) {
1061-
String message = "Error while resolving role id.";
1062-
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
1063-
message);
1064-
}
1065-
try (NamedPreparedStatement preparedStatement = new NamedPreparedStatement(connection,
1066-
INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP)) {
1067-
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_ID, sharedRoleUMId);
1068-
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_ID, mainRoleUMId);
1069-
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_TENANT_ID, sharedRoleTenantId);
1070-
preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_TENANT_ID, mainRoleTenantId);
1071-
preparedStatement.executeUpdate();
1035+
try (NamedPreparedStatement statementForGetMainRoleId = new NamedPreparedStatement(connection,
1036+
GET_ROLE_UM_ID_BY_UUID);
1037+
NamedPreparedStatement statementForGetSharedRoleId = new NamedPreparedStatement(connection,
1038+
GET_ROLE_UM_ID_BY_UUID);
1039+
NamedPreparedStatement statementForAddRoleRelationship = new NamedPreparedStatement(connection,
1040+
INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP)) {
1041+
1042+
// Retrieve UM_ID for main role.
1043+
statementForGetMainRoleId.setString(RoleConstants.RoleTableColumns.UM_UUID, mainRoleUUID);
1044+
ResultSet resultSetForMainRole = statementForGetMainRoleId.executeQuery();
1045+
while (resultSetForMainRole.next()) {
1046+
mainRoleUMId = resultSetForMainRole.getInt(1);
1047+
}
1048+
1049+
// Retrieve UM_ID for shared role.
1050+
statementForGetSharedRoleId.setString(RoleConstants.RoleTableColumns.UM_UUID, sharedRoleUUID);
1051+
ResultSet resultSetForSharedRole = statementForGetSharedRoleId.executeQuery();
1052+
while (resultSetForSharedRole.next()) {
1053+
sharedRoleUMId = resultSetForSharedRole.getInt(1);
1054+
}
1055+
1056+
if (mainRoleUMId == 0 || sharedRoleUMId == 0) {
1057+
String message = "Error while resolving role id.";
1058+
throw new IdentityRoleManagementServerException(
1059+
RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
1060+
message);
1061+
}
1062+
1063+
// Add main role to shared role relationship.
1064+
statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_ID,
1065+
sharedRoleUMId);
1066+
statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_ID, mainRoleUMId);
1067+
statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_TENANT_ID,
1068+
sharedRoleTenantId);
1069+
statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_TENANT_ID,
1070+
mainRoleTenantId);
1071+
statementForAddRoleRelationship.executeUpdate();
1072+
IdentityDatabaseUtil.commitUserDBTransaction(connection);
10721073
} catch (SQLException e) {
1074+
IdentityDatabaseUtil.rollbackUserDBTransaction(connection);
10731075
String message = "Error while adding the role relationship of role: %s.";
10741076
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
10751077
String.format(message, sharedRoleName), e);

0 commit comments

Comments
 (0)