Skip to content

Commit 36a5490

Browse files
committed
优化代码
1 parent c98e19b commit 36a5490

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Override execute in your SQLExecutor extends AbstractSQLExecutor
7373
@Override
7474
public JSONObject execute(@NotNull SQLConfig<Long> config, boolean unknownType) throws Exception {
7575
if (config.isMilvus()) {
76-
return MilvusUtil.execute(config, unknownType);
76+
return MilvusUtil.execute(config, null, unknownType);
7777
}
7878

7979
return super.execute(config, unknownType);

src/main/java/apijson/milvus/MilvusUtil.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,39 @@
5151
public class MilvusUtil {
5252
public static final String TAG = "MilvusUtil";
5353

54+
public static <T> String getClientKey(@NotNull SQLConfig<T> config) {
55+
String uri = config.getDBUri();
56+
return uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
57+
}
58+
5459
public static final Map<String, MilvusServiceClient> CLIENT_MAP = new LinkedHashMap<>();
5560
public static <T> MilvusServiceClient getClient(@NotNull SQLConfig<T> config) {
56-
String uri = config.getDBUri();
57-
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
61+
return getClient(config, true);
62+
}
63+
public static <T> MilvusServiceClient getClient(@NotNull SQLConfig<T> config, boolean autoNew) {
64+
String key = getClientKey(config);
5865

59-
MilvusServiceClient conn = CLIENT_MAP.get(key);
60-
if (conn == null) {
61-
conn = new MilvusServiceClient(
66+
MilvusServiceClient client = CLIENT_MAP.get(key);
67+
if (autoNew && client == null) {
68+
client = new MilvusServiceClient(
6269
ConnectParam.newBuilder()
6370
.withUri(config.getDBUri())
6471
.withAuthorization(config.getDBAccount(), config.getDBPassword())
6572
.build()
6673
);
67-
CLIENT_MAP.put(key, conn);
74+
CLIENT_MAP.put(key, client);
6875
}
69-
return conn;
76+
return client;
7077
}
7178

7279
public static <T> void closeClient(@NotNull SQLConfig<T> config) {
73-
MilvusServiceClient conn = getClient(config);
74-
if (conn != null) {
75-
String uri = config.getDBUri();
76-
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
80+
MilvusServiceClient client = getClient(config, false);
81+
if (client != null) {
82+
String key = getClientKey(config);
7783
CLIENT_MAP.remove(key);
7884

7985
// try {
80-
conn.close();
86+
client.close();
8187
// }
8288
// catch (Throwable e) {
8389
// e.printStackTrace();

0 commit comments

Comments
 (0)