use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ForceSwitchableDataSource method forceSwitch.
public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
synchronized (lock) {
final SwitchableDataSourceStatus oldStatus = getStatus();
final String name;
final DataSourceConfigure usedConfigure;
forceSwitchedStatus = ForceSwitchedStatus.ForceSwitching;
try {
if (isNullDataSource) {
if (configure instanceof IDataSourceConfigure) {
usedConfigure = dataSourceConfigureConvert.desDecrypt(DataSourceConfigure.valueOf((IDataSourceConfigure) configure));
name = ((IDataSourceConfigure) configure).getConnectionUrl();
} else {
throw new DalRuntimeException("Force Switch Error: datasource configure is invalid");
}
} else {
usedConfigure = getSingleDataSource().getDataSourceConfigure().clone();
name = getSingleDataSource().getName();
}
final String logName = String.format(FORCE_SWITCH, name);
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("isNullDataSource: %s; newIp: %s, newPort: %s", isNullDataSource, ip, port), new Callback() {
@Override
public void execute() throws Exception {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old connection url: %s", usedConfigure.getConnectionUrl()));
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", oldStatus.isForceSwitched(), oldStatus.isPoolCreated()));
usedConfigure.replaceURL(ip, port);
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new connection url: %s", usedConfigure.getConnectionUrl()));
asyncRefreshDataSource(name, usedConfigure, new ForceSwitchListener() {
public void onCreatePoolSuccess() {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess: %s", name), usedConfigure.getConnectionUrl());
poolCreated = true;
forceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
oldForceSwitchedStatus = ForceSwitchedStatus.ForceSwitched;
isNullDataSource = false;
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolSuccess::notifyListeners: %s", name), "notify listeners' onForceSwitchSuccess");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onForceSwitchSuccess(status);
} catch (Exception e) {
LOGGER.error("call listener.onForceSwitchSuccess() error ", e);
}
}
});
}
public void onCreatePoolFail(final Throwable e) {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail: %s", name), e.getMessage());
forceSwitchedStatus = oldForceSwitchedStatus;
final SwitchableDataSourceStatus status = getStatus();
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format("onCreatePoolFail::notifyListeners: %s", name), "notify listeners' onForceSwitchFail");
for (final SwitchListener listener : listeners) executor.submit(new Runnable() {
@Override
public void run() {
try {
listener.onForceSwitchFail(status, e);
} catch (Exception e1) {
LOGGER.error("call listener.onForceSwitchFail() error ", e1);
}
}
});
}
});
}
});
} catch (Throwable t) {
forceSwitchedStatus = oldForceSwitchedStatus;
LOGGER.error("Force switch error", t);
throw new DalRuntimeException("Force switch error", t);
}
return oldStatus;
}
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterDynamicDataSource method prepare.
protected void prepare() {
cluster.addListener(event -> {
if (status.get() == ForceSwitchedStatus.UnForceSwitched) {
try {
switchDataSource();
} catch (Throwable t) {
String msg = "Cluster switch listener error";
LOGGER.error(msg, t);
throw new DalRuntimeException(msg, t);
}
} else
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, String.format("switchIgnored:%s", cluster.getClusterName()), "");
});
switchDataSource();
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class ClusterDynamicDataSource method restore.
@Override
public SwitchableDataSourceStatus restore() {
synchronized (lock) {
SwitchableDataSourceStatus currentStatus = getStatus();
try {
String logName = String.format(RESTORE, clusterInfo.toString());
LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, "restore", () -> {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before restore: %s, old poolCreated before restore: %s", currentStatus.isForceSwitched(), currentStatus.isPoolCreated()));
if (status.get() != ForceSwitchedStatus.ForceSwitched) {
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, "not in force switched status");
return;
}
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("previous host(s): %s:%s", currentStatus.getHostName(), currentStatus.getPort()));
HostAndPort newHost = buildHostAndPort(cluster);
LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new host(s): %s:%s", newHost.getHost(), newHost.getPort()));
getExecutor().submit(() -> {
try {
switchDataSource();
status.set(ForceSwitchedStatus.UnForceSwitched);
} catch (Throwable t) {
LOGGER.error("DataSource restoring failed", t);
}
});
});
return currentStatus;
} catch (Throwable t) {
LOGGER.error("Restore error", t);
throw new DalRuntimeException("Restore error", t);
}
}
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class SingleDeleteTask method execute.
@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
StatementParameters parameters = new StatementParameters();
addParameters(parameters, fields, parser.getPrimaryKeyNames());
String tableName = getRawTableName(hints, parameters, fields);
if (taskContext instanceof DalContextConfigure)
((DalContextConfigure) taskContext).addTables(tableName);
String deleteSql = buildDeleteSql(quote(tableName));
if (client instanceof DalContextClient)
return ((DalContextClient) client).update(deleteSql, parameters, hints.setFields(fields), taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
}
use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.
the class SingleUpdateTask method execute.
@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
if (fields.size() == 0)
throw new DalException(ErrorCode.ValidateFieldCount);
Map<String, ?> pks = getPrimaryKeys(fields);
Object version = getVersion(fields);
Map<String, ?> filted = null;
if (rawPojo instanceof UpdatableEntity)
filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
else
filted = filterNullColumns(hints, fields);
// If there is no columns changed, we will not perform update
if (filted.size() == 0)
return 0;
String tableName = getRawTableName(hints, fields);
if (taskContext instanceof DalContextConfigure)
((DalContextConfigure) taskContext).addTables(tableName);
String updateSql = buildUpdateSql(quote(tableName), filted, hints);
StatementParameters parameters = new StatementParameters();
addParameters(parameters, filted);
addParameters(parameters, pks);
addVersion(parameters, version);
if (client instanceof DalContextClient)
return ((DalContextClient) client).update(updateSql, parameters, hints.setFields(fields), taskContext);
else
throw new DalRuntimeException("The client is not instance of DalClient");
}
Aggregations