Search in sources :

Example 1 with DBWNetworkHandler

use of org.jkiss.dbeaver.model.net.DBWNetworkHandler in project dbeaver by serge-rider.

the class InvalidateJob method invalidateDataSource.

public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource) {
    long timeSpent = 0;
    List<ContextInvalidateResult> invalidateResults = new ArrayList<>();
    DBPDataSourceContainer container = dataSource.getContainer();
    DBWNetworkHandler[] activeHandlers = container.getActiveNetworkHandlers();
    boolean networkOK = true;
    boolean hasGoodContexts = false;
    if (activeHandlers != null && activeHandlers.length > 0) {
        for (DBWNetworkHandler nh : activeHandlers) {
            monitor.subTask("Invalidate network [" + container.getName() + "]");
            try {
                nh.invalidateHandler(monitor);
            } catch (Exception e) {
                invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
                networkOK = false;
                break;
            }
        }
    }
    if (networkOK) {
        // Invalidate datasource
        monitor.subTask("Invalidate connections of [" + container.getName() + "]");
        DBCExecutionContext[] allContexts = dataSource.getAllContexts();
        for (DBCExecutionContext context : allContexts) {
            long startTime = System.currentTimeMillis();
            try {
                final DBCExecutionContext.InvalidateResult result = context.invalidateContext(monitor);
                if (result != DBCExecutionContext.InvalidateResult.ERROR) {
                    hasGoodContexts = true;
                }
                invalidateResults.add(new ContextInvalidateResult(result, null));
            } catch (Exception e) {
                invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
            } finally {
                timeSpent += (System.currentTimeMillis() - startTime);
            }
        }
    }
    if (!hasGoodContexts) {
        // Close whole datasource. Target host seems to be unavailable
        try {
            container.disconnect(monitor);
        } catch (Exception e) {
            log.error("Error closing unaccessible 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());
            }
        }
        UIUtils.showErrorDialog(null, "Forced disconnect", "Datasource '" + container.getName() + "' was disconnected: destination database unreachable.\n" + msg);
    }
    return invalidateResults;
}
Also used : DBWNetworkHandler(org.jkiss.dbeaver.model.net.DBWNetworkHandler) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

ArrayList (java.util.ArrayList)1 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)1 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)1 DBWNetworkHandler (org.jkiss.dbeaver.model.net.DBWNetworkHandler)1