From: | "surabhi(dot)ahuja" <surabhi(dot)ahuja(at)iiitb(dot)ac(dot)in> |
---|---|
To: | <pgsql-general(at)postgresql(dot)org> |
Subject: | code to cancel a running query, worked thread |
Date: | 2006-04-19 09:39:07 |
Message-ID: | 8626C1B7EB748940BCDD7596134632BE39867A@jal.iiitb.ac.in |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
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
From | Date | Subject | |
---|---|---|---|
Next Message | Kynn Jones | 2006-04-19 10:19:50 | How to implement a "subordinate database"? |
Previous Message | liishyan | 2006-04-19 09:27:04 | postmaster services problem |