Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XD8RF-0002lW-TO for pgsql-jdbc@arkaria.postgresql.org; Fri, 01 Aug 2014 08:43:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XD8RF-0002Xf-9S for pgsql-jdbc@arkaria.postgresql.org; Fri, 01 Aug 2014 08:43:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XD8RE-0002XX-9D for pgsql-jdbc@postgresql.org; Fri, 01 Aug 2014 08:43:44 +0000 Received: from chello080108195078.3.13.vie.surfer.at ([80.108.195.78] helo=margarita.erven.at) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XD8RA-0002sm-9M for pgsql-jdbc@postgresql.org; Fri, 01 Aug 2014 08:43:43 +0000 Received: from [192.168.27.166] (unknown [192.168.27.166]) by margarita.erven.at (Postfix) with ESMTPSA id 9F6567700133; Fri, 1 Aug 2014 10:43:35 +0200 (CEST) Message-ID: <53DB5332.4080604@erven.at> Date: Fri, 01 Aug 2014 10:43:30 +0200 From: Hannes Erven User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.0 MIME-Version: 1.0 To: Dave Cramer CC: List Subject: Re: executeQuery() throws "Statement has been closed" References: <53DA04A9.2030101@erven.at> <53DA50FF.2040803@erven.at> <53DA9A0B.6090404@erven.at> In-Reply-To: Content-Type: multipart/mixed; boundary="------------080809040507080907080604" X-Pg-Spam-Score: -0.6 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------080809040507080907080604 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, > http://jdbc.postgresql.org/documentation/81/thread.html > > "If a thread attempts to use the connection while another one is using > it, it will wait until the other thread has finished its current > operation. If the operation is a regular SQL statement, then the > operation consists of sending the statement and retrieving any ResultSet > (in full)." Consider this test case: one thread calls executeQuery("SELECT ..."), and while that query is still executing, a second thread calls close() on the statement. Given the documentation above, I would expect the regular SQL statement to complete, the statement then to be closed, and no exception thrown. I'll attach a sample code for this scenario. With the current git driver, this fails at getMaxRows(). When you change Jdbc4Statement.createResultset() so it uses this.maxrows instead of getMaxRows() and hence skip checkClosed, then that test will pass. Patch also attached, although I have no idea whether this really is the right thing to do. Still, I will also check my code. I think this happens when a user closes a view while the data inside is (re)loading, so I'll probably have the error handling discard that exception in this case instead of ringing the alarm bell... Thanks for your comments, David and Kevin! Best regards, -hannes --------------080809040507080907080604 Content-Type: text/x-java; name="TestClose.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="TestClose.java" package at.erven; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestClose { /** * @param args */ public static void main(String[] args) { try{ Class.forName("org.postgresql.Driver"); final Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/pgtest", "user", "pass"); System.out.println("Connected."); final Statement st = c.createStatement(); final Thread t1 = new Thread(){ @Override public void run() { try { System.out.println("t1: executeQuery pg_sleep()..."); final ResultSet rs = st.executeQuery("SELECT pg_sleep(3)"); System.out.println("t1: finished pg_sleep()..."); while(rs.next()){ System.out.println("t1: result: "+rs.getObject(1)); } } catch (SQLException e) { e.printStackTrace(); } } }; final Thread t2 = new Thread(){ @Override public void run() { try { Thread.sleep(1500); System.out.println("t2: closing statement..."); st.close(); System.out.println("t2: closed."); } catch (Exception e) { e.printStackTrace(); } } }; t1.start(); t2.start(); }catch(Exception z){ z.printStackTrace(); } } } --------------080809040507080907080604 Content-Type: text/x-patch; name="pgjdbc.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pgjdbc.patch" diff --git a/org/postgresql/jdbc3/AbstractJdbc3Statement.java b/org/postgresql/jdbc3/AbstractJdbc3Statement.java index 66f9a09..e7fa5ca 100644 --- a/org/postgresql/jdbc3/AbstractJdbc3Statement.java +++ b/org/postgresql/jdbc3/AbstractJdbc3Statement.java @@ -27,7 +27,7 @@ import org.postgresql.util.GT; */ public abstract class AbstractJdbc3Statement extends org.postgresql.jdbc2.AbstractJdbc2Statement { - private final int rsHoldability; + protected final int rsHoldability; public AbstractJdbc3Statement (AbstractJdbc3Connection c, int rsType, int rsConcurrency, int rsHoldability) throws SQLException { diff --git a/org/postgresql/jdbc4/Jdbc4Statement.java b/org/postgresql/jdbc4/Jdbc4Statement.java index d368133..b839f4b 100644 --- a/org/postgresql/jdbc4/Jdbc4Statement.java +++ b/org/postgresql/jdbc4/Jdbc4Statement.java @@ -32,8 +32,8 @@ class Jdbc4Statement extends AbstractJdbc4Statement implements Statement throws SQLException { Jdbc4ResultSet newResult = new Jdbc4ResultSet(originalQuery, this, fields, tuples, cursor, - getMaxRows(), getMaxFieldSize(), - getResultSetType(), getResultSetConcurrency(), getResultSetHoldability()); + this.maxrows, this.maxfieldSize, + this.resultsettype, this.concurrency, this.rsHoldability); newResult.setFetchSize(getFetchSize()); newResult.setFetchDirection(getFetchDirection()); return newResult; --------------080809040507080907080604 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --------------080809040507080907080604--