Search in sources :

Example 1 with DBPDataSourceTask

use of org.jkiss.dbeaver.model.DBPDataSourceTask in project dbeaver by dbeaver.

the class DataSourceHandler method disconnectDataSource.

public static void disconnectDataSource(DBPDataSourceContainer dataSourceContainer, @Nullable final Runnable onFinish) {
    // Save users
    for (DBPDataSourceTask user : dataSourceContainer.getTasks()) {
        if (user instanceof ISaveablePart) {
            if (!SaveChangesHandler.validateAndSave(new VoidProgressMonitor(), (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()) {
                    DBWorkbench.getPlatformUI().showError(disconnectJob.getName(), null, result);
                }
                DataSourcePropertyTester.firePropertyChange(DataSourcePropertyTester.PROP_CONNECTED);
            }
        });
        disconnectJob.schedule();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DBPDataSourceTask(org.jkiss.dbeaver.model.DBPDataSourceTask) ISaveablePart(org.eclipse.ui.ISaveablePart) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 2 with DBPDataSourceTask

use of org.jkiss.dbeaver.model.DBPDataSourceTask in project dbeaver by dbeaver.

the class JDBCUtils method isConnectionAlive.

public static boolean isConnectionAlive(DBPDataSource dataSource, Connection connection) {
    try {
        if (connection == null || connection.isClosed()) {
            return false;
        }
    } catch (SQLException e) {
        log.debug(e);
        return false;
    }
    // Check for active tasks. Do not run ping if there is active task
    for (DBPDataSourceTask task : dataSource.getContainer().getTasks()) {
        if (task.isActiveTask()) {
            return true;
        }
    }
    // Run ping query
    final String testSQL = dataSource.getSQLDialect().getTestSQL();
    int invalidateTimeout = dataSource.getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_VALIDATION_TIMEOUT);
    // Invalidate in non-blocking task.
    // Timeout is CONNECTION_VALIDATION_TIMEOUT + 2 seconds
    final boolean[] isValid = new boolean[1];
    RuntimeUtils.runTask(monitor -> {
        try {
            if (!CommonUtils.isEmpty(testSQL)) {
                // Execute test SQL
                try (Statement dbStat = connection.createStatement()) {
                    dbStat.execute(testSQL);
                    isValid[0] = true;
                }
            } else {
                try {
                    isValid[0] = connection.isValid(invalidateTimeout);
                } catch (Throwable e) {
                    // isValid may be unsupported by driver
                    // Let's try to read table list
                    connection.getMetaData().getTables(null, null, "DBEAVERFAKETABLENAMEFORPING", null);
                    isValid[0] = true;
                }
            }
        } catch (SQLException e) {
            isValid[0] = false;
        }
    }, "Ping connection " + dataSource.getContainer().getName(), invalidateTimeout + 2000, true);
    return isValid[0];
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBPDataSourceTask(org.jkiss.dbeaver.model.DBPDataSourceTask)

Example 3 with DBPDataSourceTask

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

the class DataSourceHandler method disconnectDataSource.

public static void disconnectDataSource(DBPDataSourceContainer dataSourceContainer, @Nullable final Runnable onFinish) {
    // Save users
    for (DBPDataSourceTask user : dataSourceContainer.getTasks()) {
        if (user instanceof ISaveablePart) {
            if (!SaveChangesHandler.validateAndSave(new VoidProgressMonitor(), (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()) {
                    DBWorkbench.getPlatformUI().showError(disconnectJob.getName(), null, result);
                }
                DataSourcePropertyTester.firePropertyChange(DataSourcePropertyTester.PROP_CONNECTED);
            }
        });
        disconnectJob.schedule();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DBPDataSourceTask(org.jkiss.dbeaver.model.DBPDataSourceTask) ISaveablePart(org.eclipse.ui.ISaveablePart) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 4 with DBPDataSourceTask

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

the class JDBCUtils method isConnectionAlive.

public static boolean isConnectionAlive(DBPDataSource dataSource, Connection connection) {
    try {
        if (connection == null || connection.isClosed()) {
            return false;
        }
    } catch (SQLException e) {
        log.debug(e);
        return false;
    }
    // Check for active tasks. Do not run ping if there is active task
    for (DBPDataSourceTask task : dataSource.getContainer().getTasks()) {
        if (task.isActiveTask()) {
            return true;
        }
    }
    // Run ping query
    final String testSQL = dataSource.getSQLDialect().getTestSQL();
    int invalidateTimeout = dataSource.getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_VALIDATION_TIMEOUT);
    // Invalidate in non-blocking task.
    // Timeout is CONNECTION_VALIDATION_TIMEOUT + 2 seconds
    final boolean[] isValid = new boolean[1];
    RuntimeUtils.runTask(monitor -> {
        try {
            if (!CommonUtils.isEmpty(testSQL)) {
                // Execute test SQL
                try (Statement dbStat = connection.createStatement()) {
                    dbStat.execute(testSQL);
                    isValid[0] = true;
                }
            } else {
                try {
                    isValid[0] = connection.isValid(invalidateTimeout);
                } catch (Throwable e) {
                    // isValid may be unsupported by driver
                    // Let's try to read table list
                    connection.getMetaData().getTables(null, null, "DBEAVERFAKETABLENAMEFORPING", null);
                    isValid[0] = true;
                }
            }
        } catch (SQLException e) {
            isValid[0] = false;
        }
    }, "Ping connection " + dataSource.getContainer().getName(), invalidateTimeout + 2000, true);
    return isValid[0];
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBPDataSourceTask(org.jkiss.dbeaver.model.DBPDataSourceTask)

Aggregations

DBPDataSourceTask (org.jkiss.dbeaver.model.DBPDataSourceTask)4 IStatus (org.eclipse.core.runtime.IStatus)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 ISaveablePart (org.eclipse.ui.ISaveablePart)2 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)2 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)2 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)2 DisconnectJob (org.jkiss.dbeaver.runtime.jobs.DisconnectJob)2