Skip to content

Commit 06e02d9

Browse files
committed
优化代码
1 parent 35d89ee commit 06e02d9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/java/apijson/cassandra/CassandraUtil.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@
4646
public class CassandraUtil {
4747
public static final String TAG = "CassandraUtil";
4848

49+
public static <T> String getClientKey(@NotNull SQLConfig<T> config) {
50+
String uri = config.getDBUri();
51+
return uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
52+
}
53+
4954
public static final Map<String, CqlSession> CLIENT_MAP = new LinkedHashMap<>();
5055
public static <T> CqlSession getSession(@NotNull SQLConfig<T> config) throws MalformedURLException {
51-
String uri = config.getDBUri();
52-
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
56+
return getSession(config, true);
57+
}
58+
public static <T> CqlSession getSession(@NotNull SQLConfig<T> config, boolean autoNew) throws MalformedURLException {
59+
String key = getClientKey(config);
5360

5461
CqlSession session = CLIENT_MAP.get(key);
55-
if (session == null) {
62+
if (autoNew && session == null) {
5663
session = CqlSession.builder()
5764
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
5865
.withCloudSecureConnectBundle(new URL(config.getDBUri()))
@@ -66,14 +73,13 @@ public static <T> CqlSession getSession(@NotNull SQLConfig<T> config) throws Mal
6673
}
6774

6875
public static <T> void closeSession(@NotNull SQLConfig<T> config) throws MalformedURLException {
69-
CqlSession conn = getSession(config);
70-
if (conn != null) {
71-
String uri = config.getDBUri();
72-
String key = uri + (uri.contains("?") ? "&" : "?") + "username=" + config.getDBAccount();
76+
CqlSession session = getSession(config, false);
77+
if (session != null) {
78+
String key = getClientKey(config);
7379
CLIENT_MAP.remove(key);
7480

7581
// try {
76-
conn.close();
82+
session.close();
7783
// }
7884
// catch (Throwable e) {
7985
// e.printStackTrace();

0 commit comments

Comments
 (0)