Search in sources :

Example 11 with DBSInstance

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);
            }
        }
    }
}
Also used : DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance)

Example 12 with DBSInstance

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;
}
Also used : DBWNetworkHandler(org.jkiss.dbeaver.model.net.DBWNetworkHandler) DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance) ArrayList(java.util.ArrayList)

Example 13 with DBSInstance

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();
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 14 with DBSInstance

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();
    }
}
Also used : DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance)

Aggregations

DBSInstance (org.jkiss.dbeaver.model.struct.DBSInstance)14 ArrayList (java.util.ArrayList)4 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)4 IFile (org.eclipse.core.resources.IFile)2 IStatus (org.eclipse.core.runtime.IStatus)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)2 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)2 NotNull (org.jkiss.code.NotNull)2 DBException (org.jkiss.dbeaver.DBException)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)2 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)2 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)2 DBWNetworkHandler (org.jkiss.dbeaver.model.net.DBWNetworkHandler)2 QMTransactionState (org.jkiss.dbeaver.model.qm.QMTransactionState)2 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)2