46
46
public class CassandraUtil {
47
47
public static final String TAG = "CassandraUtil" ;
48
48
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
+
49
54
public static final Map <String , CqlSession > CLIENT_MAP = new LinkedHashMap <>();
50
55
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 );
53
60
54
61
CqlSession session = CLIENT_MAP .get (key );
55
- if (session == null ) {
62
+ if (autoNew && session == null ) {
56
63
session = CqlSession .builder ()
57
64
// .withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
58
65
.withCloudSecureConnectBundle (new URL (config .getDBUri ()))
@@ -66,14 +73,13 @@ public static <T> CqlSession getSession(@NotNull SQLConfig<T> config) throws Mal
66
73
}
67
74
68
75
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 );
73
79
CLIENT_MAP .remove (key );
74
80
75
81
// try {
76
- conn .close ();
82
+ session .close ();
77
83
// }
78
84
// catch (Throwable e) {
79
85
// e.printStackTrace();
0 commit comments