use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class PostgreDebugController method getSessionDescriptors.
@Override
public List<PostgreDebugSessionInfo> getSessionDescriptors() throws DBGException {
DBCExecutionContext executionContext = getExecutionContext();
try (Statement stmt = getConnection(executionContext).createStatement();
ResultSet rs = stmt.executeQuery(SQL_SESSION)) {
List<PostgreDebugSessionInfo> res = new ArrayList<PostgreDebugSessionInfo>();
while (rs.next()) {
int pid = rs.getInt("pid");
String usename = rs.getString("usename");
String state = rs.getString("state");
String applicationName = rs.getString("application_name");
String query = rs.getString("query");
PostgreDebugSessionInfo info = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query);
res.add(info);
}
return res;
} catch (SQLException e) {
throw new DBGException("SQL error", e);
}
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class InvalidateJob method invalidateDataSource.
public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource, boolean disconnectOnFailure) {
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, disconnectOnFailure);
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 && disconnectOnFailure) {
// 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());
}
}
DBUserInterface.getInstance().showError("Forced disconnect", "Datasource '" + container.getName() + "' was disconnected: destination database unreachable.\n" + msg);
}
return invalidateResults;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class GlobalProxySelector method select.
@Override
public List<Proxy> select(URI uri) {
String scheme = uri.getScheme();
if (CommonUtils.isEmpty(scheme)) {
return parent.select(uri);
}
if (scheme.startsWith("http")) {
// 1. Check for drivers download proxy
}
if (SocksConstants.SOCKET_SCHEME.equals(scheme)) {
// 2. Check for connections' proxy config
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(uri.getHost(), uri.getPort(), uri.getPath());
if (activeContext != null) {
List<Proxy> proxies = null;
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
Map<String, String> proxyProps = networkHandler.getProperties();
String proxyHost = proxyProps.get(SocksConstants.PROP_HOST);
String proxyPort = proxyProps.get(SocksConstants.PROP_PORT);
if (!CommonUtils.isEmpty(proxyHost)) {
int portNumber = SocksConstants.DEFAULT_SOCKS_PORT;
if (!CommonUtils.isEmpty(proxyPort)) {
try {
portNumber = Integer.parseInt(proxyPort);
} catch (NumberFormatException e) {
log.warn("Bad proxy port number", e);
}
}
InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, portNumber);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
if (proxies == null) {
proxies = new ArrayList<>();
}
proxies.add(proxy);
log.debug("Use SOCKS proxy [" + proxyAddr + "]");
}
}
}
if (proxies != null) {
return proxies;
}
}
}
return parent.select(uri);
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class BaseSQLDialog method createSQLPanel.
protected Composite createSQLPanel(Composite parent) {
Composite panel = UIUtils.createPlaceholder(parent, 1);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (isLabelVisible()) {
UIUtils.createControlLabel(panel, "SQL Preview");
}
// new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite editorPH = new Composite(panel, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 3;
gd.horizontalSpan = 1;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
editorPH.setLayoutData(gd);
editorPH.setLayout(new FillLayout());
sqlViewer = new SQLEditorBase() {
@NotNull
@Override
public SQLDialect getSQLDialect() {
return BaseSQLDialog.this.getSQLDialect();
}
@Override
public DBCExecutionContext getExecutionContext() {
return BaseSQLDialog.this.getExecutionContext();
}
};
updateSQL();
sqlViewer.createPartControl(editorPH);
if (isWordWrap()) {
Object text = sqlViewer.getAdapter(Control.class);
if (text instanceof StyledText) {
((StyledText) text).setWordWrap(true);
}
}
sqlViewer.reloadSyntaxRules();
parent.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sqlViewer.dispose();
}
});
return panel;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by dbeaver.
the class DBGBaseController method attach.
@Override
public Object attach(DBRProgressMonitor monitor) throws DBGException {
DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (!dataSourceContainer.isConnected()) {
throw new DBGException("Not connected to database");
}
try {
this.executionContext = dataSource.openIsolatedContext(monitor, "Debug controller");
DBGSessionInfo targetInfo = getSessionDescriptor(getExecutionContext());
DBCExecutionContext sessionContext = dataSource.openIsolatedContext(monitor, "Debug session");
DBGBaseSession debugSession = createSession(targetInfo, sessionContext);
Object id = targetInfo.getID();
sessions.put(id, debugSession);
attachSession(debugSession, sessionContext, configuration, monitor);
return id;
} catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context, dataSourceContainer);
log.error(message, e);
throw new DBGException(message, e);
}
}
Aggregations