}
public ServiceDO getService(int serverID, String serviceName) throws BAMException {
ServiceDO cashService = BAMConfigurationCache.getService(serverID, serviceName);
ServiceDO service = null;
BAMConfigurationDSClient configurationDSClient = null;
try {
if (cashService == null) {
configurationDSClient = BAMUtil.getBAMConfigurationDSClient();
// load service from DB
service = configurationDSClient.getService(serverID, serviceName);
if (service == null) {
// service not there in the DB, hence we need to add it
service = new ServiceDO();
service.setName(serviceName);
service.setServerID(serverID);
// This is to guard against occasional race condition of service
// being added while operation is also being added.
boolean raceCondition = false;
try {
configurationDSClient.addService(service);
BAMConfigurationCache.addService(service);
} catch (Exception ignore) {
// In this case the service should have already been added
// to the DB, thus the following getService
// now would definitely retrieve it the service.
log.error("Race condition - trying to add the same service from two events." +
" Attempting to recover...");
raceCondition = true;
}
service = configurationDSClient.getService(serverID, serviceName);
if (raceCondition && service != null) {
log.info("Recovered from race condition. " + serviceName + " successfully added!");
} else if (raceCondition && service == null) {
log.error("Failed to recover from race condition, in adding service " + serviceName);
}
}
}
return service;
} catch (BAMException e) {
throw e;
} finally {
if (configurationDSClient != null) {
configurationDSClient.cleanup();
}
}
}