From: Hannes Erven <hannes@erven.at>
To: Dave Cramer <pg@fastcrypt.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: executeQuery() throws "Statement has been closed"
Date: Fri, 01 Aug 2014 10:43:30 +0200
Message-ID: <53DB5332.4080604@erven.at> (raw)
In-Reply-To: <CADK3HH+M5XRDs8foAQEJiMS6v1HMhCgR89MnvvMM9zasQq79hg@mail.gmail.com>
References: <53DA04A9.2030101@erven.at>
<CADK3HH+9x3Ni8y=KzyUsdsLN5j3cZUcbvjMKC7mmt8o3zCTK3A@mail.gmail.com>
<53DA50FF.2040803@erven.at>
<CADK3HHKUi8DF_R4DAtMOjFEeO5z9aqxtULHZb9iQUqwWRBG3KA@mail.gmail.com>
<53DA9A0B.6090404@erven.at>
<CADK3HH+M5XRDs8foAQEJiMS6v1HMhCgR89MnvvMM9zasQq79hg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
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
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbcAttachments:TestClose.java (text/x-java, 1.3K) download
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();
}
}
}
pgjdbc.patch (text/x-patch, 1.5K) downloaddiff --git a/org/postgresql/jdbc3/AbstractJdbc3Statement.java b/org/postgresql/jdbc3/AbstractJdbc3Statement.javaindex 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.javaindex 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;
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: hannes@erven.at, pg@fastcrypt.com
Subject: Re: executeQuery() throws "Statement has been closed"
In-Reply-To: <53DB5332.4080604@erven.at>
References: <53DA04A9.2030101@erven.at> <CADK3HH+9x3Ni8y=KzyUsdsLN5j3cZUcbvjMKC7mmt8o3zCTK3A@mail.gmail.com> <53DA50FF.2040803@erven.at> <CADK3HHKUi8DF_R4DAtMOjFEeO5z9aqxtULHZb9iQUqwWRBG3KA@mail.gmail.com> <53DA9A0B.6090404@erven.at> <CADK3HH+M5XRDs8foAQEJiMS6v1HMhCgR89MnvvMM9zasQq79hg@mail.gmail.com> <53DB5332.4080604@erven.at>
* 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