FW: code to cancel a running query, worker thread - Mailing list pgsql-general

From surabhi.ahuja
Subject FW: code to cancel a running query, worker thread
Date
Msg-id 8626C1B7EB748940BCDD7596134632BE39867C@jal.iiitb.ac.in
Whole thread Raw
Responses Re: FW: code to cancel a running query, worker thread
List pgsql-general
 
Help needed,
 
i have the following peice of code, which is meant for cancelling queries in between
 
import java.sql.*;

public class QueryExecutor implements Runnable {
 /**
  * @param args
  */
 private Thread worker;
 private Params params;
 private Results results;
 private volatile boolean cancelRequest;
 private volatile boolean closeRequest;
 private class Params {
     public Statement statement;
     public String query;
     public boolean pending;
 }
 private class Results {
     public ResultSet rs;
     public SQLException exception;
     public boolean serviced;
 }
 public QueryExecutor() {
  params = new Params();
  results = new Results();
  worker = new Thread(this);
  worker.start();
 }
 /**
  * Executes an SQL query.
  * The method can be interrupted by another thread at any moment.
  * @return <code>ResultSet</code> if execution successful
  * @exception SQLException if a database error occurs
  * @exception InterruptedException if interrupted by another thread
  **/
 public synchronized ResultSet executeQuery(Statement statement, String query)
         throws SQLException, InterruptedException {
     //Set query parameters
     synchronized(params) {
         params.statement = statement;
         params.query = query;
         params.pending = true;
         params.notify();
     }
     synchronized(results) {
         try {
             //Wait for the query to complete
             while(!results.serviced) {
                 results.wait();
                 System.out.println("waiting for results");
             }
             System.out.println("obtained results");
             if (results.exception != null) {
                 throw results.exception;
             }
         } catch (InterruptedException e) {
          System.out.println("Cancelling");
             cancel();
             //throw e;
         } finally {
             results.serviced = false;
         }
         return results.rs;
     }
 }
 private void cancel() {
     cancelRequest = true;
     try {
         params.statement.cancel();
         synchronized(results) {
             while(!results.serviced) {
                 results.wait();
             }
         }
     } catch (SQLException e) {
         return;
     } catch (InterruptedException e) {
         return;
     } finally {
         cancelRequest = false;
     }
 }
 public void close() {
  closeRequest = true;
  if (params.statement != null) {
   cancel();
  }
  worker.interrupt();
  try {
   worker.join();
  } catch (InterruptedException e) {}
 }
// The implementation of the Runnable interface (for the worker thread)
 public void run() {
     ResultSet rs = null;
     SQLException ex = null;
     while(!closeRequest) {
         synchronized(params) {
             try {
                 //Wait for query parameters
                 while(!params.pending) {
                     params.wait();
                 }
                 params.pending = false;
             } catch (InterruptedException e) {
                 if (closeRequest) {
                     return;
                 }
             }
             //Execute query
             try {
                 rs = params.statement.executeQuery(
                     params.query);
                 System.out.println(params.query);
             } catch (SQLException e) {
                 if (!cancelRequest) {
                     ex = e;
                 }
             }
         }
         //Set query results
         synchronized(results) {
             results.rs = rs;
             results.exception = ex;
             results.serviced = true;
             results.notify();
         }
     }
 }
}
 
in the front end i select a particular item , whcih will perform executeQuery,
 
when i select another item, the prev query gets cancelled and new one is executed.
 
however if i change my selection very fast, it seems that the worker thread stops responding.
 
and then even if i click on other items, no query gets submitted.
 
Is the above peice of code fine, does the problem lie in the code which calls the executeQuery of QueryExecutor?
 
Thanks,
regards
Surabhi

pgsql-general by date:

Previous
From: Klint Gore
Date:
Subject: Re: primary keys
Next
From: "Dave Page"
Date:
Subject: Re: postmaster services problem