Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #446 from hpmtissera/fix_db_connection_issues
Browse files Browse the repository at this point in the history
Fix db connection issues
  • Loading branch information
hpmtissera committed Jun 17, 2015
2 parents a6731ae + d859d78 commit 1faa630
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,7 @@ public ApplicationBasicInfo[] getAllApplicationBasicInfo()
}
}
}
connection.commit();
} catch (SQLException | IdentityException e) {
throw new IdentityApplicationManagementException("Error while Reading all Applications");
} finally {
Expand Down Expand Up @@ -2412,6 +2413,7 @@ public String getServiceProviderNameByClientId(String clientId, String clientTyp
if (appNameResult.next()) {
applicationName = appNameResult.getString(1);
}
connection.commit();
} catch (SQLException | IdentityException e) {
throw new IdentityApplicationManagementException("Error while reading application");
} finally {
Expand Down Expand Up @@ -2470,7 +2472,7 @@ private Map<String, String> getClaimMapping(String serviceProviderName, String t
claimMapping.put(resultSet.getString(2), resultSet.getString(1));
}
}

connection.commit();
} catch (IdentityException e) {
throw new IdentityApplicationManagementException("Error while reading claim mappings.", e);
} finally {
Expand Down Expand Up @@ -2537,12 +2539,12 @@ public List<String> getAllRequestedClaimsByServiceProvider(String serviceProvide
getClaimPreStmt.setString(1, CharacterEncoder.getSafeText(serviceProviderName));
getClaimPreStmt.setInt(2, tenantID);
resultSet = getClaimPreStmt.executeQuery();

while (resultSet.next()) {
if ("1".equalsIgnoreCase(resultSet.getString(3))) {
reqClaimUris.add(resultSet.getString(1));
}
}
connection.commit();
} catch (SQLException | IdentityException e) {
throw new IdentityApplicationManagementException(
"Error while retrieving requested claims", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void removeOAuthApplication(String clientIdentifier) throws IdentityAppli
prepStmt.setString(1, clientIdentifier);
prepStmt.execute();
connection.commit();

} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ private boolean isExistingUserDataValue(String userName, int tenantId, String ke
return true;
}
connection.commit();
} catch (SQLException e) {
log.error("Error while retrieving user identity data in database", e);
throw new IdentityException("Error while retrieving user identity data in database", e);
} catch (IdentityException e) {
} catch (SQLException | IdentityException e) {
log.error("Error while retrieving user identity data in database", e);
throw new IdentityException("Error while retrieving user identity data in database", e);
} finally {
Expand All @@ -128,10 +125,7 @@ private void addUserDataValue(String userName, int tenantId, String key, String
prepStmt.setString(4, value);
prepStmt.execute();
connection.commit();
} catch (SQLException e) {
log.error("Error while persisting user identity data in database", e);
throw new IdentityException("Error while persisting user identity data in database", e);
} catch (IdentityException e) {
} catch (SQLException | IdentityException e) {
log.error("Error while persisting user identity data in database", e);
throw new IdentityException("Error while persisting user identity data in database", e);
} finally {
Expand All @@ -155,10 +149,7 @@ private void updateUserDataValue(String userName, int tenantId, String key, Stri
prepStmt.setString(4, key);
prepStmt.executeUpdate();
connection.commit();
} catch (SQLException e) {
log.error("Error while persisting user identity data in database", e);
throw new IdentityException("Error while persisting user identity data in database", e);
} catch (IdentityException e) {
} catch (SQLException | IdentityException e) {
log.error("Error while persisting user identity data in database", e);
throw new IdentityException("Error while persisting user identity data in database", e);
} finally {
Expand Down Expand Up @@ -203,12 +194,9 @@ public UserIdentityClaimsDO load(String userName, UserStoreManager userStoreMana
}
dto = new UserIdentityClaimsDO(userName, data);
dto.setTenantId(tenantId);
connection.commit();
return dto;
} catch (SQLException e) {
log.error("Error while reading user identity data", e);
} catch (UserStoreException e) {
log.error("Error while reading user identity data", e);
} catch (IdentityException e) {
} catch (SQLException | IdentityException | UserStoreException e) {
log.error("Error while reading user identity data", e);
} finally {
IdentityDatabaseUtil.closeResultSet(results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public void invalidate(UserRecoveryDataDO recoveryDataDO) throws IdentityExcepti
prepStmt.setInt(2, recoveryDataDO.getTenantId());
prepStmt.setString(3, recoveryDataDO.getCode());
prepStmt.execute();
connection.setAutoCommit(false);
connection.commit();
} catch (SQLException e) {
throw new IdentityException("Error while storing user identity data", e);
Expand All @@ -79,7 +78,6 @@ public void invalidate(String userId, int tenant) throws IdentityException {
prepStmt = connection.prepareStatement(SQLQuery.INVALIDATE_METADATA);
prepStmt.setString(1, userId);
prepStmt.setInt(2, tenant);
connection.setAutoCommit(false);
connection.commit();
} catch (SQLException e) {
throw new IdentityException("Error while invalidating user identity data", e);
Expand Down Expand Up @@ -179,6 +177,7 @@ public UserRecoveryDataDO[] load(String userName, int tenantId) throws IdentityE
results.getString(3), results.getString(4)));
}
UserRecoveryDataDO[] resultMetadata = new UserRecoveryDataDO[metada.size()];
connection.commit();
return metada.toArray(resultMetadata);
} catch (SQLException e) {
throw new IdentityException("Error while reading user identity data", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public void storeMetadata(IdentityMetadataDO metadata) throws IdentityException
prepStmt.setString(4, metadata.getMetadata());
prepStmt.setString(5, Boolean.toString(metadata.isValid()));
prepStmt.execute();
connection.setAutoCommit(false);
connection.commit();
} catch (SQLException e) {
throw new IdentityException("Error while storing user identity data", e);
Expand Down Expand Up @@ -177,7 +176,7 @@ public IdentityMetadataDO loadMetadata(String userName, int tenantId, String met
prepStmt.setString(3, metadataType);
prepStmt.setString(4, metadata);
results = prepStmt.executeQuery();

connection.commit();
if (results.next()) {
return new IdentityMetadataDO(results.getString(1), results.getInt(2),
results.getString(3), results.getString(4),
Expand Down Expand Up @@ -220,6 +219,7 @@ public IdentityMetadataDO[] loadMetadata(String userName, int tenantId)
Boolean.parseBoolean(results.getString(5))));
}
IdentityMetadataDO[] resultMetadata = new IdentityMetadataDO[metada.size()];
connection.commit();
return metada.toArray(resultMetadata);
} catch (SQLException e) {
throw new IdentityException("Error while reading user identity data", e);
Expand Down Expand Up @@ -256,6 +256,7 @@ public IdentityMetadataDO[] loadMetadata(String userName, int tenantId, String m
Boolean.parseBoolean(results.getString(5))));
}
IdentityMetadataDO[] resultMetadata = new IdentityMetadataDO[metada.size()];
connection.commit();
return metada.toArray(resultMetadata);
} catch (SQLException e) {
throw new IdentityException("Error while reading user identity data", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public List<IdentityProvider> getIdPs(Connection dbConnection, int tenantId, Str
idps.add(identityProvider);
}
}
dbConnection.commit();
return idps;
} catch (SQLException | IdentityException e) {
log.error(e.getMessage(), e);
Expand Down Expand Up @@ -1020,6 +1021,7 @@ public IdentityProvider getIdPByName(Connection dbConnection, String idPName, in
dbConnection, idPName, idpId, tenantId));

}
dbConnection.commit();
return federatedIdp;
} catch (SQLException | IdentityException e) {
IdentityApplicationManagementUtil.rollBack(dbConnection);
Expand Down Expand Up @@ -1169,6 +1171,7 @@ public IdentityProvider getIdPByAuthenticatorPropertyValue(Connection dbConnecti
dbConnection, idPName, idpId, tenantId));

}
dbConnection.commit();
return federatedIdp;
} catch (SQLException | IdentityException e) {
IdentityApplicationManagementUtil.rollBack(dbConnection);
Expand Down Expand Up @@ -1213,6 +1216,7 @@ public IdentityProvider getIdPByRealmId(String realmId, int tenantId, String ten
IdentityApplicationManagementUtil.closeStatement(prepStmt);
IdentityApplicationManagementUtil.closeResultSet(rs);

dbConnection.commit();
return getIdPByName(dbConnection, idPName, tenantId, tenantDomain);
} catch (SQLException | IdentityException e) {
throw new IdentityApplicationManagementException(
Expand Down Expand Up @@ -1602,6 +1606,7 @@ public boolean isIdpReferredBySP(String idPName, int tenantId)
isReffered = rsProvIdp.getInt(1) > 0;
}
}
dbConnection.commit();
} catch (SQLException | IdentityException e) {
log.error(e.getMessage(), e);
String msg = "Error occurred while searching for IDP references in SP ";
Expand Down Expand Up @@ -1681,6 +1686,7 @@ public IdentityProvider getPrimaryIdP(Connection dbConnection, int tenantId, Str
prepStmt.setInt(1, tenantId);
prepStmt.setString(2, "1");
ResultSet rs = prepStmt.executeQuery();
dbConnection.commit();
if (rs.next()) {
IdentityProvider identityProviderDO = new IdentityProvider();
identityProviderDO.setIdentityProviderName(rs.getString(1));
Expand Down Expand Up @@ -2384,6 +2390,7 @@ private int getIdentityProviderIdByName(Connection dbConnection, String idpName,
prepStmt.setInt(2, MultitenantConstants.SUPER_TENANT_ID);
prepStmt.setString(3, CharacterEncoder.getSafeText(idpName));
rs = prepStmt.executeQuery();
dbConnection.commit();
if (rs.next()) {
return rs.getInt(1);
}
Expand Down Expand Up @@ -2489,6 +2496,7 @@ public boolean isSimilarIdPEntityIdsAvailble(String idPEntityId, int tenantId)
if (rs.next()) {
isAvailable = rs.getInt(1) > 0;
}
dbConnection.commit();
} catch (SQLException | IdentityException e) {
log.error(e.getMessage(), e);
String msg = "Error occurred while searching for similar IdP EntityIds";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public OAuthAppDO[] getOAuthConsumerAppsOfUser(String username, int tenantId) th
}
}
oauthAppsOfUser = oauthApps.toArray(new OAuthAppDO[oauthApps.size()]);
connection.commit();
} catch (IdentityException e) {
throw new IdentityOAuthAdminException("Error when getting an Identity Persistence Store instance.", e);
} catch (SQLException e) {
Expand Down Expand Up @@ -223,6 +224,7 @@ public OAuthAppDO getAppInformation(String consumerKey) throws InvalidOAuthClien

throw new InvalidOAuthClientException("Cannot find an application associated with the given consumer key : " + consumerKey);
}
connection.commit();
} catch (IdentityException e) {
throw new IdentityOAuth2Exception("Error while retrieving database connection" ,e);
} catch (SQLException e) {
Expand Down Expand Up @@ -280,6 +282,7 @@ public OAuthAppDO getAppInformationByAppName(String appName) throws InvalidOAuth
log.debug(message);
throw new InvalidOAuthClientException(message);
}
connection.commit();
} catch (IdentityException e) {
throw new IdentityOAuth2Exception("Error while retrieving database connection", e);
} catch (SQLException e) {
Expand Down Expand Up @@ -357,6 +360,7 @@ private boolean isDuplicateApplication(String username, int tenantId, OAuthAppDO
if (rSet.next()) {
isDuplicateApp = true;
}
connection.commit();
} catch (IdentityException e) {
throw new IdentityOAuthAdminException("Error when getting an Identity Persistence Store instance.", e);
} catch (SQLException e) {
Expand All @@ -383,6 +387,7 @@ private boolean isDuplicateConsumer(String consumerKey) throws IdentityOAuthAdmi
if (rSet.next()) {
isDuplicateConsumer = true;
}
connection.commit();
} catch (IdentityException e) {
throw new IdentityOAuthAdminException("Error when getting an Identity Persistence Store instance.", e);
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public String getOAuthConsumerSecret(String consumerKey) throws IdentityOAuthAdm
} else {
log.debug("Invalid Consumer Key : " + consumerKey);
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -117,6 +118,7 @@ public String getAuthenticatedUsername(String clientId, String clientSecret) thr
} else {
log.debug("Invalid client id : " + clientId + ", and consumer secret : " + clientSecret);
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -160,6 +162,7 @@ public String getOAuthTokenSecret(String token, Boolean isAccessToken) throws Id
prepStmt = connection.prepareStatement(sqlStmt);
prepStmt.setString(1, token);
resultSet = prepStmt.executeQuery();
connection.commit();

if (resultSet.next()) {
tokenSecret = resultSet.getString(1);
Expand Down Expand Up @@ -298,7 +301,7 @@ public Parameters getRequestToken(String oauthToken) throws IdentityOAuthAdminEx
log.error("Invalid request token : " + oauthToken);
throw new IdentityException("Invalid request token. No such token issued.");
}

connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -381,6 +384,7 @@ public String validateAccessToken(String consumerKey, String oauthToken, String
} else {
throw new IdentityException("Invalid access token. No such token issued.");
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -415,6 +419,7 @@ private String getCallbackURLOfApp(String consumerKey) throws IdentityOAuthAdmin
if (resultSet.next()) {
callbackURL = resultSet.getString(1);
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -445,6 +450,7 @@ private String getCallbackURLOfReqToken(String oauthToken) throws IdentityOAuthA
if (resultSet.next()) {
callbackURL = resultSet.getString(1);
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ public AccessTokenDO retrieveLatestAccessToken(String consumerKey, String userNa
prepStmt.setString(3, scope);
}
resultSet = prepStmt.executeQuery();

connection.commit();

if (resultSet.next()) {
boolean returnToken = false;
String tokenState = resultSet.getString(7);
Expand Down Expand Up @@ -360,7 +361,7 @@ public Set<AccessTokenDO> retrieveAccessTokens(String consumerKey, String userNa

Set<AccessTokenDO> accessTokenDOs = new HashSet<AccessTokenDO>();

Connection connection = null;
Connection connection;
try {
connection = JDBCPersistenceManager.getInstance().getDBConnection();
} catch (IdentityException e) {
Expand Down Expand Up @@ -404,6 +405,7 @@ public Set<AccessTokenDO> retrieveAccessTokens(String consumerKey, String userNa

accessTokenDOs.add(dataDO);
}
connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance";
throw new IdentityOAuth2Exception(errorMsg, e);
Expand Down Expand Up @@ -446,7 +448,7 @@ public AuthzCodeDO validateAuthorizationCode(String consumerKey, String authoriz
OAuth2Util.buildScopeArray(scopeString),
issuedTime, validityPeriod, callbackUrl);
}

connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -576,7 +578,7 @@ public RefreshTokenValidationDataDO validateRefreshToken(String consumerKey, Str
("UTC"))));
validationDataDO.setValidityPeriodInMillis(resultSet.getLong(6));
}

connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
log.error(errorMsg, e);
Expand Down Expand Up @@ -683,7 +685,7 @@ public AccessTokenDO retrieveAccessToken(String accessTokenIdentifier, boolean i
dataDO.setAccessToken(accessTokenIdentifier);
dataDO.setRefreshToken(refreshToken);
}

connection.commit();
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance";
throw new IdentityOAuth2Exception(errorMsg, e);
Expand Down Expand Up @@ -862,6 +864,7 @@ public String findScopeOfResource(String resourceUri) throws IdentityOAuth2Excep
if (rs.next()) {
return rs.getString("SCOPE_KEY");
}
connection.commit();
return null;
} catch (IdentityException e) {
String errorMsg = "Error when getting an Identity Persistence Store instance.";
Expand Down
Loading

0 comments on commit 1faa630

Please sign in to comment.