提交 491f515e 编写于 作者: S Serge Rider

#5172 PG isolated context reinit. QM model update (context reinitialization)


Former-commit-id: 23e1f708
上级 134f5eac
......@@ -84,11 +84,21 @@ public class PostgreExecutionContext extends JDBCExecutionContext implements DBC
return true;
}
// FIXME: Now it changes default database for entire datasource. Perhaps we need to change it only for current editor?
@Override
public void setDefaultCatalog(DBRProgressMonitor monitor, PostgreDatabase catalog, PostgreSchema schema) throws DBCException {
PostgreDataSource dataSource = getDefaultCatalog().getDataSource();
PostgreDatabase defaultInstance = dataSource.getDefaultInstance();
try {
getDefaultCatalog().getDataSource().setDefaultInstance(monitor, catalog, schema);
if (defaultInstance.getDefaultContext() == this) {
dataSource.setDefaultInstance(monitor, catalog, schema);
} else {
disconnect();
setOwnerInstance(catalog);
connect(monitor, null, null, false, false);
if (schema != null) {
setDefaultSchema(monitor, schema);
}
}
} catch (DBException e) {
throw new DBCException("Error changing default database", e);
}
......
......@@ -67,6 +67,10 @@ public class JDBCExecutionContext extends AbstractExecutionContext<JDBCDataSourc
return instance;
}
protected void setOwnerInstance(@NotNull JDBCRemoteInstance instance) {
this.instance = instance;
}
@NotNull
private Connection getConnection() {
return connection;
......@@ -76,7 +80,7 @@ public class JDBCExecutionContext extends AbstractExecutionContext<JDBCDataSourc
connect(monitor, null, null, false, true);
}
void connect(@NotNull DBRProgressMonitor monitor, Boolean autoCommit, @Nullable Integer txnLevel, boolean forceActiveObject, boolean addContext) throws DBCException {
protected void connect(@NotNull DBRProgressMonitor monitor, Boolean autoCommit, @Nullable Integer txnLevel, boolean forceActiveObject, boolean addContext) throws DBCException {
if (connection != null && addContext) {
log.error("Reopening not-closed connection");
close();
......@@ -157,6 +161,19 @@ public class JDBCExecutionContext extends AbstractExecutionContext<JDBCDataSourc
}
}
protected void disconnect() {
// [JDBC] Need sync here because real connection close could take some time
// while UI may invoke callbacks to operate with connection
synchronized (this) {
if (this.connection != null) {
this.dataSource.closeConnection(connection, purpose);
}
this.connection = null;
}
// Notify QM
super.closeContext();
}
@NotNull
public Connection getConnection(DBRProgressMonitor monitor) throws SQLException {
if (connection == null) {
......@@ -223,15 +240,7 @@ public class JDBCExecutionContext extends AbstractExecutionContext<JDBCDataSourc
}
private void closeContext(boolean removeContext) {
// [JDBC] Need sync here because real connection close could take some time
// while UI may invoke callbacks to operate with connection
synchronized (this) {
if (this.connection != null) {
this.dataSource.closeConnection(connection, purpose);
}
this.connection = null;
super.closeContext();
}
disconnect();
if (removeContext) {
// Remove self from context list
......
......@@ -29,12 +29,12 @@ import org.jkiss.utils.CommonUtils;
public class QMMSessionInfo extends QMMObject {
private final String containerId;
private final String containerName;
private final String driverId;
private String containerName;
@Nullable
private final DBPConnectionConfiguration connectionConfiguration;
private final String instanceId;
private final String contextName;
private DBPConnectionConfiguration connectionConfiguration;
private String instanceId;
private String contextName;
@Nullable
private SQLDialect sqlDialect;
private boolean transactional;
......@@ -47,8 +47,13 @@ public class QMMSessionInfo extends QMMObject {
public QMMSessionInfo(DBCExecutionContext context, boolean transactional)
{
this.containerId = context.getDataSource().getContainer().getId();
this.containerName = context.getDataSource().getContainer().getName();
this.driverId = context.getDataSource().getContainer().getDriver().getId();
initFromContext(context, transactional);
}
private void initFromContext(DBCExecutionContext context, boolean transactional) {
this.containerName = context.getDataSource().getContainer().getName();
this.connectionConfiguration = context.getDataSource().getContainer().getConnectionConfiguration();
this.instanceId = context.getOwnerInstance().getName();
this.contextName = context.getContextName();
......@@ -89,8 +94,8 @@ public class QMMSessionInfo extends QMMObject {
super.close();
}
public void reopen()
{
public void reopen(DBCExecutionContext context) {
initFromContext(context, transactional);
super.reopen();
}
......
......@@ -173,7 +173,7 @@ public class QMMCollectorImpl extends DefaultExecutionHandler implements QMMColl
} else {
// This session may already be in cache in case of reconnect/invalidate
// (when context closed and reopened without new context object creation)
session.reopen();
session.reopen(context);
}
// Remove from closed sessions (in case of re-opened connection)
......
......@@ -116,8 +116,8 @@ import org.jkiss.utils.CommonUtils;
import java.io.*;
import java.net.URI;
import java.util.List;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -370,11 +370,7 @@ public class SQLEditor extends SQLEditorBase implements
final DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
releaseExecutionContext();
} else if (curDataSource != dataSource ||
(curDataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.EDITOR_SEPARATE_CONNECTION) &&
executionContext != null &&
curDataSource.getDefaultInstance() != executionContext.getOwnerInstance()))
{
} else if (curDataSource != dataSource) {
// Datasource was changed or instance was changed (PG)
releaseExecutionContext();
curDataSource = dataSource;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册