pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedcode to cancel a running query, worker thread
3+ messages / 2 participants
[nested] [flat]
* code to cancel a running query, worker thread
@ 2006-05-04 06:09 surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>
0 siblings, 0 replies; 3+ messages in thread
From: surabhi.ahuja @ 2006-05-04 06:09 UTC (permalink / raw)
To: 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
^ permalink raw reply [nested|flat] 3+ messages in thread
* FW: code to cancel a running query, worker thread
@ 2006-05-07 16:34 surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>
2006-05-08 01:12 ` Re: FW: code to cancel a running query, worker thread Oliver Jowett <oliver@opencloud.com>
0 siblings, 1 reply; 3+ messages in thread
From: surabhi.ahuja @ 2006-05-07 16:34 UTC (permalink / raw)
To: 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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: FW: code to cancel a running query, worker thread
2006-05-07 16:34 FW: code to cancel a running query, worker thread surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>
@ 2006-05-08 01:12 ` Oliver Jowett <oliver@opencloud.com>
0 siblings, 0 replies; 3+ messages in thread
From: Oliver Jowett @ 2006-05-08 01:12 UTC (permalink / raw)
To: surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>; +Cc: pgsql-jdbc
surabhi.ahuja wrote:
> Is the above peice of code fine, does the problem lie in the code which
> calls the executeQuery of QueryExecutor?
Really, this sounds like a threading problem in your code that you need
to solve yourself.
If you can show a testcase showing the driver doing something wrong ..
or a thread dump where a thread is stuck in the driver .. now that is
more interesting.
Otherwise it is just-another-threading-problem; I looked at your code
briefly but it is not particularly clear what its behaviour is meant to
be (i.e. it has "no obvious bugs" rather than "obviously no bugs" ;-)
-O
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-05-08 01:12 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-05-04 06:09 code to cancel a running query, worker thread surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>
2006-05-07 16:34 FW: code to cancel a running query, worker thread surabhi.ahuja <surabhi.ahuja@iiitb.ac.in>
2006-05-08 01:12 ` Oliver Jowett <oliver@opencloud.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox