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();
}
}
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];
}
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();
}
}
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];
}
Aggregations