From 5d0c6c1305dfc738b6bd031f54957f0692fb2283 Mon Sep 17 00:00:00 2001 From: "Destiny.Lin" Date: Mon, 21 Oct 2024 16:07:12 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-137542=20-=20=E8=BF=9C=E7=A8=8B=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E4=B8=8B=E9=95=BF=E6=97=B6=E9=97=B4=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=90=8E=E5=88=87=E6=8D=A2=E6=A8=A1=E6=9D=BF=E5=8D=A1=E6=85=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/server/GlobalTableDataAction.java | 5 +- .../data/datapane/TreeTableDataComboBox.java | 3 +- .../connect/ConnectionComboBoxPanel.java | 6 +- .../datapane/connect/ConnectionListPane.java | 4 +- .../preview/ConnectionInfoBeanHelper.java | 73 ++++++++++++++----- .../datapane/preview/TableDataBeanHelper.java | 5 +- .../tabledata/wrapper/TableDataFactory.java | 5 +- 7 files changed, 64 insertions(+), 37 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java b/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java index 138ab32295..b9f8ba5949 100644 --- a/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java +++ b/designer-base/src/main/java/com/fr/design/actions/server/GlobalTableDataAction.java @@ -24,11 +24,8 @@ import com.fr.design.menu.MenuKeySet; import com.fr.esd.event.DSMapping; import com.fr.esd.event.DsNameTarget; import com.fr.esd.event.StrategyEventsNotifier; - import com.fr.log.FineLoggerFactory; import com.fr.report.LockItem; -import com.fr.security.encryption.transmission.TransmissionEncryptionManager; -import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.TableDataRepository; import javax.swing.KeyStroke; @@ -85,7 +82,7 @@ public class GlobalTableDataAction extends UpdateAction implements ResponseDataS // 锁定成功,执行后续操作 final DesignerFrame designerFrame = DesignerContext.getDesignerFrame(); final List beans = TableDataRepository.getInstance().getAllTableData(); - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); final Map tableDataMap = new HashMap<>(); for (ServerDataSetBean bean : beans) { try { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java b/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java index 8954d12593..c80f756af3 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataComboBox.java @@ -14,7 +14,6 @@ import com.fr.design.data.tabledata.wrapper.TemplateTableDataWrapper; import com.fr.design.gui.icombobox.UIComboBox; import com.fr.design.gui.icombobox.UIComboBoxRenderer; import com.fr.stable.StringUtils; -import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.TableDataRepository; import javax.swing.*; @@ -70,7 +69,7 @@ public class TreeTableDataComboBox extends UIComboBox { // 全局数据集 List beans = TableDataRepository.getInstance().getAllTableData(); Map map = new HashMap<>(); - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); try { for (ServerDataSetBean bean : beans) { map.put(bean.getDatasetName() , TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java index 2376872a0f..9d4774b7de 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionComboBoxPanel.java @@ -1,8 +1,6 @@ package com.fr.design.data.datapane.connect; -import com.fanruan.config.impl.data.ConnectionConfigProviderFactory; import com.fine.theme.icon.LazyIcon; -import com.fr.base.svg.IconUtils; import com.fr.data.impl.AbstractDatabaseConnection; import com.fr.data.impl.Connection; import com.fr.data.impl.NameDatabaseConnection; @@ -15,9 +13,7 @@ import com.fr.design.gui.ibutton.UILockButton; import com.fr.report.LockItem; import com.fr.stable.StringUtils; import com.fr.workspace.WorkContext; -import com.fr.workspace.server.entity.connection.ConnectionBean; import com.fr.workspace.server.repository.authority.RemoteAuthorityRepository; -import com.fr.workspace.server.repository.connection.ConnectionRepository; import javax.swing.SwingUtilities; import java.awt.Dimension; @@ -91,7 +87,7 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { if (noAuthConnections == null) { return nameList.iterator(); } - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); for (Map.Entry connectionEntry : connectionMap.entrySet()) { String conName = connectionEntry.getKey(); if (noAuthConnections.contains(conName)) { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java index d1acf83474..93780213d5 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java @@ -207,7 +207,7 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh public void populate(Map connectionMap) { List nameObjectList = new ArrayList(); populatedConnectionsSnapshot.clear(); - final Map map = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map map = ConnectionInfoBeanHelper.getCurrentConnectionMap(); for (Map.Entry entry : map.entrySet()) { nameObjectList.add(new NameObject(entry.getKey(), entry.getValue())); try { @@ -369,7 +369,7 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh public static void showDialog(Window parent) { try { - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); final ConnectionManagerPane connectionManagerPane = new ConnectionManagerPane() { @Override public void complete() { diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java b/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java index 0aa05737ed..b1adba8b09 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/preview/ConnectionInfoBeanHelper.java @@ -22,20 +22,30 @@ import com.fr.decision.webservice.bean.datasource.JDBCConnectionBean; import com.fr.decision.webservice.utils.DecisionServiceConstants; import com.fr.decision.webservice.v10.datasource.connection.processor.impl.ConnectionProcessorFactory; import com.fr.decision.webservice.v10.datasource.connection.processor.impl.JDBCConnectionProcessor; +import com.fr.event.Event; +import com.fr.event.EventDispatcher; +import com.fr.event.Listener; +import com.fr.event.Null; import com.fr.log.FineLoggerFactory; import com.fr.module.tool.ActivatorToolBox; import com.fr.security.encryption.transmission.TransmissionEncryptionManager; import com.fr.stable.Constants; import com.fr.stable.StringUtils; import com.fr.third.fasterxml.jackson.databind.ObjectMapper; +import com.fr.third.guava.cache.CacheBuilder; +import com.fr.third.guava.cache.CacheLoader; +import com.fr.third.guava.cache.LoadingCache; import com.fr.third.springframework.beans.BeanUtils; import com.fr.workspace.server.repository.WorkplaceConstants; +import com.fr.workspace.server.repository.connection.ConnectionCacheEvent; import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.DataEncryptionHelper; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; /** * 数据连接传输工具类 @@ -50,19 +60,38 @@ public class ConnectionInfoBeanHelper { private static final int ORACLE_DEFAULT_FETCH_SIZE = 128; private static final int DB2_DEFAULT_FETCH_SIZE = 50; private static final int POSTGRE_DEFAULT_FETCH_SIZE = 10000; + public static final int CACHE_MAX_SIZE = 1; + public static final String VALUE = "value"; + private static LoadingCache> cache = CacheBuilder.newBuilder() + // 缓存容量1 + .maximumSize(CACHE_MAX_SIZE) + // 默认写入过60S后过期 + .expireAfterWrite(60, TimeUnit.SECONDS) + .build(new CacheLoader>() { + @Override + public Map load(String s) throws Exception { + return updateCache(); + } + }); + static { FETCH_SIZE_MAP.put("oracle", ORACLE_DEFAULT_FETCH_SIZE); FETCH_SIZE_MAP.put("ibm-db2", DB2_DEFAULT_FETCH_SIZE); FETCH_SIZE_MAP.put("postgresql", POSTGRE_DEFAULT_FETCH_SIZE); + // 监听数据连接缓存失效 + EventDispatcher.listen(ConnectionCacheEvent.REMOVE, new Listener() { + @Override + public void on(Event event, Null aNull) { + cache.invalidateAll(); + } + }); } - /** - * 获取连接Map - */ - public static Map createConnectionMap(ConnectionInfoBean[] beans) { + private static Map updateCache() { Map ans = new HashMap<>(); + ConnectionInfoBean[] beans = ConnectionRepository.getInstance().getAll(); for (ConnectionInfoBean infoBean : beans) { try { if (JDBCConnectionProcessor.KEY.acceptConnectionTypes().contains(infoBean.getConnectionType())) { @@ -77,23 +106,33 @@ public class ConnectionInfoBeanHelper { return ans; } + + + + /** + * 获取连接Map + */ + public static Map getCurrentConnectionMap() { + try { + return cache.get(VALUE); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + } + /** * 根据bean创建连接 */ public static Connection createConnection(ConnectionInfoBean infoBean) { - Connection connection = null; - try { - if (JDBCConnectionProcessor.KEY.acceptConnectionTypes().contains(infoBean.getConnectionType())) { - connection = createJDBCConnection(infoBean); - } else { - connection = ConnectionProcessorFactory.createConnection(infoBean, true); - } - connection.setConnectionName(infoBean.getConnectionName()); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - return connection; - } - return connection; + try { + Connection connection = cache.get(VALUE).get(infoBean.getConnectionName()); + if (connection != null) { + connection.setConnectionName(infoBean.getConnectionName()); + } + return connection; + } catch (ExecutionException e) { + throw new RuntimeException(e); + } } /** diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java b/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java index a5309a8ce0..6884df268d 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/preview/TableDataBeanHelper.java @@ -25,13 +25,10 @@ import com.fr.general.sql.SqlUtils; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.script.Calculator; -import com.fr.security.encryption.transmission.TransmissionEncryptionManager; import com.fr.stable.ParameterProvider; import com.fr.stable.StringUtils; -import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.DataEncryptionHelper; import com.fr.workspace.server.repository.tabledata.TableDataRepository; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; @@ -52,7 +49,7 @@ public class TableDataBeanHelper { */ public static Map getServerTableDatas() { final List beans = TableDataRepository.getInstance().getAllTableData(); - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); final Map tableDataMap = new HashMap<>(); try { for (ServerDataSetBean bean : beans) { diff --git a/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java b/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java index 5cd4893572..96441bb326 100644 --- a/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java +++ b/designer-base/src/main/java/com/fr/design/data/tabledata/wrapper/TableDataFactory.java @@ -31,7 +31,6 @@ import com.fr.design.data.tabledata.tabledatapane.TreeTableDataPane; import com.fr.log.FineLoggerFactory; import com.fr.stable.ArrayUtils; import com.fr.stable.StringUtils; -import com.fr.workspace.server.repository.connection.ConnectionRepository; import com.fr.workspace.server.repository.tabledata.TableDataRepository; import javax.swing.Icon; @@ -205,7 +204,7 @@ public abstract class TableDataFactory { clearAll(); try { List beans = TableDataRepository.getInstance().getAllTableData(); - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); Map tableDataMap = new HashMap<>(); for (ServerDataSetBean bean : beans) { tableDataMap.put(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); @@ -229,7 +228,7 @@ public abstract class TableDataFactory { Map tableDataMap = new HashMap<>(); try { List beans = TableDataRepository.getInstance().getAllTableData(); - final Map connectionMap = ConnectionInfoBeanHelper.createConnectionMap(ConnectionRepository.getInstance().getAll()); + final Map connectionMap = ConnectionInfoBeanHelper.getCurrentConnectionMap(); for (ServerDataSetBean bean : beans) { tableDataMap.put(bean.getDatasetName(), TableDataBeanHelper.getTableDataSet(connectionMap, bean.getDatasetType(), bean.getDatasetData())); }