Search in sources :

Example 1 with DisconnectJob

use of org.jkiss.dbeaver.runtime.jobs.DisconnectJob in project dbeaver by serge-rider.

the class DataSourceHandler method disconnectDataSource.

public static void disconnectDataSource(DBPDataSourceContainer dataSourceContainer, @Nullable final Runnable onFinish) {
    // Save users
    for (DBPDataSourceUser user : dataSourceContainer.getUsers()) {
        if (user instanceof ISaveablePart) {
            if (!SaveChangesHandler.validateAndSave(VoidProgressMonitor.INSTANCE, (ISaveablePart) user)) {
                return;
            }
        }
    }
    if (!checkAndCloseActiveTransaction(dataSourceContainer)) {
        return;
    }
    if (dataSourceContainer instanceof DataSourceDescriptor && dataSourceContainer.isConnected()) {
        final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) dataSourceContainer;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(dataSourceDescriptor))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final DisconnectJob disconnectJob = new DisconnectJob(dataSourceDescriptor);
        disconnectJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                IStatus result = disconnectJob.getConnectStatus();
                if (onFinish != null) {
                    onFinish.run();
                } else if (!result.isOK()) {
                    UIUtils.showErrorDialog(null, disconnectJob.getName(), null, result);
                }
            }
        });
        // Run in UI thread to update actions (some Eclipse magic)
        DBeaverUI.asyncExec(new Runnable() {

            @Override
            public void run() {
                disconnectJob.schedule();
            }
        });
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) ISaveablePart(org.eclipse.ui.ISaveablePart) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 2 with DisconnectJob

use of org.jkiss.dbeaver.runtime.jobs.DisconnectJob in project dbeaver by serge-rider.

the class DataSourceInvalidateHandler method execute.

public static void execute(final Shell shell, final DBCExecutionContext context) {
    if (context != null) {
        //final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) context;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(context.getDataSource().getContainer()))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final InvalidateJob invalidateJob = new InvalidateJob(context);
        invalidateJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                StringBuilder message = new StringBuilder();
                Throwable error = null;
                int totalNum = 0, connectedNum = 0, aliveNum = 0;
                for (InvalidateJob.ContextInvalidateResult result : invalidateJob.getInvalidateResults()) {
                    totalNum++;
                    if (result.error != null) {
                        error = result.error;
                    }
                    switch(result.result) {
                        case CONNECTED:
                        case RECONNECTED:
                            connectedNum++;
                            break;
                        case ALIVE:
                            aliveNum++;
                            break;
                        default:
                            break;
                    }
                }
                if (connectedNum > 0) {
                    message.insert(0, "Connections reopened: " + connectedNum + " (of " + totalNum + ")");
                } else if (message.length() == 0) {
                    message.insert(0, "All connections (" + totalNum + ") are alive!");
                }
                if (error != null) {
                    //                        UIUtils.showErrorDialog(
                    //                            shell,
                    //                            "Invalidate data source [" + context.getDataSource().getContainer().getName() + "]",
                    //                            "Error while connecting to the datasource",// + "\nTime spent: " + RuntimeUtils.formatExecutionTime(invalidateJob.getTimeSpent()),
                    //                            error);
                    DBeaverUI.syncExec(new Runnable() {

                        @Override
                        public void run() {
                        }
                    });
                    final DBPDataSourceContainer container = context.getDataSource().getContainer();
                    final Throwable dialogError = error;
                    final Integer result = new UITask<Integer>() {

                        @Override
                        protected Integer runTask() {
                            ConnectionLostDialog clDialog = new ConnectionLostDialog(shell, container, dialogError, "Disconnect");
                            return clDialog.open();
                        }
                    }.execute();
                    if (result == IDialogConstants.STOP_ID) {
                        // Disconnect - to notify UI and reflect model changes
                        new DisconnectJob(container).schedule();
                    } else if (result == IDialogConstants.RETRY_ID) {
                        execute(shell, context);
                    }
                } else {
                    log.info(message);
                }
            }
        });
        invalidateJob.schedule();
    }
}
Also used : UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) InvalidateJob(org.jkiss.dbeaver.runtime.jobs.InvalidateJob) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 DisconnectJob (org.jkiss.dbeaver.runtime.jobs.DisconnectJob)2 IStatus (org.eclipse.core.runtime.IStatus)1 ISaveablePart (org.eclipse.ui.ISaveablePart)1 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)1 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)1 InvalidateJob (org.jkiss.dbeaver.runtime.jobs.InvalidateJob)1 UITask (org.jkiss.dbeaver.ui.UITask)1 ConnectionLostDialog (org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog)1