@@ -136,47 +136,51 @@ private static void testSessionLevel() throws Exception {
136136 .build ();
137137 MilvusClientV2Pool pool = new MilvusClientV2Pool (poolConfig , connectConfig );
138138
139- // The same process, different MilvusClient object, insert and search with Session level.
140- // The Session level ensure that the newly inserted data instantaneously become searchable.
141- Gson gson = new Gson ();
142- for (int i = 0 ; i < 100 ; i ++) {
143- List <Float > vector = CommonUtils .generateFloatVector (VECTOR_DIM );
144- JsonObject row = new JsonObject ();
145- row .addProperty ("id" , i );
146- row .add ("vector" , gson .toJsonTree (vector ));
147-
148- // insert by a MilvusClient
149- String clientName1 = String .format ("client_%d" , i % 10 );
150- MilvusClientV2 client1 = pool .getClient (clientName1 );
151- client1 .insert (InsertReq .builder ()
152- .collectionName (collectionName )
153- .data (Collections .singletonList (row ))
154- .build ());
155- pool .returnClient (clientName1 , client1 ); // don't forget to return the client to pool
156- System .out .println ("insert" );
157-
158- // search by another MilvusClient, use the just inserted vector to search
159- // the returned item is expected to be the just inserted item
160- String clientName2 = String .format ("client_%d" , i % 10 + 1 );
161- MilvusClientV2 client2 = pool .getClient (clientName2 );
162- SearchResp searchR = client2 .search (SearchReq .builder ()
163- .collectionName (collectionName )
164- .data (Collections .singletonList (new FloatVec (vector )))
165- .limit (1 )
166- .build ());
167- pool .returnClient (clientName2 , client2 ); // don't forget to return the client to pool
168- List <List <SearchResp .SearchResult >> searchResults = searchR .getSearchResults ();
169- List <SearchResp .SearchResult > results = searchResults .get (0 );
170- if (results .size () != 1 ) {
171- throw new RuntimeException ("Search result is empty" );
172- }
173- if (i != (Long ) results .get (0 ).getId ()) {
174- throw new RuntimeException ("The just inserted entity is not found" );
139+ try {
140+ // The same process, different MilvusClient object, insert and search with Session level.
141+ // The Session level ensure that the newly inserted data instantaneously become searchable.
142+ Gson gson = new Gson ();
143+ for (int i = 0 ; i < 100 ; i ++) {
144+ List <Float > vector = CommonUtils .generateFloatVector (VECTOR_DIM );
145+ JsonObject row = new JsonObject ();
146+ row .addProperty ("id" , i );
147+ row .add ("vector" , gson .toJsonTree (vector ));
148+
149+ // insert by a MilvusClient
150+ String clientName1 = String .format ("client_%d" , i % 10 );
151+ MilvusClientV2 client1 = pool .getClient (clientName1 );
152+ client1 .insert (InsertReq .builder ()
153+ .collectionName (collectionName )
154+ .data (Collections .singletonList (row ))
155+ .build ());
156+ pool .returnClient (clientName1 , client1 ); // don't forget to return the client to pool
157+ System .out .println ("insert" );
158+
159+ // search by another MilvusClient, use the just inserted vector to search
160+ // the returned item is expected to be the just inserted item
161+ String clientName2 = String .format ("client_%d" , i % 10 + 1 );
162+ MilvusClientV2 client2 = pool .getClient (clientName2 );
163+ SearchResp searchR = client2 .search (SearchReq .builder ()
164+ .collectionName (collectionName )
165+ .data (Collections .singletonList (new FloatVec (vector )))
166+ .limit (1 )
167+ .build ());
168+ pool .returnClient (clientName2 , client2 ); // don't forget to return the client to pool
169+ List <List <SearchResp .SearchResult >> searchResults = searchR .getSearchResults ();
170+ List <SearchResp .SearchResult > results = searchResults .get (0 );
171+ if (results .size () != 1 ) {
172+ throw new RuntimeException ("Search result is empty" );
173+ }
174+ if (i != (Long ) results .get (0 ).getId ()) {
175+ throw new RuntimeException ("The just inserted entity is not found" );
176+ }
177+ System .out .println ("search" );
175178 }
176- System .out .println ("search" );
177- }
178179
179- System .out .println ("Session level is working fine" );
180+ System .out .println ("Session level is working fine" );
181+ } finally {
182+ pool .close ();
183+ }
180184 }
181185
182186 private static void testBoundedLevel () {
@@ -207,5 +211,7 @@ public static void main(String[] args) throws Exception {
207211 testBoundedLevel ();
208212 System .out .println ("==============================================================" );
209213 testEventuallyLevel ();
214+
215+ client .close ();
210216 }
211217}
0 commit comments