From ebfc4ee71f89b55393f902aab976076ec747dfdb Mon Sep 17 00:00:00 2001 From: dzmipt Date: Fri, 24 Nov 2023 20:11:36 +0100 Subject: [PATCH] Remove ConnectionPool --- src/studio/kdb/ConnectionPool.java | 54 ------------------------- src/studio/kdb/Session.java | 34 +++++++++++++++- src/studio/ui/EditorTab.java | 12 +++--- src/studio/ui/StudioWindow.java | 3 -- src/studio/ui/action/QueryExecutor.java | 19 ++------- 5 files changed, 43 insertions(+), 79 deletions(-) delete mode 100755 src/studio/kdb/ConnectionPool.java diff --git a/src/studio/kdb/ConnectionPool.java b/src/studio/kdb/ConnectionPool.java deleted file mode 100755 index 60c087e5..00000000 --- a/src/studio/kdb/ConnectionPool.java +++ /dev/null @@ -1,54 +0,0 @@ -package studio.kdb; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class ConnectionPool { - - private static final Logger log = LogManager.getLogger(); - - private final static ConnectionPool instance = new ConnectionPool(); - - private final Map> sessionMap = new HashMap<>(); - - public static ConnectionPool getInstance() { - return instance; - } - - private ConnectionPool() {} - - private static void cleanupSessions(List sessions) { - sessions.removeIf(Session::isClosed); - } - - public synchronized Session leaseConnection(Server server) { - List sessions = sessionMap.computeIfAbsent(server, k-> new ArrayList<>()); - cleanupSessions(sessions); - - for (Session session: sessions) { - if (! session.isBusy()) { - session.validate(); - session.setBusy(); - return session; - } - } - Session session = new Session(server); - - sessions.add(session); - return session; - } - - public synchronized void purge(Server server) { - List sessions = sessionMap.computeIfAbsent(server, k-> new ArrayList<>()); - for(Session session: sessions) { - session.close(); - } - sessions.clear(); - } - -} diff --git a/src/studio/kdb/Session.java b/src/studio/kdb/Session.java index 0ca3c0fe..c93567de 100644 --- a/src/studio/kdb/Session.java +++ b/src/studio/kdb/Session.java @@ -8,8 +8,13 @@ import studio.core.AuthenticationManager; import studio.core.Credentials; import studio.core.IAuthenticationMechanism; +import studio.ui.EditorTab; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class Session { private KConnection kConn; @@ -20,10 +25,37 @@ public class Session { private static final long HOUR = 1000*3600; private static SessionCreator sessionCreator = new SessionCreator(); + private final List editors = new ArrayList<>(); private static final Logger log = LogManager.getLogger(); - public Session(Server server) { + private final static Map sessions = new HashMap<>(); + + public static Session newSession(EditorTab editor) { + Server server = editor.getServer(); + Session session; + if (Config.getInstance().getBoolean(Config.SESSION_REUSE) ) { + session = sessions.get(server); + if (session == null) { + session = new Session(server); + sessions.put(server, session); + } + } else { + session = new Session(server); + } + + session.editors.add(editor); + return session; + } + + public void removeTab(EditorTab editor) { + editors.remove(editor); + if (editors.size() == 0) { + close(); + } + } + + private Session(Server server) { this.server = server; init(); } diff --git a/src/studio/ui/EditorTab.java b/src/studio/ui/EditorTab.java index c1888594..f99cae7d 100644 --- a/src/studio/ui/EditorTab.java +++ b/src/studio/ui/EditorTab.java @@ -56,14 +56,10 @@ public Session getSession() { return session; } - public void setSession(Session session) { - this.session = session; - } - public void closing() { stopFileWatching(); if (session != null) { - session.close(); + session.removeTab(this); } } @@ -249,7 +245,13 @@ public Server getServer() { public void setServer(Server server) { if (this.server == server) return; + + if (session != null) { + session.removeTab(this); + } this.server = server; + session = Session.newSession(this); + getTextArea().setBackground(server.getBackgroundColor()); studioWindow.getMainStatusBar().setTemporaryStatus("Changed server: " + server.getDescription(true)); diff --git a/src/studio/ui/StudioWindow.java b/src/studio/ui/StudioWindow.java index ac4deec5..52e69a3e 100755 --- a/src/studio/ui/StudioWindow.java +++ b/src/studio/ui/StudioWindow.java @@ -535,7 +535,6 @@ private void initActions() { if (stopAction.isEnabled()) stopAction.actionPerformed(e); - ConnectionPool.getInstance().purge(editor.getServer()); CONFIG.removeServer(editor.getServer()); Server newServer = f.getServer(); @@ -553,7 +552,6 @@ private void initActions() { if (f.getResult() == ACCEPTED) { Server s = f.getServer(); CONFIG.addServer(s); - ConnectionPool.getInstance().purge(s); //? setServer(s); rerfreshAll(); } @@ -906,7 +904,6 @@ private void refreshMenu() { clone = f.getServer(); CONFIG.addServer(clone); setServer(clone); - ConnectionPool.getInstance().purge(clone); //? rerfreshAll(); } }); diff --git a/src/studio/ui/action/QueryExecutor.java b/src/studio/ui/action/QueryExecutor.java index 6cbb43e2..881b323f 100644 --- a/src/studio/ui/action/QueryExecutor.java +++ b/src/studio/ui/action/QueryExecutor.java @@ -4,7 +4,9 @@ import kx.ProgressCallback; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import studio.kdb.*; +import studio.kdb.K; +import studio.kdb.Server; +import studio.kdb.Session; import studio.ui.EditorTab; import studio.ui.StudioWindow; @@ -116,22 +118,7 @@ void closeConnection() { } private Session getSession() { - if (Config.getInstance().getBoolean(Config.SESSION_REUSE)) { - return ConnectionPool.getInstance().leaseConnection(server); - } - Session session = editor.getSession(); - if (session == null) { - session = new Session(server); - editor.setSession(session); - } - - if (! session.getServer().equals(server) ) { - session.close(); - session = new Session(server); - editor.setSession(session); - } - session.validate(); return session; }