X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 143269DCAD1 for ; Thu, 1 Dec 2005 19:07:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41494-01 for ; Thu, 1 Dec 2005 19:07:53 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from gwmta.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by postgresql.org (Postfix) with ESMTP id D54539DCD50 for ; Thu, 1 Dec 2005 19:07:54 -0400 (AST) Received: from Courts-MTA by gwmta.wicourts.gov with Novell_GroupWise; Thu, 01 Dec 2005 17:07:57 -0600 Message-Id: <438F2DE5.EE98.0025.0@wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 7.0 Date: Thu, 01 Dec 2005 17:07:45 -0600 From: "Kevin Grittner" To: Subject: Statement.cancel() race condition Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7755B651.0__=" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/5 X-Sequence-Number: 14336 --=__Part7755B651.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline 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(""); while (rs.next()) { } 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 --=__Part7755B651.0__= Content-Type: application/octet-stream; name="AbstractJdbc2Statement.java.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="AbstractJdbc2Statement.java.patch" SW5kZXg6IEFic3RyYWN0SmRiYzJTdGF0ZW1lbnQuamF2YQo9PT09PT09PT09PT09PT09PT09PT09 PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09ClJDUyBmaWxlOiAv dXNyL2xvY2FsL2N2c3Jvb3QvcGdqZGJjL3BnamRiYy9vcmcvcG9zdGdyZXNxbC9qZGJjMi9BYnN0 cmFjdEpkYmMyU3RhdGVtZW50LmphdmEsdgpyZXRyaWV2aW5nIHJldmlzaW9uIDEuODUKZGlmZiAt YyAtcjEuODUgQWJzdHJhY3RKZGJjMlN0YXRlbWVudC5qYXZhCioqKiBBYnN0cmFjdEpkYmMyU3Rh dGVtZW50LmphdmEJMjQgTm92IDIwMDUgMDY6MTg6MjkgLTAwMDAJMS44NQotLS0gQWJzdHJhY3RK ZGJjMlN0YXRlbWVudC5qYXZhCTEg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IHRocm93cyBTUUxFeGNl cHRpb24KICAgICAgeworICAgICAgICAgLy8gUXVpY2sgZXhpdCBmb3IgY2FuY2VsIGZyb20gZXhl Y3V0aW9uIHRocmVhZC4KKyAgICAgICAgIGlmIChUaHJlYWQuY3VycmVudFRocmVhZCgpID09IGV4 ZWN1dGlvblRocmVhZCkKKyAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICBjb25uZWN0aW9u LmNhbmNlbFF1ZXJ5KCk7CiAgICAgIH0KICAK --=__Part7755B651.0__=--