[libpostgresql-jdbc-java] 04/11: Statement.cancel was prone to race conditions even with a single threaded client. It would fire off a cancel message without waiting for an acknowledgement of its success. This resulted in future queries being cancelled when the cancel message was received by the backend. Ensure we get an EOF from the server before returning from the cancel call. This still does nothing about multi-threaded race conditions.
Emmanuel Bourg
ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:19:19 UTC 2017
This is an automated email from the git hooks/post-receive script.
ebourg-guest pushed a commit to tag REL8_0_315
in repository libpostgresql-jdbc-java.
commit 74979e3668b3e5e65b29797ef6e5420d3719f169
Author: Kris Jurka <books at ejurka.com>
Date: Fri Dec 2 03:07:00 2005 +0000
Statement.cancel was prone to race conditions even with a single
threaded client. It would fire off a cancel message without waiting
for an acknowledgement of its success. This resulted in future
queries being cancelled when the cancel message was received by
the backend. Ensure we get an EOF from the server before returning from the cancel call. This still does nothing about multi-threaded
race conditions.
Original analysis and patch by Oliver Jowett
---
org/postgresql/core/PGStream.java | 16 +++++++++++++++-
org/postgresql/core/v2/ProtocolConnectionImpl.java | 3 ++-
org/postgresql/core/v3/ProtocolConnectionImpl.java | 3 ++-
org/postgresql/test/jdbc2/MiscTest.java | 17 ++++++++++++++++-
4 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/org/postgresql/core/PGStream.java b/org/postgresql/core/PGStream.java
index 49cd03e..cf6500e 100644
--- a/org/postgresql/core/PGStream.java
+++ b/org/postgresql/core/PGStream.java
@@ -3,7 +3,7 @@
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.12 2005/01/10 15:30:10 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.13 2005/01/11 08:25:43 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -21,6 +21,8 @@ import java.net.Socket;
import java.sql.*;
import org.postgresql.util.GT;
+import org.postgresql.util.PSQLState;
+import org.postgresql.util.PSQLException;
/**
* Wrapper around the raw connection to the server that implements some basic
@@ -495,6 +497,18 @@ public class PGStream
}
/**
+ * Consume an expected EOF from the backend
+ * @exception SQLException if we get something other than an EOF
+ */
+ public void ReceiveEOF() throws SQLException, IOException
+ {
+ int c = pg_input.read();
+ if (c < 0)
+ return;
+ throw new PSQLException(GT.tr("Expected an EOF from server, got: {0}", new Integer(c)), PSQLState.COMMUNICATION_ERROR);
+ }
+
+ /**
* Closes the connection
*
* @exception IOException if an I/O Error occurs
diff --git a/org/postgresql/core/v2/ProtocolConnectionImpl.java b/org/postgresql/core/v2/ProtocolConnectionImpl.java
index 0e57a94..635961c 100644
--- a/org/postgresql/core/v2/ProtocolConnectionImpl.java
+++ b/org/postgresql/core/v2/ProtocolConnectionImpl.java
@@ -4,7 +4,7 @@
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v 1.5 2005/01/11 08:25:43 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v 1.5.2.1 2005/06/21 20:11:53 davec Exp $
*
*-------------------------------------------------------------------------
*/
@@ -93,6 +93,7 @@ class ProtocolConnectionImpl implements ProtocolConnection {
cancelStream.SendInteger4(cancelPid);
cancelStream.SendInteger4(cancelKey);
cancelStream.flush();
+ cancelStream.ReceiveEOF();
cancelStream.close();
cancelStream = null;
}
diff --git a/org/postgresql/core/v3/ProtocolConnectionImpl.java b/org/postgresql/core/v3/ProtocolConnectionImpl.java
index 6377868..f5aae2f 100644
--- a/org/postgresql/core/v3/ProtocolConnectionImpl.java
+++ b/org/postgresql/core/v3/ProtocolConnectionImpl.java
@@ -4,7 +4,7 @@
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v 1.6 2005/01/11 08:25:44 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v 1.6.2.1 2005/06/21 20:11:53 davec Exp $
*
*-------------------------------------------------------------------------
*/
@@ -91,6 +91,7 @@ class ProtocolConnectionImpl implements ProtocolConnection {
cancelStream.SendInteger4(cancelPid);
cancelStream.SendInteger4(cancelKey);
cancelStream.flush();
+ cancelStream.ReceiveEOF();
cancelStream.close();
cancelStream = null;
}
diff --git a/org/postgresql/test/jdbc2/MiscTest.java b/org/postgresql/test/jdbc2/MiscTest.java
index f4ee26b..3deb2a1 100644
--- a/org/postgresql/test/jdbc2/MiscTest.java
+++ b/org/postgresql/test/jdbc2/MiscTest.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.17 2005/01/11 08:25:48 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.17.2.1 2005/04/10 16:39:48 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -53,6 +53,21 @@ public class MiscTest extends TestCase
TestUtil.closeDB(con);
}
+ /**
+ * Ensure the cancel call does not return before it has completed.
+ * Previously it did which cancelled future queries.
+ */
+ public void testSingleThreadCancel() throws Exception
+ {
+ Connection con = TestUtil.openDB();
+ Statement stmt = con.createStatement();
+ for (int i=0; i<100; i++) {
+ ResultSet rs = stmt.executeQuery("SELECT 1");
+ rs.close();
+ stmt.cancel();
+ }
+ }
+
public void testError() throws Exception
{
Connection con = TestUtil.openDB();
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git
More information about the pkg-java-commits
mailing list