will cancel execution when stopping from progress handle
authorchrislovsund@netbeans.org
Mon, 27 May 2013 22:21:10 +0200
changeset 4149a3adf102190
parent 413 472826f8e6e0
child 415 8519fa8e0853
will cancel execution when stopping from progress handle
EADS-3744 - cancel from progressbar should work
PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlExecutor.java
PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlFileExecutor.java
PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/StatementHolder.java
     1.1 --- a/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlExecutor.java	Mon May 27 17:47:13 2013 +0200
     1.2 +++ b/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlExecutor.java	Mon May 27 22:21:10 2013 +0200
     1.3 @@ -7,6 +7,7 @@
     1.4  import java.beans.PropertyChangeListener;
     1.5  import java.sql.Connection;
     1.6  import java.util.List;
     1.7 +import java.util.logging.Level;
     1.8  import java.util.logging.Logger;
     1.9  import javax.swing.JOptionPane;
    1.10  import javax.swing.text.Document;
    1.11 @@ -40,13 +41,14 @@
    1.12      private final DatabaseConnectionIO io;
    1.13      private final DatabaseTransaction transaction;
    1.14      private DatabaseConnection connection;
    1.15 +    private final StatementHolder statementHolder;
    1.16  
    1.17 -    //    private final boolean isCommandWindow;
    1.18      public PlsqlExecutor(DatabaseConnectionManager connectionProvider, DatabaseConnectionIO io, DatabaseConnection connection, DatabaseTransaction transaction) {
    1.19          this.connectionProvider = connectionProvider;
    1.20          this.io = io;
    1.21          this.connection = connection;
    1.22          this.transaction = transaction;
    1.23 +        this.statementHolder = new StatementHolder();
    1.24      }
    1.25  
    1.26      @Override
    1.27 @@ -95,9 +97,6 @@
    1.28          return transaction.hasOpenTransaction();
    1.29      }
    1.30  
    1.31 -//    public void openTransaction() {
    1.32 -//        transaction.open();
    1.33 -//    }
    1.34      public Connection getJDBCConnection() {
    1.35          return connection.getJDBCConnection();
    1.36      }
    1.37 @@ -162,11 +161,11 @@
    1.38      private RequestProcessor.Task task;
    1.39  
    1.40      private boolean handleCancel() {
    1.41 -        LOG.info("handleCancel");
    1.42 +        LOG.log(Level.INFO, "Cancel {0}", task.toString());
    1.43          if (null == task) {
    1.44              return false;
    1.45          }
    1.46 -
    1.47 +        statementHolder.cancel();
    1.48          return task.cancel();
    1.49      }
    1.50  
    1.51 @@ -191,7 +190,6 @@
    1.52  
    1.53          @Override
    1.54          public void run() {
    1.55 -//            ProgressHandle handle = ProgressHandleFactory.createHandle("Executing database file...", this);
    1.56              try {
    1.57                  handle.start();
    1.58                  // If autocommit OFF - take the connection from data object.
    1.59 @@ -217,12 +215,10 @@
    1.60  //                    return;
    1.61  //                }
    1.62                  reconnectIfNeeded();
    1.63 -                executor = new PlsqlFileExecutor(connectionProvider, connection, io.getIO());
    1.64 +                executor = new PlsqlFileExecutor(statementHolder, connection, io.getIO());
    1.65                  executor.executePLSQL(executableObjects, document);
    1.66              } catch (InterruptedException ex) {
    1.67 -                LOG.info("the task was CANCELLED");
    1.68 -//                return;
    1.69 -
    1.70 +                io.println("the task was CANCELLED");
    1.71              } finally {
    1.72                  if (autoCommit()) {
    1.73                      connectionProvider.releaseDatabaseConnection(connection);
    1.74 @@ -233,16 +229,6 @@
    1.75              }
    1.76          }
    1.77  
    1.78 -//        @Override
    1.79 -//        public boolean cancel() {
    1.80 -//            if (executor != null) {
    1.81 -////                executor.cancel();
    1.82 -////                io.getErr().println("!!!Execution cancelled. Performing rollback");
    1.83 -////                jdbcConnection.rollback();
    1.84 -//            }
    1.85 -//            return true;
    1.86 -//        }
    1.87 -//    }
    1.88          private void reconnectIfNeeded() {
    1.89              //to reconnect if the connection is gone. 
    1.90              if (connection.getJDBCConnection() == null) {
     2.1 --- a/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlFileExecutor.java	Mon May 27 17:47:13 2013 +0200
     2.2 +++ b/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/PlsqlFileExecutor.java	Mon May 27 22:21:10 2013 +0200
     2.3 @@ -41,9 +41,6 @@
     2.4   */
     2.5  package org.netbeans.modules.plsql.execution.impl;
     2.6  
     2.7 -import org.netbeans.modules.plsql.execution.*;
     2.8 -import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObjectType;
     2.9 -import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObject;
    2.10  import java.awt.Component;
    2.11  import java.awt.datatransfer.Clipboard;
    2.12  import java.awt.datatransfer.StringSelection;
    2.13 @@ -58,6 +55,8 @@
    2.14  import java.sql.*;
    2.15  import java.util.*;
    2.16  import java.util.Date;
    2.17 +import java.util.logging.Level;
    2.18 +import java.util.logging.Logger;
    2.19  import javax.swing.JButton;
    2.20  import javax.swing.JMenuItem;
    2.21  import javax.swing.JOptionPane;
    2.22 @@ -79,13 +78,19 @@
    2.23  import org.netbeans.modules.db.sql.execute.StatementInfo;
    2.24  import org.netbeans.modules.db.sql.history.SQLHistoryEntry;
    2.25  import org.netbeans.modules.db.sql.history.SQLHistoryManager;
    2.26 +import org.netbeans.modules.plsql.execution.FileExecutionUtil;
    2.27 +import org.netbeans.modules.plsql.execution.PlsqlErrorObject;
    2.28 +import org.netbeans.modules.plsql.execution.PlsqlExecutableBlocksMaker;
    2.29 +import org.netbeans.modules.plsql.execution.PlsqlOutputListener;
    2.30 +import org.netbeans.modules.plsql.execution.PromptDialog;
    2.31  import org.netbeans.modules.plsql.filetype.PlsqlEditor;
    2.32  import org.netbeans.modules.plsql.filetype.StatementExecutionHistory;
    2.33  import org.netbeans.modules.plsql.lexer.PlsqlTokenId;
    2.34  import org.netbeans.modules.plsql.utilities.PlsqlFileUtil;
    2.35  import org.netbeans.modules.plsql.utilities.PlsqlFileValidatorService;
    2.36 -import org.netbeans.modules.plsqlsupport.db.DatabaseConnectionManager;
    2.37  import org.netbeans.modules.plsqlsupport.db.DatabaseContentManager;
    2.38 +import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObject;
    2.39 +import org.netbeans.modules.plsqlsupport.db.PlsqlExecutableObjectType;
    2.40  import org.openide.DialogDisplayer;
    2.41  import org.openide.NotifyDescriptor;
    2.42  import org.openide.filesystems.FileObject;
    2.43 @@ -101,42 +106,24 @@
    2.44  
    2.45  public class PlsqlFileExecutor {
    2.46  
    2.47 -//    private static final RequestProcessor RP = new RequestProcessor("SQLExecution", 4, true);
    2.48      private static final PlsqlFileValidatorService validator = Lookup.getDefault().lookup(PlsqlFileValidatorService.class);
    2.49 -//    private boolean cancel = false;
    2.50 -//    private Connection debugConnection;
    2.51 -    private final DatabaseConnectionManager connectionProvider;
    2.52 +    private final StatementHolder statementHolder;
    2.53      private final DatabaseConnection connection;
    2.54      private final Connection jdbcConnection;
    2.55 -//    private final List<PlsqlExecutableObject> executableObjs;
    2.56 -//    private final Document doc;
    2.57      private PlsqlEditor plsqlEditor;
    2.58      private final InputOutput io;
    2.59      private final DatabaseContentManager cache;
    2.60      private final String connectionDisplayName;
    2.61  
    2.62 -    PlsqlFileExecutor(DatabaseConnectionManager connectionProvider, DatabaseConnection connection, InputOutput io) {
    2.63 +    PlsqlFileExecutor(StatementHolder holder, DatabaseConnection connection, InputOutput io) {
    2.64          this.connectionDisplayName = "Using DB: " + connection.getDisplayName() + " [" + connection.getName() + "]";
    2.65          this.connection = connection;
    2.66          this.jdbcConnection = connection.getJDBCConnection();
    2.67 -//        this.executableObjs = executableObjects;
    2.68 -//        this.doc = document;
    2.69          this.io = io;
    2.70 -        this.connectionProvider = connectionProvider;
    2.71 +        this.statementHolder = holder;
    2.72          this.cache = DatabaseContentManager.getInstance(connection);
    2.73      }
    2.74  
    2.75 -    //    public PlsqlFileExecutor(DatabaseConnectionManager connectionProvider, Connection debugConnection, InputOutput io) {
    2.76 -    //        this.connection = null;
    2.77 -    //        this.connectionDisplayName = null;
    2.78 -    //        this.debugConnection = debugConnection;
    2.79 -    //        this.preparedIO = io;
    2.80 -    //        this.connectionProvider = connectionProvider;
    2.81 -    //        this.cache = DatabaseContentManager.getInstance(connection);
    2.82 -    //    }
    2.83 -//    public void cancel() {
    2.84 -//        cancel = true;
    2.85 -//    }
    2.86      /**
    2.87       * Method that will execute Dbmb_Output.Enable();
    2.88       *
    2.89 @@ -166,9 +153,6 @@
    2.90          DataObject obj = FileExecutionUtil.getDataObject(doc);
    2.91          SQLExecutor executor = new SQLExecutor(obj, con, formattedQuery, label);
    2.92          executor.run();
    2.93 -//        RequestProcessor.Task task = RP.create(executor);
    2.94 -//        executor.setTask(task);
    2.95 -//        task.run();
    2.96          return false;
    2.97      }
    2.98      final static int NORMAL = 0;
    2.99 @@ -384,10 +368,8 @@
   2.100          if (validator.isValidTDB(dataObj)) {
   2.101              endMsg = "Finished executing command ";
   2.102          }
   2.103 -//        Connection con;
   2.104          Statement stm = null;
   2.105          String firstWord = null;
   2.106 -//        DatabaseTransaction transaction = DatabaseTransaction.getInstance((DataObject) object);
   2.107  
   2.108          //quick & dirty fix to avoid having output tabs for the SQL Execution window (unless there's an exception)
   2.109          //first check to see if this is a simple select statement and if so treat it separately.
   2.110 @@ -419,7 +401,6 @@
   2.111  
   2.112                  } catch (SQLException sqlEx) {
   2.113                      try {
   2.114 -//                        io = initializeIO(fileName, dataObj.getNodeDelegate().getDisplayName(), dataObj, executableObjs.get(0));
   2.115                          int errLine = getLineNumberFromMsg(sqlEx.getMessage());
   2.116                          int outLine = executionObject.getStartLineNo() + errLine - 1;
   2.117                          String msg = getmodifiedErorrMsg(sqlEx.getMessage(), outLine);
   2.118 @@ -434,7 +415,7 @@
   2.119                          io.getErr().close();
   2.120                          return;
   2.121                      } catch (IOException ex) {
   2.122 -                        connectionProvider.setOnline(false);
   2.123 +//                        connectionProvider.setOnline(false);
   2.124                          return;
   2.125                      }
   2.126                  }
   2.127 @@ -450,22 +431,17 @@
   2.128              io.getOut().println(startMsg);
   2.129              io.getOut().println("-------------------------------------------------------------");
   2.130  
   2.131 -//            io = initializeIO(fileName, dataObj.getNodeDelegate().getDisplayName(), dataObj, executableObjs.get(0));
   2.132 -//            con = debugConnection != null ? debugConnection : connection.getJDBCConnection();
   2.133              jdbcConnection.setAutoCommit(false);
   2.134              enableDbmsOut(jdbcConnection);
   2.135  
   2.136              stm = jdbcConnection.createStatement();
   2.137 +            statementHolder.setStatement(stm);
   2.138              stm.setEscapeProcessing(false);
   2.139 +            LOG.log(Level.FINE, "stm={0}", stm);
   2.140              boolean firstSelectStatement = true;
   2.141  
   2.142              for (PlsqlExecutableObject exeObj : executableObjs) {
   2.143                  Thread.sleep(0); //throws InterruptedException is the task was cancelled
   2.144 -                //                if (cancel) {
   2.145 -                //                    io.getErr().println("!!!Execution cancelled. Performing rollback");
   2.146 -                //                    jdbcConnection.rollback();
   2.147 -                //                    return io;
   2.148 -//                }
   2.149                  int lineNumber = exeObj.getStartLineNo();
   2.150                  String plsqlText = exeObj.getPlsqlString();
   2.151  
   2.152 @@ -1003,6 +979,7 @@
   2.153          }
   2.154  //        return deploymentOk ? null : io;
   2.155      }
   2.156 +    private static final Logger LOG = Logger.getLogger(PlsqlFileExecutor.class.getName());
   2.157  
   2.158      public String getmodifiedErorrMsg(String msg, int lineNumber) {
   2.159          int index = msg.indexOf("\n");
   2.160 @@ -1015,8 +992,6 @@
   2.161  
   2.162      private List<PlsqlErrorObject> getPackageerrors(String packageName, String packageType) {
   2.163          List<PlsqlErrorObject> errorObjects = new ArrayList<PlsqlErrorObject>();
   2.164 -//        Connection con = debugConnection != null ? debugConnection : connection.getJDBCConnection();
   2.165 -//        if (con != null) {
   2.166          Statement stm = null;
   2.167          try {
   2.168              stm = jdbcConnection.createStatement();
   2.169 @@ -1042,9 +1017,6 @@
   2.170                  throw new RuntimeException(ex);
   2.171              }
   2.172          }
   2.173 -//        } else {
   2.174 -//            throw new RuntimeException("Not Connected to Database to Get USER_ERRORS");
   2.175 -//        }
   2.176          return errorObjects;
   2.177      }
   2.178  
   2.179 @@ -1358,7 +1330,6 @@
   2.180          private final DatabaseConnection dbconn;
   2.181          private final String sqlStmt;
   2.182          private String label;
   2.183 -//        private RequestProcessor.Task task;
   2.184          private DataObject parent;
   2.185  
   2.186          public SQLExecutor(DataObject dataObj, DatabaseConnection dbconn, String sqlStmt, String label) {
   2.187 @@ -1368,12 +1339,8 @@
   2.188              this.label = label;
   2.189          }
   2.190  
   2.191 -//        public void setTask(RequestProcessor.Task task) {
   2.192 -//            this.task = task;
   2.193 -//        }
   2.194          @Override
   2.195          public void run() {
   2.196 -//            assert task != null : "Should have called setTask()"; // NOI18N
   2.197  
   2.198              ProgressHandle handle = ProgressHandleFactory.createHandle("Executing Statements");
   2.199              handle.start();
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/impl/StatementHolder.java	Mon May 27 22:21:10 2013 +0200
     3.3 @@ -0,0 +1,32 @@
     3.4 +/*
     3.5 + * To change this template, choose Tools | Templates
     3.6 + * and open the template in the editor.
     3.7 + */
     3.8 +package org.netbeans.modules.plsql.execution.impl;
     3.9 +
    3.10 +import java.sql.SQLException;
    3.11 +import java.sql.Statement;
    3.12 +import org.openide.util.Exceptions;
    3.13 +
    3.14 +/**
    3.15 + * Used to hold {@link java.sql.Statement} to be able to access and cancel already executing statement from another
    3.16 + * thread.
    3.17 + *
    3.18 + * @author ChrLSE
    3.19 + */
    3.20 +class StatementHolder {
    3.21 +
    3.22 +    private Statement statement;
    3.23 +
    3.24 +    public void setStatement(Statement statement) {
    3.25 +        this.statement = statement;
    3.26 +    }
    3.27 +
    3.28 +    void cancel() {
    3.29 +        try {
    3.30 +            statement.cancel();
    3.31 +        } catch (SQLException ex) {
    3.32 +            Exceptions.printStackTrace(ex);
    3.33 +        }
    3.34 +    }
    3.35 +}