Skip to content

Commit 7776319

Browse files
author
Dave Cramer
committed
Implementation for cancelQuery by Grant Finnemore <grantf@guruhut.co.za>
1 parent 29e3ef0 commit 7776319

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.postgresql.core.*;
1212

1313
/*
14-
* $Id: Connection.java,v 1.40 2001/12/11 04:44:23 barry Exp $
14+
* $Id: Connection.java,v 1.41 2002/02/26 02:15:54 davec Exp $
1515
*
1616
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1717
* JDBC2 versions of the Connection class.
@@ -91,6 +91,40 @@ public abstract class Connection
9191
public Connection()
9292
{}
9393

94+
public void cancelQuery() throws SQLException
95+
{
96+
PG_Stream cancelStream = null;
97+
try {
98+
cancelStream = new PG_Stream(PG_HOST, PG_PORT);
99+
} catch (ConnectException cex) {
100+
// Added by Peter Mount <peter@retep.org.uk>
101+
// ConnectException is thrown when the connection cannot be made.
102+
// we trap this an return a more meaningful message for the end user
103+
throw new PSQLException ("postgresql.con.refused");
104+
} catch (IOException e) {
105+
throw new PSQLException ("postgresql.con.failed",e);
106+
}
107+
108+
// Now we need to construct and send a cancel packet
109+
try {
110+
cancelStream.SendInteger(16, 4);
111+
cancelStream.SendInteger(80877102, 4);
112+
cancelStream.SendInteger(pid, 4);
113+
cancelStream.SendInteger(ckey, 4);
114+
cancelStream.flush();
115+
}
116+
catch(IOException e) {
117+
throw new PSQLException("postgresql.con.failed",e);
118+
}
119+
finally {
120+
try {
121+
if(cancelStream != null)
122+
cancelStream.close();
123+
}
124+
catch(IOException e) {} // Ignore
125+
}
126+
}
127+
94128
/*
95129
* This method actually opens the connection. It is called by Driver.
96130
*
@@ -266,8 +300,8 @@ protected void openConnection(String host, int port, Properties info, String dat
266300
switch (beresp)
267301
{
268302
case 'K':
269-
pid = pg_stream.ReceiveInteger(4);
270-
ckey = pg_stream.ReceiveInteger(4);
303+
pid = pg_stream.ReceiveIntegerR(4);
304+
ckey = pg_stream.ReceiveIntegerR(4);
271305
break;
272306
case 'E':
273307
case 'N':
@@ -281,6 +315,16 @@ protected void openConnection(String host, int port, Properties info, String dat
281315
switch (beresp)
282316
{
283317
case 'Z':
318+
319+
try
320+
{
321+
pg_stream.SendChar('Q');
322+
pg_stream.SendChar(' ');
323+
pg_stream.SendChar(0);
324+
pg_stream.flush();
325+
} catch (IOException e) {
326+
throw new PSQLException("postgresql.con.ioerror",e);
327+
}
284328
break;
285329
case 'E':
286330
case 'N':
@@ -448,6 +492,7 @@ public String getURL() throws SQLException
448492
* @return the user name
449493
* @exception SQLException just in case...
450494
*/
495+
int lastMessage = 0;
451496
public String getUserName() throws SQLException
452497
{
453498
return PG_USER;

src/interfaces/jdbc/org/postgresql/Statement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void clearWarnings() throws SQLException
179179
*/
180180
public void cancel() throws SQLException
181181
{
182-
// FIXME: Cancel feature has been available since 6.4. Implement it here!
182+
throw new PSQLException("postgresql.unimplemented");
183183
}
184184

185185
/*

src/interfaces/jdbc/org/postgresql/jdbc2/Statement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ public int[] executeBatch() throws SQLException
211211
return result;
212212
}
213213

214+
public void Cancel() throws SQLException
215+
{
216+
connection.cancelQuery();
217+
}
218+
214219
public java.sql.Connection getConnection() throws SQLException
215220
{
216221
return (java.sql.Connection)connection;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy