From 05c8fbc51e9498a9693965f32f5688b6909cca69 Mon Sep 17 00:00:00 2001 From: lvzk <897706680@qq.com> Date: Fri, 7 Mar 2025 19:38:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]mongodb=E5=A4=9A?= =?UTF-8?q?=E7=A7=9F=E6=88=B7client=E3=80=81session=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20#[1371850741874688]mongodb=E5=A4=9A?= =?UTF-8?q?=E7=A7=9F=E6=88=B7client=E3=80=81session=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20http://192.168.0.96:8090/demo/rdm.html#/st?= =?UTF-8?q?ory-detail/939050947543040/939050947543042/1371850741874688?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/META-INF/root-context.xml | 9 -- .../threadlocal/MongodbSessionContext.java | 49 ++++++++++ .../listener/ThreadlocalClearListener.java | 3 + .../framework/store/mongodb/MongoConfig.java | 42 +++++++++ .../store/mongodb/MongoDbManager.java | 16 +++- .../mongodb/NeatLogicMongoDbFactory.java | 35 -------- .../NeatlogicMongoDatabaseFactory.java | 89 +++++++++++++++++++ 7 files changed, 197 insertions(+), 46 deletions(-) create mode 100644 src/main/java/neatlogic/framework/asynchronization/threadlocal/MongodbSessionContext.java create mode 100644 src/main/java/neatlogic/framework/store/mongodb/MongoConfig.java delete mode 100644 src/main/java/neatlogic/framework/store/mongodb/NeatLogicMongoDbFactory.java create mode 100644 src/main/java/neatlogic/framework/store/mongodb/NeatlogicMongoDatabaseFactory.java diff --git a/src/main/java/META-INF/root-context.xml b/src/main/java/META-INF/root-context.xml index e215ba63f..050bd2316 100644 --- a/src/main/java/META-INF/root-context.xml +++ b/src/main/java/META-INF/root-context.xml @@ -140,15 +140,6 @@ along with this program. If not, see .--> - - - - - - - - - diff --git a/src/main/java/neatlogic/framework/asynchronization/threadlocal/MongodbSessionContext.java b/src/main/java/neatlogic/framework/asynchronization/threadlocal/MongodbSessionContext.java new file mode 100644 index 000000000..87166b685 --- /dev/null +++ b/src/main/java/neatlogic/framework/asynchronization/threadlocal/MongodbSessionContext.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.asynchronization.threadlocal; + +import com.mongodb.client.ClientSession; + +public class MongodbSessionContext { + private ClientSession session; + // ThreadLocal 存储当前线程的 Session 对象 + private static final ThreadLocal sessionThreadLocal = new ThreadLocal<>(); + + public static MongodbSessionContext init(ClientSession _session) { + MongodbSessionContext context = new MongodbSessionContext(); + context.setSession(_session); + sessionThreadLocal.set(context); + return context; + } + + public static MongodbSessionContext get() { + return sessionThreadLocal.get(); + } + + public ClientSession getSession() { + return session; + } + + public void setSession(ClientSession session) { + this.session = session; + } + + public void release() { + sessionThreadLocal.remove(); + } +} diff --git a/src/main/java/neatlogic/framework/listener/ThreadlocalClearListener.java b/src/main/java/neatlogic/framework/listener/ThreadlocalClearListener.java index 3e46d1b5c..ebd4404b1 100644 --- a/src/main/java/neatlogic/framework/listener/ThreadlocalClearListener.java +++ b/src/main/java/neatlogic/framework/listener/ThreadlocalClearListener.java @@ -43,6 +43,9 @@ public class ThreadlocalClearListener implements ServletRequestListener { if (LicensePolicyContext.get() != null) { LicensePolicyContext.get().release(); } + if (MongodbSessionContext.get() != null) { + MongodbSessionContext.get().release(); + } CacheContext.release(); } diff --git a/src/main/java/neatlogic/framework/store/mongodb/MongoConfig.java b/src/main/java/neatlogic/framework/store/mongodb/MongoConfig.java new file mode 100644 index 000000000..3b3ad498b --- /dev/null +++ b/src/main/java/neatlogic/framework/store/mongodb/MongoConfig.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.store.mongodb; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.MongoTransactionManager; +import org.springframework.data.mongodb.core.MongoTemplate; + +@Configuration +public class MongoConfig { + + @Bean + public NeatlogicMongoDatabaseFactory neatlogicMongoDatabaseFactory() { + return new NeatlogicMongoDatabaseFactory(); + } + + @Bean + public MongoTemplate mongoTemplate(NeatlogicMongoDatabaseFactory factory) { + return new MongoTemplate(factory); + } + + @Bean + public MongoTransactionManager transactionManagerMongoDb(NeatlogicMongoDatabaseFactory factory) { + return new MongoTransactionManager(factory); // 使用标准事务管理器‌:ml-citation{ref="2,3" data="citationList"} + } +} diff --git a/src/main/java/neatlogic/framework/store/mongodb/MongoDbManager.java b/src/main/java/neatlogic/framework/store/mongodb/MongoDbManager.java index 4f78b61f0..6f3e5cf39 100644 --- a/src/main/java/neatlogic/framework/store/mongodb/MongoDbManager.java +++ b/src/main/java/neatlogic/framework/store/mongodb/MongoDbManager.java @@ -15,12 +15,14 @@ along with this program. If not, see .*/ package neatlogic.framework.store.mongodb; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.WriteConcern; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import neatlogic.framework.common.RootComponent; import neatlogic.framework.dao.mapper.MongoDbMapper; import neatlogic.framework.dto.MongoDbVo; -import org.apache.commons.lang3.StringUtils; import javax.annotation.PostConstruct; import javax.annotation.Resource; @@ -47,7 +49,17 @@ public class MongoDbManager { } public static void addDynamicDataSource(MongoDbVo mongoDbVo) { - MongoClient client = MongoClients.create("mongodb://" + mongoDbVo.getUsername() + ":" + mongoDbVo.getPasswordPlain() + "@" + mongoDbVo.getHost() + "/" + mongoDbVo.getDatabase() + (StringUtils.isNotBlank(mongoDbVo.getOption()) ? "?" + mongoDbVo.getOption() : "")); + MongoClientSettings settings = MongoClientSettings.builder() + .applyConnectionString(new ConnectionString("mongodb://" + + mongoDbVo.getUsername() + ":" + + mongoDbVo.getPasswordPlain() + "@" + + mongoDbVo.getHost() + "/" + + mongoDbVo.getDatabase() + "?" + + mongoDbVo.getOption())) + .writeConcern(WriteConcern.MAJORITY) // 添加这一行 + .build(); + MongoClient client = MongoClients.create(settings); + mongoDbMap.put(mongoDbVo.getTenantUuid(), client); mongoDatabaseMap.put(mongoDbVo.getTenantUuid(), mongoDbVo.getDatabase()); } diff --git a/src/main/java/neatlogic/framework/store/mongodb/NeatLogicMongoDbFactory.java b/src/main/java/neatlogic/framework/store/mongodb/NeatLogicMongoDbFactory.java deleted file mode 100644 index 1a3fc2204..000000000 --- a/src/main/java/neatlogic/framework/store/mongodb/NeatLogicMongoDbFactory.java +++ /dev/null @@ -1,35 +0,0 @@ -/*Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see .*/ - -package neatlogic.framework.store.mongodb; - -import com.mongodb.client.MongoDatabase; -import neatlogic.framework.asynchronization.threadlocal.TenantContext; -import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; - -public class NeatLogicMongoDbFactory extends SimpleMongoClientDatabaseFactory { - - public NeatLogicMongoDbFactory(String connectionString) { - super(connectionString); - } - - - @Override - protected MongoDatabase doGetMongoDatabase(String dbName) { - return MongoDbManager.getMongoClient(TenantContext.get().getTenantUuid()).getDatabase(MongoDbManager.getDatabase(TenantContext.get().getTenantUuid())); - } - - -} diff --git a/src/main/java/neatlogic/framework/store/mongodb/NeatlogicMongoDatabaseFactory.java b/src/main/java/neatlogic/framework/store/mongodb/NeatlogicMongoDatabaseFactory.java new file mode 100644 index 000000000..c6a4b17a7 --- /dev/null +++ b/src/main/java/neatlogic/framework/store/mongodb/NeatlogicMongoDatabaseFactory.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.store.mongodb; + +import com.mongodb.ClientSessionOptions; +import com.mongodb.client.ClientSession; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoDatabase; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.data.mongodb.MongoDatabaseFactory; +import org.springframework.data.mongodb.core.MongoExceptionTranslator; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) +public class NeatlogicMongoDatabaseFactory implements MongoDatabaseFactory { + + @Override + public @NonNull MongoDatabase getMongoDatabase() { + return getMongoDatabase(MongoDbManager.getDatabase(TenantContext.get().getTenantUuid())); + } + + @Override + public @NonNull MongoDatabase getMongoDatabase(@NonNull String dbName) { + return MongoDbManager.getMongoClient(TenantContext.get().getTenantUuid()).getDatabase(dbName); + } + + @Override + public @NonNull ClientSession getSession(@NonNull ClientSessionOptions options) { + // 确保 MongoClient 是从租户管理中获取的 + return MongoDbManager.getMongoClient(TenantContext.get().getTenantUuid()).startSession(options); + } + + @Override + public @NonNull MongoExceptionTranslator getExceptionTranslator() { + return new MongoExceptionTranslator(); + } + + @Override + public @NonNull MongoDatabaseFactory withSession(@NonNull ClientSession session) { + MongoClient mongoClient = MongoDbManager.getMongoClient(TenantContext.get().getTenantUuid()); + + return new MongoDatabaseFactory() { + @Override + public @NonNull MongoDatabase getMongoDatabase() { + return getMongoDatabase(MongoDbManager.getDatabase(TenantContext.get().getTenantUuid())); + } + + @Override + public @NonNull MongoDatabase getMongoDatabase(@NonNull String dbName) { + return mongoClient.getDatabase(dbName); + } + + @Override + public @NonNull ClientSession getSession(@NonNull ClientSessionOptions options) { + return session; + } + + @Override + public @NonNull MongoExceptionTranslator getExceptionTranslator() { + return new MongoExceptionTranslator(); + } + + @Override + public @NonNull MongoDatabaseFactory withSession(@NonNull ClientSession session) { + return this; + } + }; + } +} + -- Gitee From 69c846924279c62d77017f415ba55494c102bc20 Mon Sep 17 00:00:00 2001 From: lvzk <897706680@qq.com> Date: Tue, 11 Mar 2025 19:40:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]mongodb=E5=A4=9A?= =?UTF-8?q?=E7=A7=9F=E6=88=B7client=E3=80=81session=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20#[1371850741874688]mongodb=E5=A4=9A?= =?UTF-8?q?=E7=A7=9F=E6=88=B7client=E3=80=81session=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20http://192.168.0.96:8090/demo/rdm.html#/st?= =?UTF-8?q?ory-detail/939050947543040/939050947543042/1371850741874688?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thread/NeatLogicThread.java | 4 +++ .../framework/common/config/Config.java | 25 ------------------- .../framework/common/config/LocalConfig.java | 6 ----- 3 files changed, 4 insertions(+), 31 deletions(-) diff --git a/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java b/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java index 22e9bacb9..8958eef6c 100644 --- a/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java +++ b/src/main/java/neatlogic/framework/asynchronization/thread/NeatLogicThread.java @@ -30,6 +30,7 @@ import java.util.concurrent.Semaphore; public abstract class NeatLogicThread implements Runnable, Comparable { private static final Logger logger = LoggerFactory.getLogger(NeatLogicThread.class); protected UserContext userContext; + protected MongodbSessionContext mongodbSessionContext; private String tenantUuid; private List activeModuleList; protected InputFromContext inputFromContext; @@ -97,6 +98,7 @@ public abstract class NeatLogicThread implements Runnable, Comparable Date: Wed, 12 Mar 2025 14:51:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E6=97=B6=E9=94=81=E7=AB=9E=E4=BA=89=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neatlogic/framework/dao/mapper/runner/RunnerMapper.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/neatlogic/framework/dao/mapper/runner/RunnerMapper.xml b/src/main/java/neatlogic/framework/dao/mapper/runner/RunnerMapper.xml index eba7bfc7f..6c5753806 100644 --- a/src/main/java/neatlogic/framework/dao/mapper/runner/RunnerMapper.xml +++ b/src/main/java/neatlogic/framework/dao/mapper/runner/RunnerMapper.xml @@ -582,9 +582,8 @@ along with this program. If not, see .--> - INSERT INTO `runner_map` (`id`, `runner_id`) + INSERT IGNORE INTO `runner_map` (`id`, `runner_id`) VALUES (#{runnerMapId}, #{id}) - ON DUPLICATE KEY UPDATE `runner_id` = #{id} ; -- Gitee