use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterManagerImpl method getOrCreateCluster.
@Override
public Cluster getOrCreateCluster(String clusterName, DalConfigCustomizedOption customizedOption) {
if (StringUtils.isEmpty(clusterName))
throw new DalRuntimeException("cluster name is empty");
clusterName = StringUtils.toTrimmedLowerCase(clusterName);
Cluster cluster = clusters.get(clusterName);
if (cluster == null)
synchronized (clusters) {
cluster = clusters.get(clusterName);
if (cluster == null) {
cluster = createCluster(clusterName, customizedOption);
clusters.put(clusterName, cluster);
}
}
return cluster;
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterConfigAdapter method load.
private void load(DalConnectionStringConfigure configure) {
ClusterConfig clusterConfig;
if (configure instanceof InvalidVariableConnectionString) {
throw new DalRuntimeException("connectionString invalid for db: " + provider.getDbName(), ((InvalidVariableConnectionString) configure).getConnectionStringException());
}
if (configure == null)
throw new RuntimeException("Get null config from mysqlapi for db: " + provider.getDbName());
if (configure instanceof MultiHostConnectionStringConfigure)
clusterConfig = buildMultiHostClusterConfig((MultiHostConnectionStringConfigure) configure);
else
clusterConfig = buildNormalClusterConfig(configure);
clusterConfigRef.getAndSet(clusterConfig);
DalConnectionStringConfigure prev = connStrConfigRef.getAndSet(configure);
if (prev != null && !equals(prev, configure))
for (Listener<ClusterConfig> listener : getListeners()) {
try {
listener.onChanged(this);
} catch (Throwable t) {
// ignore
}
}
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterDynamicDataSource method getCustomDataSourceFactory.
private CustomDataSourceFactory getCustomDataSourceFactory() {
DalConfigCustomizedOption customizedOption = cluster.getCustomizedOption();
String clazz = null;
if (customizedOption != null) {
clazz = customizedOption.getDataSourceFactory();
}
if (StringUtils.isEmpty(clazz)) {
Properties properties = cluster.getCustomProperties();
clazz = properties.getProperty(DATASOURCE_FACTORY);
}
try {
return (CustomDataSourceFactory) Class.forName(clazz).newInstance();
} catch (Exception e) {
throw new DalRuntimeException("Construct CustomDataSourceFactory error", e);
}
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterDynamicDataSource method forceSwitch.
@Override
public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
synchronized (lock) {
SwitchableDataSourceStatus currentStatus = getStatus();
ForceSwitchedStatus prevStatus = status.getAndSet(ForceSwitchedStatus.ForceSwitching);
try {
String logName = String.format(FORCE_SWITCH, clusterInfo.toString());
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("newIp: %s, newPort: %s", ip, port), () -> {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", currentStatus.isForceSwitched(), currentStatus.isPoolCreated()));
getExecutor().submit(() -> {
try {
DataSource newDataSource;
if (DatabaseCategory.CUSTOM == cluster.getDatabaseCategory()) {
newDataSource = createCustomDataSource();
} else {
DataSourceConfigure dataSourceConfig = getSingleDataSource().getDataSourceConfigure().clone();
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("previous host(s): %s:%s", currentStatus.getHostName(), currentStatus.getPort()));
dataSourceConfig.replaceURL(ip, port);
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new host(s): %s:%s", ip, port));
newDataSource = new RefreshableDataSource(dataSourceId, dataSourceConfig);
}
switchDataSource(newDataSource);
status.set(ForceSwitchedStatus.ForceSwitched);
currentHost.set(new HostAndPort(null, ip, port));
} catch (Throwable t) {
LOGGER.error("DataSource creation failed", t);
// TODO: handle pool creation failure
status.set(prevStatus);
}
});
});
return currentStatus;
} catch (Throwable t) {
status.set(prevStatus);
LOGGER.error("Force switch error", t);
throw new DalRuntimeException("Force switch error", t);
}
}
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class DefaultClusterRouteStrategyConfig method generate.
@Override
public RouteStrategy generate() {
String strategyName = routeStrategyName();
String clazz = RouteStrategyEnum.parse(strategyName);
try {
return (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) Class.forName(clazz).newInstance();
} catch (Throwable t) {
String msg = "Error constructing route strategy: " + strategyName;
throw new DalRuntimeException(msg, t);
}
}
Aggregations