pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: pgsql-jdbc@postgresql.org
Subject: Statement.cancel() race condition
Date: Thu, 01 Dec 2005 17:07:45 -0600
Message-ID: <438F2DE5.EE98.0025.0@wicourts.gov> (raw)

We have about 100 database servers distributed statewide.  We are
planning to migrate to PostgreSQL, and to that end, we have three of our
larger databases (hundreds of GB each) currently running PostgreSQL in
the distributed mix.  (We started with databases containing redundant
data, to minimize risk in the early stages.)  Our software has been
written with every effort to maintain portability, with the ANSI / ISO
standards as our guide.

Some database products, including the one which has been our mainstay
for years, treat the JDBC response as a stream.  This is an allowed
option under the JDBC spec, and has its pros and cons.  One thing we
have found, using this product, is that it is beneficial to cancel a
Statement before closing the ResultSet for that Statement if an
exception occurs.  (If you don't do this you can wait for a while while
the ResultSet.close() method reads and discards all rows.)

All of this is to introduce a pattern which we have found very useful,
from which we are reluctant to move.  Simplified, it goes:

Statement stmt = null;
ResultSet rs = null;
try
{
    stmt = conn.createStatement();
    rs = stmt.executeQuery("<qry>");
    while (rs.next())
    {
        <process the row>
    }
    rs.close();
    rs = null;
    stmt.close();
    stmt = null;
}
finally
{
    if (stmt != null)
    {
        try
        {
            stmt.cancel();
        }
        catch (Exception e)
        {
            // ignore
        }
    }
    if (rs != null)
    {
        try
        {
            rs.close();
        }
        catch (Exception e)
        {
            // ignore
        }
        rs = null;
    }
    if (stmt != null)
    {
        try
        {
            stmt.close();
        }
        catch (Exception e)
        {
            // ignore
        }
        stmt = null;
    }
}

The problem is that even after the exception comes out of this code, is
caught, and the transaction is rolled back -- we are still often able to
start another statement which is running by the time the server gets
around to interrupting the related back end process.  Obviously, having
the cancel of one statement actually interrupt the processing of a
subsequent statement violates the popular principle of "least surprising
result".

Since the place we've been hitting this involves a race condition on a
single client thread, there is an obvious simple-minded solution. 
Attached is a patch representing that solution, which actually solves
the problem where we've been having.  It clearly isn't ready for
prime-time, though, since it fails to deal with a result set which uses
a cursor which is canceled from the thread which started the execution. 
I'm posting at this point because I'm not sure how best to identify that
condition, or how best to handle it.

Discussion welcome.

-Kevin

Attachments:

AbstractJdbc2Statement.java.patch (application/octet-stream, 1.4K) download Index: AbstractJdbc2Statement.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java,v retrieving revision 1.85 diff -c -r1.85 AbstractJdbc2Statement.java *** AbstractJdbc2Statement.java 24 Nov 2005 06:18:29 -0000 1.85 --- AbstractJdbc2Statement.java 1 Dec 2005 23:02:11 -0000 *************** *** 103,108 **** --- 103,111 ---- protected Object []callResult; protected int maxfieldSize = 0; + /** The last thread to start an execute method. Useful in the cancel method. */ + private volatile Thread executionThread; + public ResultSet createDriverResultSet(Field[] fields, Vector tuples) throws SQLException { *************** *** 394,399 **** --- 397,404 ---- } protected void execute(Query queryToExecute, ParameterList queryParameters, int flags) throws SQLException { + executionThread = Thread.currentThread(); + // Every statement execution clears any previous warnings. clearWarnings(); *************** *** 2556,2561 **** --- 2561,2569 ---- */ public void cancel() throws SQLException { + // Quick exit for cancel from execution thread. + if (Thread.currentThread() == executionThread) + return; connection.cancelQuery(); }

view thread (3+ messages)  latest in thread

Message-ID: <438F2DE5.EE98.0025.0@wicourts.gov>
Permalink:  ../438F2DE5.EE98.0025.0@wicourts.gov/
Also on:    postgresql.org/message-id/438F2DE5.EE98.0025.0@wicourts.gov

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov
  Subject: Re: Statement.cancel() race condition
  In-Reply-To: <438F2DE5.EE98.0025.0@wicourts.gov>
  References: <438F2DE5.EE98.0025.0@wicourts.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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