postgresql-jdbc.git / summary / log / commit / refs

commit    09caa728892ebdacef80f8007c0d5e317406dfd9
Author:   Kris Jurka <books@ejurka.com>
Date:     Fri Dec 02 03:05:10 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 3d0605b2..c7dd8ae5 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.15 2005/04/20 00:10:58 oliver Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.16 2005/06/25 00:53:15 oliver 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 @@ -500,6 +502,18 @@ public class PGStream pg_output.flush(); } + /** + * 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 * diff --git a/org/postgresql/core/v2/ProtocolConnectionImpl.java b/org/postgresql/core/v2/ProtocolConnectionImpl.java index 245b1319..936de315 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.7 2005/06/21 18:07:09 davec Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v 1.8 2005/11/24 02:29:20 oliver 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 1e18c6c2..a2d36c92 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.8 2005/06/21 18:07:09 davec Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v 1.9 2005/11/24 02:29:21 oliver 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 b08eb071..f1b6059e 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.18 2005/04/10 16:44:13 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.19 2005/11/24 02:31:43 oliver 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(); [parent: 2ce90ba7c448]