code to cancel a running query, worker thread

From: "surabhi(dot)ahuja" <surabhi(dot)ahuja(at)iiitb(dot)ac(dot)in>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: code to cancel a running query, worker thread
Date: 2006-05-04 06:09:10
Message-ID: 8626C1B7EB748940BCDD7596134632BE398680@jal.iiitb.ac.in
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


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

Browse pgsql-jdbc by date

  From Date Subject
Next Message Martina 2006-05-04 13:13:38 Transaction foreign key violation
Previous Message Kris Jurka 2006-05-04 05:02:58 Re: Bug with callable statement and output parameters