use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.
the class InvalidateJob method invalidateTransaction.
public static void invalidateTransaction(DBRProgressMonitor monitor, DBPDataSource dataSource, DBCExecutionContext executionContext) {
// Invalidate transactions
if (executionContext != null) {
monitor.subTask("Invalidate context [" + executionContext.getDataSource().getContainer().getName() + "/" + executionContext.getContextName() + "] transactions");
invalidateTransaction(monitor, executionContext);
} else {
monitor.subTask("Invalidate datasource [" + dataSource.getContainer().getName() + "] transactions");
for (DBSInstance instance : dataSource.getAvailableInstances()) {
for (DBCExecutionContext context : instance.getAllContexts()) {
invalidateTransaction(monitor, context);
}
}
}
}
use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.
the class InvalidateJob method invalidateDataSource.
public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource, boolean disconnectOnFailure, boolean showErrors, Runnable feedback) {
List<ContextInvalidateResult> invalidateResults = new ArrayList<>();
DBPDataSourceContainer container = dataSource.getContainer();
boolean networkOK;
int goodContextsNumber, aliveContextsNumber;
monitor.beginTask("Invalidate datasource '" + dataSource.getContainer().getName() + "'", 1);
monitor.subTask("Obtain exclusive datasource lock");
Object dsLock = container.getExclusiveLock().acquireTaskLock(TASK_INVALIDATE, true);
if (dsLock == DBPExclusiveResource.TASK_PROCESED) {
// Already invalidated
monitor.done();
log.debug("Datasource was already invalidated");
return invalidateResults;
}
try {
long timeSpent = 0;
monitor.subTask("Invalidate network connection");
DBWNetworkHandler[] activeHandlers = container.getActiveNetworkHandlers();
networkOK = true;
aliveContextsNumber = 0;
goodContextsNumber = 0;
if (activeHandlers != null && activeHandlers.length > 0) {
for (DBWNetworkHandler nh : activeHandlers) {
monitor.subTask("Invalidate handler [" + container.getClass().getSimpleName() + "]");
try {
nh.invalidateHandler(monitor, dataSource);
} catch (Exception e) {
invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
networkOK = false;
break;
}
}
}
// Invalidate datasource
int totalContexts = 0;
monitor.subTask("Invalidate connections of [" + container.getName() + "]");
for (DBSInstance instance : dataSource.getAvailableInstances()) {
for (DBCExecutionContext context : instance.getAllContexts()) {
totalContexts++;
if (networkOK) {
long startTime = System.currentTimeMillis();
Object exclusiveLock = instance.getExclusiveLock().acquireExclusiveLock();
try {
final DBCExecutionContext.InvalidateResult result = context.invalidateContext(monitor, disconnectOnFailure);
if (result != DBCExecutionContext.InvalidateResult.ERROR) {
goodContextsNumber++;
}
if (result == DBCExecutionContext.InvalidateResult.ALIVE) {
aliveContextsNumber++;
}
invalidateResults.add(new ContextInvalidateResult(result, null));
} catch (Exception e) {
invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
} finally {
timeSpent += (System.currentTimeMillis() - startTime);
instance.getExclusiveLock().releaseExclusiveLock(exclusiveLock);
}
}
}
}
if (goodContextsNumber > 0 && goodContextsNumber == aliveContextsNumber) {
// Nothing to reinit, all contexts are alive. Why we are here??
return invalidateResults;
}
if (goodContextsNumber == 0 && disconnectOnFailure) {
// Close whole datasource. Target host seems to be unavailable
try {
container.disconnect(monitor);
} catch (Exception e) {
log.error("Error closing inaccessible datasource", e);
}
StringBuilder msg = new StringBuilder();
for (ContextInvalidateResult result : invalidateResults) {
if (result.error != null) {
if (msg.length() > 0)
msg.append("\n");
msg.append(result.error.getMessage());
}
}
DBWorkbench.getPlatformUI().showError("Forced disconnect", "Datasource '" + container.getName() + "' was disconnected: destination database unreachable.\n" + msg);
}
if (totalContexts > 0) {
if (goodContextsNumber == 0) {
if (showErrors) {
DBeaverNotifications.showNotification(dataSource, DBeaverNotifications.NT_RECONNECT, "Datasource invalidate failed", DBPMessageType.ERROR, feedback);
}
} else {
DBeaverNotifications.showNotification(dataSource, DBeaverNotifications.NT_RECONNECT, "Datasource was invalidated\n\n" + "Live connection count: " + goodContextsNumber + "/" + totalContexts, DBPMessageType.INFORMATION);
}
}
} finally {
container.getExclusiveLock().releaseTaskLock(TASK_INVALIDATE, dsLock);
monitor.done();
}
return invalidateResults;
}
use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.
the class DashboardListViewer method openSeparateContext.
private void openSeparateContext() {
DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
return;
}
new AbstractJob("Open connection for dashboard") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
DBSInstance instance = DBUtils.getObjectOwnerInstance(dataSource);
if (instance != null) {
try {
isolatedContext = instance.openIsolatedContext(monitor, "Dashboard connection", null);
} catch (DBException e) {
return GeneralUtils.makeExceptionStatus(e);
}
}
return Status.OK_STATUS;
}
}.schedule();
}
use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.
the class SQLEditor method initSeparateConnection.
private void initSeparateConnection(@NotNull DBPDataSource dataSource, Runnable onSuccess) {
DBSInstance dsInstance = dataSource.getDefaultInstance();
String[] contextDefaults = isRestoreActiveSchemaFromScript() ? EditorUtils.getInputContextDefaults(dataSource.getContainer(), getEditorInput()) : null;
if (!ArrayUtils.isEmpty(contextDefaults) && contextDefaults[0] != null) {
DBSInstance selectedInstance = DBUtils.findObject(dataSource.getAvailableInstances(), contextDefaults[0]);
if (selectedInstance != null) {
dsInstance = selectedInstance;
}
}
if (dsInstance != null) {
final OpenContextJob job = new OpenContextJob(dsInstance, onSuccess);
job.schedule();
}
}
Aggregations