connectionId = RegistryUtils.getConnectionId(conn);
}
RegistryCacheKey key =
RegistryUtils.buildRegistryCacheKey(connectionId,
CurrentSession.getTenantId(), path);
RegistryCacheEntry result = (RegistryCacheEntry) pathCache.get(key);
// TODO: FIX: Path Cache should only be updated if the key yields a valid registry path.
// Recently, this has lead to:
// org.wso2.carbon.registry.core.exceptions.RegistryException: Failed to add resource to
// path /_system. Cannot add or update a child row: a foreign key constraint fails
// (`stratos_db`.`REG_RESOURCE`, CONSTRAINT `REG_RESOURCE_FK_BY_PATH_ID` FOREIGN KEY
// (`REG_PATH_ID`, `REG_TENANT_ID`) REFERENCES `REG_PATH` (`REG_PATH_ID`, `REG_TENANT_ID`))
//
// when registry separation is enabled. Thus, we need a better solution to address this, and
// a better key which also contains the name of the DB in use. Un-comment the below once
// this has been done.
//
// IMPORTANT: Never remove this comment until we are certain that the current fix is
// actually working - Senaka.
if (result != null) {
return result.getPathId();
} else {
ResultSet results = null;
PreparedStatement ps = null;
try {
String sql =
"SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE=? " +
"AND REG_TENANT_ID=?";
ps = conn.prepareStatement(sql);
ps.setString(1, path);
ps.setInt(2, CurrentSession.getTenantId());
results = ps.executeQuery();
int pathId;
if (results.next()) {
pathId = results.getInt(DatabaseConstants.PATH_ID_FIELD);
if (pathId > 0) {
RegistryCacheEntry e = new RegistryCacheEntry(pathId);
pathCache.put(key, e);
return pathId;
}
}
} catch (SQLException e) {
String msg = "Failed to retrieving resource from " + path + ". " + e.getMessage();
log.error(msg, e);
throw e;
} finally {
try {
try {
if (results != null) {
results.close();
}
} finally {
if (ps != null) {
ps.close();
}
}
} catch (SQLException e) {
String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR +
e.getMessage();
log.error(msg, e);
}
}
}
return -1;