提交 d09b9e38 编写于 作者: A Alexander Fedorov

#2556 bind session create/close

Former-commit-id: 09a5a31c
上级 2faade9c
......@@ -30,7 +30,7 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
public abstract class DBGBaseController <SESSION_ID_TYPE, OBJECT_ID_TYPE> implements DBGController {
public abstract class DBGBaseController<SID_TYPE, OID_TYPE> implements DBGController {
private static final Log log = Log.getLog(DBGBaseController.class);
......@@ -38,8 +38,6 @@ public abstract class DBGBaseController <SESSION_ID_TYPE, OBJECT_ID_TYPE> implem
private DBCExecutionContext debugContext;
private DBCSession debugSession;
private DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> dbgSessionManager;
private DBGSession<DBGSessionInfo<SESSION_ID_TYPE>, DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE> dbgSession;
public DBGBaseController() {
}
......@@ -49,42 +47,42 @@ public abstract class DBGBaseController <SESSION_ID_TYPE, OBJECT_ID_TYPE> implem
this.dataSourceDescriptor = dataSourceDescriptor;
}
protected abstract DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> initSessionManager(DBCSession session) throws DBGException;
@Override
public void connect(DBRProgressMonitor monitor) throws DBGException {
DBPDataSource dataSource = dataSourceDescriptor.getDataSource();
if (!dataSourceDescriptor.isConnected()) {
try {
//FIXME: AF: the contract of this call is not clear, we need some utility for this
// FIXME: AF: the contract of this call is not clear, we need
// some utility for this
dataSourceDescriptor.connect(monitor, true, true);
} catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_connecting_datasource, dataSourceDescriptor);
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_connecting_datasource,
dataSourceDescriptor);
log.error(message, e);
throw new DBGException(message, e);
}
}
try {
this.debugContext = dataSource.openIsolatedContext(monitor, DebugMessages.DatabaseDebugController_debug_context_purpose);
this.debugSession = debugContext.openSession(monitor, DBCExecutionPurpose.UTIL, DebugMessages.DatabaseDebugController_debug_session_name);
this.debugContext = dataSource.openIsolatedContext(monitor,
DebugMessages.DatabaseDebugController_debug_context_purpose);
this.debugSession = debugContext.openSession(monitor, DBCExecutionPurpose.UTIL,
DebugMessages.DatabaseDebugController_debug_session_name);
afterSessionOpen(debugSession);
} catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context, dataSourceDescriptor);
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context,
dataSourceDescriptor);
log.error(message, e);
throw new DBGException(message, e);
}
}
protected void afterSessionOpen(DBCSession session) throws DBGException {
this.dbgSessionManager = initSessionManager(session);
//do nothing by default
}
protected void beforeSessionClose(DBCSession session) throws DBGException {
if (this.dbgSessionManager != null) {
this.dbgSessionManager.dispose();;
}
this.dbgSessionManager = null;
//do nothing by default
}
@Override
......@@ -116,7 +114,7 @@ public abstract class DBGBaseController <SESSION_ID_TYPE, OBJECT_ID_TYPE> implem
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
package org.jkiss.dbeaver.debug;
public abstract class DBGProcedureController<SESSION_ID_TYPE, OBJECT_ID_TYPE> extends DBGBaseController<SESSION_ID_TYPE, OBJECT_ID_TYPE> {
import org.jkiss.dbeaver.model.exec.DBCSession;
private SESSION_ID_TYPE sessionId;
public abstract class DBGProcedureController<SID_TYPE, OID_TYPE> extends DBGBaseController<SID_TYPE, OID_TYPE> {
private SID_TYPE sessionId;
private DBGSessionManager<SID_TYPE, OID_TYPE> dbgSessionManager;
private DBGSession<? extends DBGSessionInfo<SID_TYPE>, ? extends DBGObject<OID_TYPE>, SID_TYPE, OID_TYPE> dbgSession;
public DBGProcedureController() {
super();
}
public SESSION_ID_TYPE getSessionId() {
@Override
protected void afterSessionOpen(DBCSession session) throws DBGException {
super.afterSessionOpen(session);
this.dbgSessionManager = initSessionManager(session);
if (this.dbgSessionManager == null) {
throw new DBGException("Failed to initialize Debug Session Manager");
}
this.dbgSession = createSession(session, dbgSessionManager);
if (this.dbgSession == null) {
throw new DBGException("Failed to initialize Debug Session");
}
setSessionId(dbgSession.getSessionId());
}
protected abstract DBGSessionManager<SID_TYPE, OID_TYPE> initSessionManager(DBCSession session) throws DBGException;
protected abstract DBGSession<? extends DBGSessionInfo<SID_TYPE>, ? extends DBGObject<OID_TYPE>, SID_TYPE, OID_TYPE> createSession(
DBCSession session, DBGSessionManager<SID_TYPE, OID_TYPE> sessionManager) throws DBGException;
@Override
protected void beforeSessionClose(DBCSession session) throws DBGException {
if (this.dbgSessionManager != null) {
this.dbgSessionManager.terminateSession(getSessionId());
this.dbgSessionManager.dispose();
}
this.dbgSessionManager = null;
super.beforeSessionClose(session);
}
public SID_TYPE getSessionId() {
return sessionId;
}
public void setSessionId(SESSION_ID_TYPE sessionId) {
public void setSessionId(SID_TYPE sessionId) {
this.sessionId = sessionId;
}
......
......@@ -20,7 +20,7 @@ package org.jkiss.dbeaver.debug;
import java.util.List;
public interface DBGSession<SESSION_INFO extends DBGSessionInfo<SESSION_ID_TYPE>, DEBUG_OBJECT extends DBGObject<?>, SESSION_ID_TYPE> {
public interface DBGSession<SESSION_INFO extends DBGSessionInfo<SESSION_ID_TYPE>, DEBUG_OBJECT extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE, OBJECT_ID_TYPE> {
SESSION_INFO getSessionInfo();
String getTitle();
......
......@@ -26,13 +26,13 @@ public interface DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> {
List<? extends DBGSessionInfo<SESSION_ID_TYPE>> getSessions() throws DBGException;
DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE> getDebugSession(SESSION_ID_TYPE id) throws DBGException;
DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE, OBJECT_ID_TYPE> getDebugSession(SESSION_ID_TYPE id) throws DBGException;
List<DBGSession<?, ?, SESSION_ID_TYPE>> getDebugSessions() throws DBGException;
List<DBGSession<?, ?, SESSION_ID_TYPE, OBJECT_ID_TYPE>> getDebugSessions() throws DBGException;
void terminateSession(SESSION_ID_TYPE id);
DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE> createDebugSession(Connection connection) throws DBGException;
DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE, OBJECT_ID_TYPE> createDebugSession(Connection connection) throws DBGException;
boolean isSessionExists(SESSION_ID_TYPE id);
......
......@@ -27,17 +27,17 @@ import org.jkiss.dbeaver.debug.core.model.DatabaseProcess;
import org.jkiss.dbeaver.debug.core.model.ProcedureDebugTarget;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedureController> {
public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedureController<?,?>> {
@Override
protected DBGProcedureController createController(DataSourceDescriptor datasourceDescriptor, String databaseName,
protected DBGProcedureController<?,?> createController(DataSourceDescriptor datasourceDescriptor, String databaseName,
Map<String, Object> attributes) throws CoreException {
String providerId = DebugCore.extractProviderId(datasourceDescriptor);
if (providerId == null) {
String message = NLS.bind("Unable to setup procedure debug for {0}", datasourceDescriptor.getName());
throw new CoreException(DebugCore.newErrorStatus(message));
}
DBGProcedureController procedureController = DebugCore.findProcedureController(datasourceDescriptor);
DBGProcedureController<?,?> procedureController = DebugCore.findProcedureController(datasourceDescriptor);
if (procedureController == null) {
String message = NLS.bind("Procedure debug is not supported for {0}", datasourceDescriptor.getName());
throw new CoreException(DebugCore.newErrorStatus(message));
......@@ -52,7 +52,7 @@ public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedure
}
@Override
protected ProcedureDebugTarget createDebugTarget(ILaunch launch, DBGProcedureController controller,
protected ProcedureDebugTarget createDebugTarget(ILaunch launch, DBGProcedureController<?,?> controller,
DatabaseProcess process) {
return new ProcedureDebugTarget(launch, process, controller);
}
......
......@@ -25,14 +25,14 @@ import org.jkiss.dbeaver.debug.DBGProcedureController;
import org.jkiss.dbeaver.debug.core.DebugCore;
import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages;
public class ProcedureDebugTarget extends DatabaseDebugTarget<DBGProcedureController> {
public class ProcedureDebugTarget extends DatabaseDebugTarget<DBGProcedureController<?,?>> {
public ProcedureDebugTarget(ILaunch launch, IProcess process, DBGProcedureController controller) {
public ProcedureDebugTarget(ILaunch launch, IProcess process, DBGProcedureController<?,?> controller) {
super(DebugCore.MODEL_IDENTIFIER_PROCEDURE, launch, process, controller);
}
@Override
protected DatabaseThread newThread(DBGProcedureController controller) {
protected DatabaseThread newThread(DBGProcedureController<?,?> controller) {
return new ProcedureThread(this, controller);
}
......
......@@ -20,95 +20,53 @@ package org.jkiss.dbeaver.ext.postgresql.debug.internal;
import java.sql.Connection;
import java.sql.SQLException;
import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGObject;
import org.jkiss.dbeaver.debug.DBGProcedureController;
import org.jkiss.dbeaver.debug.DBGSession;
import org.jkiss.dbeaver.debug.DBGSessionInfo;
import org.jkiss.dbeaver.debug.DBGSessionManager;
import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugSessionManager;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCResultSet;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.DBCStatementType;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
//FIXME: AF: adapter type for PostgreDebugSessionManager/PostgreDebugSession
//FIXME: AF: rework up to DBGBaseController if needed
public class PostgreProcedureController extends DBGProcedureController<Integer, Integer> {
private static final String PROPERTY_APPLICATION_NAME = "ApplicationName"; //$NON-NLS-1$
private static final String SELECT_FROM_PLDBG_ABORT_TARGET = "select * from pldbg_abort_target(?)"; //$NON-NLS-1$
private static final String SELECT_PLDBG_CREATE_LISTENER = "select pldbg_create_listener() as sessionid"; //$NON-NLS-1$
private static final Log log = Log.getLog(PostgreProcedureController.class);
public PostgreProcedureController()
{
public PostgreProcedureController() {
super();
}
@Override
protected void afterSessionOpen(DBCSession session) throws DBGException {
super.afterSessionOpen(session);
if (session instanceof JDBCSession ) {
JDBCSession jdbcSession = (JDBCSession) session;
String query = SELECT_PLDBG_CREATE_LISTENER;
try (final JDBCStatement jdbcStat = jdbcSession.prepareStatement(DBCStatementType.QUERY, query, false, false, false)) {
if (jdbcStat.executeStatement()) {
try (final DBCResultSet dbResult = jdbcStat.openResultSet()) {
while (dbResult.nextRow()) {
final Object cellValue = dbResult.getAttributeValue(0);
if (cellValue instanceof Integer) {
Integer cellValueInteger = (Integer) cellValue;
setSessionId(cellValueInteger);
String applicationName = NLS.bind(PostgreDebugCoreMessages.PgSqlDebugController_connection_application_name, getSessionId());
try {
jdbcSession.getOriginal().setClientInfo(PROPERTY_APPLICATION_NAME, applicationName);
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
}
}
}
}
} catch (DBCException e) {
String message = NLS.bind(PostgreDebugCoreMessages.PgSqlDebugController_e_failed_session_open, session.getDataSource());
log.error(message, e);
}
}
}
@Override
protected void beforeSessionClose(DBCSession session) throws DBGException {
protected DBGSessionManager<Integer, Integer> initSessionManager(DBCSession session) throws DBGException {
if (session instanceof JDBCSession) {
JDBCSession jdbcSession = (JDBCSession) session;
String query = SELECT_FROM_PLDBG_ABORT_TARGET;
try (final JDBCPreparedStatement prepared = jdbcSession.prepareStatement(query)) {
prepared.setInt(1, getSessionId());
prepared.execute();
Connection original;
try {
original = jdbcSession.getOriginal();
return new PostgreDebugSessionManager(original);
} catch (SQLException e) {
String message = NLS.bind(PostgreDebugCoreMessages.PgSqlDebugController_e_failed_session_close, getSessionId(), session.getDataSource());
log.error(message, e);
throw new DBGException("Unable to obtain connection");
}
}
super.beforeSessionClose(session);
throw new DBGException("Invalid JDBC session handle");
}
@Override
protected DBGSessionManager<Integer, Integer> initSessionManager(DBCSession session) throws DBGException {
protected DBGSession<? extends DBGSessionInfo<Integer>, ? extends DBGObject<Integer>, Integer, Integer> createSession(
DBCSession session, DBGSessionManager<Integer, Integer> sessionManager) throws DBGException {
if (session instanceof JDBCSession) {
JDBCSession jdbcSession = (JDBCSession) session;
Connection original;
try {
original = jdbcSession.getOriginal();
return new PostgreDebugSessionManager(original);
DBGSession<? extends DBGSessionInfo<Integer>, ? extends DBGObject<Integer>, Integer, Integer> created = sessionManager.createDebugSession(original);
return created;
} catch (SQLException e) {
throw new DBGException("Unable to to obtain connection");
throw new DBGException("Unable to obtain connection");
}
}
throw new DBGException("Invalid session handle");
throw new DBGException("Invalid JDBC session handle");
}
}
......@@ -133,7 +133,7 @@ public class Debugger {
PostgreDebugSession debugSession = null;
List<DBGSession<?, ?, Integer>> sessions = pgDbgManager.getDebugSessions();
List<DBGSession<?, ?, Integer, Integer>> sessions = pgDbgManager.getDebugSessions();
Scanner scArg;
......@@ -147,7 +147,7 @@ public class Debugger {
int sessNo = 1;
for (DBGSession<?, ?, Integer> s : sessions) {
for (DBGSession<?, ?, Integer, Integer> s : sessions) {
System.out.println(String.format(" (%d) %s", sessNo++, s.toString()));
}
......@@ -545,7 +545,7 @@ public class Debugger {
System.out.println("no debug sessions");
break;
}
for (DBGSession<?, ?, Integer> s : pgDbgManager.getDebugSessions()) {
for (DBGSession<?, ?, Integer, Integer> s : pgDbgManager.getDebugSessions()) {
System.out.println(s);
}
......
......@@ -36,7 +36,7 @@ import org.jkiss.dbeaver.debug.DBGStackFrame;
import org.jkiss.dbeaver.debug.DBGVariable;
@SuppressWarnings("nls")
public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer> {
public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> {
private final PostgreDebugSessionInfo sessionManagerInfo;
......
......@@ -124,7 +124,7 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In
}
@Override
public DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer> getDebugSession(Integer id)
public DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> getDebugSession(Integer id)
throws DBGException {
return sessions.get(id);
}
......@@ -164,8 +164,8 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In
}
@Override
public List<DBGSession<?, ?, Integer>> getDebugSessions() throws DBGException {
return new ArrayList<DBGSession<?, ?, Integer>>(sessions.values());
public List<DBGSession<?, ?, Integer, Integer>> getDebugSessions() throws DBGException {
return new ArrayList<DBGSession<?, ?, Integer, Integer>>(sessions.values());
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册