Revision: 5907
          http://sourceforge.net/p/jump-pilot/code/5907
Author:   michaudm
Date:     2018-06-30 20:32:37 +0000 (Sat, 30 Jun 2018)
Log Message:
-----------
Fix #478 : AdHoc SQL query can be interrupted properly

Modified Paths:
--------------
    core/trunk/ChangeLog
    
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/datastore/RunDatastoreQueryPlugIn.java

Modified: core/trunk/ChangeLog
===================================================================
--- core/trunk/ChangeLog        2018-06-30 17:46:04 UTC (rev 5906)
+++ core/trunk/ChangeLog        2018-06-30 20:32:37 UTC (rev 5907)
@@ -3,11 +3,15 @@
 # 2. make sure that lines break at 80 chars for constricted display situations
 #<-------------------------------- 80 chars 
---------------------------------->#
 
-2018-06-26 Giuseppe Aruta. DEMStatisticsPlugIn. Substitute output HTMLDoc 
-     with JTable. Allow selection of multiple layers on plugin dialog. 
-     Extend to multi band raster files
+2018-06-30 mmichaud <m.michael.mich...@orange.fr>
+  * Fix #478 : AdHoc SQL query can be interrupted properly
 
-2018-06-25
+2018-06-26 Giuseppe Aruta.
+  * DEMStatisticsPlugIn. Substitute output HTMLDoc with JTable.
+    Allow selection of multiple layers on plugin dialog.
+    Extend to multi band raster files
+
+2018-06-25 mmichaud <m.michael.mich...@orange.fr>
   * Modify slightly the way the windows .bat search for java
 
 2018-06-11 Giuseppe Aruta. Raster profile plugin:

Modified: 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/datastore/RunDatastoreQueryPlugIn.java
===================================================================
--- 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/datastore/RunDatastoreQueryPlugIn.java
   2018-06-30 17:46:04 UTC (rev 5906)
+++ 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/datastore/RunDatastoreQueryPlugIn.java
   2018-06-30 20:32:37 UTC (rev 5907)
@@ -68,19 +68,28 @@
         if (driver.contains("Postgis") && 
query.matches("(?s).*\\$\\{[^\\{\\}]*\\}.*")) {
             query = DataStoreQueryDataSource.expandQuery(query, context);
         }
+        final AdhocQuery adhocQuery = new AdhocQuery(query);
         // end
         // Nicolas Ribot, 08 dec 2015:
         // manages several datasources now
         ConnectionDescriptor desc = panel.getConnectionDescriptor();
-        DataStoreConnection dscon = 
ConnectionManager.instance(context.getWorkbenchContext()).getOpenConnection(desc);
-        
-        FeatureInputStream featureInputStream = dscon.execute(new 
AdhocQuery(query));
-        
-//        FeatureInputStream featureInputStream =
-//            ConnectionManager.instance(context.getWorkbenchContext())
-//                .getOpenConnection(panel.getConnectionDescriptor())
-//                .execute(new AdhocQuery(query));
+        final DataStoreConnection dscon = 
ConnectionManager.instance(context.getWorkbenchContext()).getOpenConnection(desc);
+        RunnableQuery rQuery = new RunnableQuery(dscon, adhocQuery);
+        FeatureInputStream featureInputStream;
+
         try {
+            // SQL query is execute in a separate thread to give the user a 
chance
+            // to interrupt it
+            rQuery.start();
+            while (rQuery.getState() == Thread.State.RUNNABLE) {
+                Thread.sleep(1000);
+                if (monitor.isCancelRequested()) {
+                    throw new InterruptedException("The following query has 
been interrupted :\n" +
+                            adhocQuery.getQuery());
+                }
+            }
+            rQuery.join();
+            featureInputStream = rQuery.getFeatureInputStream();
             FeatureDataset featureDataset = new FeatureDataset(
                 featureInputStream.getFeatureSchema());
             int i = 0;
@@ -101,16 +110,32 @@
             return layer;
         }
         finally {
-            // This code had been added as an attempt to cancel a long running 
query
-            // but it has a side effect on the connection which is closed
-            // This peace of code is removed until a better solution is found 
-            //if (featureInputStream instanceof 
com.vividsolutions.jump.datastore.postgis.PostgisFeatureInputStream) {
-            //    java.sql.Statement stmt = 
-            //    
((com.vividsolutions.jump.datastore.postgis.PostgisFeatureInputStream)featureInputStream).getStatement();
-            //    if (stmt != null) stmt.cancel();
-            //}
-            featureInputStream.close();
+            dscon.close();
         }
     }
+
+    class RunnableQuery extends Thread {
+
+        DataStoreConnection connection;
+        AdhocQuery query;
+        FeatureInputStream featureInputStream;
+
+        RunnableQuery(DataStoreConnection connection, AdhocQuery query) {
+            this.connection = connection;
+            this.query = query;
+        }
+
+        public void run() {
+            try {
+                featureInputStream = connection.execute(query);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        FeatureInputStream getFeatureInputStream() {
+            return featureInputStream;
+        }
+    }
     
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to