3232import java .sql .SQLException ;
3333import java .sql .SQLWarning ;
3434import java .sql .Statement ;
35+ import java .util .ArrayList ;
3536import java .util .HashMap ;
37+ import java .util .List ;
3638import java .util .Map ;
3739import java .util .Properties ;
3840import java .util .Random ;
@@ -861,7 +863,7 @@ public void testMultipleCancels() throws Exception {
861863 public void testCancelQueryWithBrokenNetwork () throws SQLException , IOException , InterruptedException {
862864 // check that stmt.cancel() doesn't hang forever if the network is broken
863865
864- ExecutorService executor = Executors .newSingleThreadExecutor ();
866+ ExecutorService executor = Executors .newCachedThreadPool ();
865867
866868 try (StrangeProxyServer proxyServer = new StrangeProxyServer (TestUtil .getServer (), TestUtil .getPort ())) {
867869 Properties props = new Properties ();
@@ -875,6 +877,9 @@ public void testCancelQueryWithBrokenNetwork() throws SQLException, IOException,
875877 proxyServer .stopForwardingAllClients ();
876878
877879 stmt .cancel ();
880+ // Note: network is still inaccessible, so the statement execution is still in progress.
881+ // So we abort the connection to allow implicit conn.close()
882+ conn .abort (executor );
878883 }
879884 }
880885
@@ -940,12 +945,13 @@ public Void call() throws Exception {
940945 }
941946
942947 @ Test (timeout = 10000 )
943- public void testConcurrentIsValid () throws InterruptedException {
948+ public void testConcurrentIsValid () throws Throwable {
944949 ExecutorService executor = Executors .newCachedThreadPool ();
945950 try {
951+ List <Future <?>> results = new ArrayList <>();
946952 Random rnd = new Random ();
947953 for (int i = 0 ; i < 10 ; i ++) {
948- executor .submit (() -> {
954+ Future <?> future = executor .submit (() -> {
949955 try {
950956 for (int j = 0 ; j < 50 ; j ++) {
951957 con .isValid (1 );
@@ -969,7 +975,14 @@ public void testConcurrentIsValid() throws InterruptedException {
969975 throw new RuntimeException (e );
970976 }
971977 });
978+ results .add (future );
979+ }
980+ for (Future <?> result : results ) {
981+ // Propagate exception if any
982+ result .get ();
972983 }
984+ } catch (ExecutionException e ) {
985+ throw e .getCause ();
973986 } finally {
974987 executor .shutdown ();
975988 executor .awaitTermination (10 , TimeUnit .SECONDS );
0 commit comments