Thread: FW: code to cancel a running query, worker thread
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;
* @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 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
**/
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();
}
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;
}
}
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;
}
}
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) {}
}
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;
}
}
}
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();
}
}
}
}
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
"surabhi.ahuja" <surabhi.ahuja@iiitb.ac.in> writes: > i have the following peice of code, which is meant for cancelling > queries in between > [ big chunk of Java code ] You'd be more likely to find a knowledgeable answer on the pgsql-jdbc mailing list. regards, tom lane