|
51 | 51 | public class MilvusUtil { |
52 | 52 | public static final String TAG = "MilvusUtil"; |
53 | 53 |
|
| 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 | + |
54 | 59 | public static final Map<String, MilvusServiceClient> CLIENT_MAP = new LinkedHashMap<>(); |
55 | 60 | 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); |
58 | 65 |
|
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( |
62 | 69 | ConnectParam.newBuilder() |
63 | 70 | .withUri(config.getDBUri()) |
64 | 71 | .withAuthorization(config.getDBAccount(), config.getDBPassword()) |
65 | 72 | .build() |
66 | 73 | ); |
67 | | - CLIENT_MAP.put(key, conn); |
| 74 | + CLIENT_MAP.put(key, client); |
68 | 75 | } |
69 | | - return conn; |
| 76 | + return client; |
70 | 77 | } |
71 | 78 |
|
72 | 79 | 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); |
77 | 83 | CLIENT_MAP.remove(key); |
78 | 84 |
|
79 | 85 | // try { |
80 | | - conn.close(); |
| 86 | + client.close(); |
81 | 87 | // } |
82 | 88 | // catch (Throwable e) { |
83 | 89 | // e.printStackTrace(); |
|
0 commit comments