Search in sources :

Example 1 with DBSInstance

use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by serge-rider.

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 2 with DBSInstance

use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.

the class PendingTransactionsDialog method loadContexts.

private void loadContexts(boolean showAllContexts) {
    contextTree.removeAll();
    // Load all open context
    for (DBPDataSourceContainer dataSource : DataSourceRegistry.getAllDataSources()) {
        if (!dataSource.isConnected() || dataSource.getDataSource() == null) {
            continue;
        }
        for (DBSInstance instance : dataSource.getDataSource().getAvailableInstances()) {
            DBCExecutionContext[] allContexts = instance.getAllContexts();
            if (ArrayUtils.isEmpty(allContexts)) {
                continue;
            }
            List<DBCExecutionContext> txnContexts = new ArrayList<>();
            for (DBCExecutionContext context : allContexts) {
                if (showAllContexts || QMUtils.isTransactionActive(context, false)) {
                    txnContexts.add(context);
                }
            }
            if (txnContexts.isEmpty()) {
                continue;
            }
            TreeItem dsItem = new TreeItem(contextTree, SWT.NONE);
            dsItem.setText(dataSource.getName());
            dsItem.setImage(DBeaverIcons.getImage(dataSource.getDriver().getIcon()));
            dsItem.setData(dataSource);
            for (DBCExecutionContext context : txnContexts) {
                QMTransactionState txnState = QMUtils.getTransactionState(context);
                TreeItem contextItem = new TreeItem(dsItem, SWT.NONE);
                contextItem.setText(0, context.getContextName());
                String stateString = String.valueOf(txnState.getUpdateCount()) + "/" + String.valueOf(txnState.getExecuteCount());
                contextItem.setText(1, stateString);
                contextItem.setData(context);
            }
            dsItem.setExpanded(true);
        }
    }
    UIUtils.asyncExec(new Runnable() {

        @Override
        public void run() {
            UIUtils.packColumns(contextTree);
        }
    });
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance) ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) QMTransactionState(org.jkiss.dbeaver.model.qm.QMTransactionState)

Example 3 with DBSInstance

use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by dbeaver.

the class FilterMapping method getFilterContainerUniqueID.

public static String getFilterContainerUniqueID(@Nullable DBSObject parentObject) {
    String objectFullName = DBUtils.getObjectFullName(parentObject, DBPEvaluationContext.UI);
    DBSInstance ownerInstance = DBUtils.getObjectOwnerInstance(parentObject);
    if (!CommonUtils.equalObjects(ownerInstance.getName(), parentObject.getDataSource().getName())) {
        return ownerInstance.getName() + ":" + objectFullName;
    } else {
        return objectFullName;
    }
}
Also used : DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance)

Example 4 with DBSInstance

use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by serge-rider.

the class PendingTransactionsDialog method loadContexts.

private void loadContexts(boolean showAllContexts) {
    contextTree.removeAll();
    // Load all open context
    for (DBPDataSourceContainer dataSource : DataSourceRegistry.getAllDataSources()) {
        if (!dataSource.isConnected() || dataSource.getDataSource() == null) {
            continue;
        }
        for (DBSInstance instance : dataSource.getDataSource().getAvailableInstances()) {
            DBCExecutionContext[] allContexts = instance.getAllContexts();
            if (ArrayUtils.isEmpty(allContexts)) {
                continue;
            }
            List<DBCExecutionContext> txnContexts = new ArrayList<>();
            for (DBCExecutionContext context : allContexts) {
                if (showAllContexts || QMUtils.isTransactionActive(context, false)) {
                    txnContexts.add(context);
                }
            }
            if (txnContexts.isEmpty()) {
                continue;
            }
            TreeItem dsItem = new TreeItem(contextTree, SWT.NONE);
            dsItem.setText(dataSource.getName());
            dsItem.setImage(DBeaverIcons.getImage(dataSource.getDriver().getIcon()));
            dsItem.setData(dataSource);
            for (DBCExecutionContext context : txnContexts) {
                QMTransactionState txnState = QMUtils.getTransactionState(context);
                TreeItem contextItem = new TreeItem(dsItem, SWT.NONE);
                contextItem.setText(0, context.getContextName());
                String stateString = String.valueOf(txnState.getUpdateCount()) + "/" + String.valueOf(txnState.getExecuteCount());
                contextItem.setText(1, stateString);
                contextItem.setData(context);
            }
            dsItem.setExpanded(true);
        }
    }
    UIUtils.asyncExec(new Runnable() {

        @Override
        public void run() {
            UIUtils.packColumns(contextTree);
        }
    });
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSInstance(org.jkiss.dbeaver.model.struct.DBSInstance) ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) QMTransactionState(org.jkiss.dbeaver.model.qm.QMTransactionState)

Example 5 with DBSInstance

use of org.jkiss.dbeaver.model.struct.DBSInstance in project dbeaver by serge-rider.

the class FilterMapping method getFilterContainerUniqueID.

public static String getFilterContainerUniqueID(@Nullable DBSObject parentObject) {
    String objectFullName = DBUtils.getObjectFullName(parentObject, DBPEvaluationContext.UI);
    DBSInstance ownerInstance = DBUtils.getObjectOwnerInstance(parentObject);
    if (!CommonUtils.equalObjects(ownerInstance.getName(), parentObject.getDataSource().getName())) {
        return ownerInstance.getName() + ":" + objectFullName;
    } else {
        return objectFullName;
    }
}
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