Re: "This ResultSet is closed" exception on a PreparedStatement getMetaData()

From: Dave Cramer <pg(at)fastcrypt(dot)com>
To: Emmanuel GUITON <Emmanuel(dot)GUITON(at)intrinsec(dot)com>
Cc: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: "This ResultSet is closed" exception on a PreparedStatement getMetaData()
Date: 2013-12-19 14:20:02
Message-ID: CADK3HHKufUiBs4i2QgpFpNAubin6wRV7OT-TaPs+2irM71YAZg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi Emmanuel,

That's what I get for answering without checking the code! You are correct
and it does seem like a bug, and fairly easily fixed. I'll echo Kevin's
sentiments here.

Thanks!

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca

On Thu, Dec 19, 2013 at 9:06 AM, Emmanuel GUITON <
Emmanuel(dot)GUITON(at)intrinsec(dot)com> wrote:

> Hi Dave,
>
> I am a bit surprised with what you say since in the test case I provided,
> I call getMetaData() on the PreparedStatement without having ever
> executed the PreparedStatement itself to get the ResultSet. And it works.
> Do you mean that under the hook the PreparedStatement executes a query to
> be able to fetch the metadata ? (Then what would it be since a ResultSet
> could not be fetched without parameter values when the query has
> parameters.)
>
> Unfortunately, I do not know if this behavior is consistent with previous
> versions of the JDBC driver.
>
> Regards,
> - Emmanuel
>
>
>
> *Emmanuel GUITON*
>
> *Ingénieur Développement *
> Fixe : +33170928416 l Standard : +33141917777
>
> 215, avenue Georges Clemenceau l 92024 Nanterre
>
> <http://www.forum-fic.com/2014/fr/inscription-fic/>
> <http://www.intrinsec.com/>
>
>
>
> ------------------------------
> *From:* davecramer(at)gmail(dot)com <davecramer(at)gmail(dot)com> on behalf of Dave
> Cramer <pg(at)fastcrypt(dot)com>
> *Sent:* Thursday, December 19, 2013 2:35 PM
> *To:* Emmanuel GUITON
> *Cc:* pgsql-jdbc(at)postgresql(dot)org
> *Subject:* Re: [JDBC] "This ResultSet is closed" exception on a
> PreparedStatement getMetaData()
>
>
> Emmanuel,
>
> The assumption that the spec makes about the prepared statement being
> compiled are not valid for PostgreSQL. The metadata comes from the result
> set, not from the statement. You could in fact not call getMetaData on a
> statement which was not executed.
>
> That being said, do you know if this behaviour is consistent with
> previous versions of the JDBC driver ?
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
>
>
> ------------------------------
>
> On Thu, Dec 19, 2013 at 4:33 AM, Emmanuel GUITON <
> Emmanuel(dot)GUITON(at)intrinsec(dot)com> wrote:
>
>> Hello,
>>
>> I ran into troubles fetching metadata on a PreparedStatement. After some
>> time, I isolated a simple test case that shows the issue (see the code
>> snippet below). On a brand new PreparedStatement, I call the getMetaData()
>> method without any problem. However, if I execute the query
>> (executeQuery()) on this same statement, close the ResultSet and then calls
>> again
>> getMetaData() (still on the same PreparedStatement), getMetaData() throws
>> a RuntimeException saying "This ResultSet is closed".
>> I would expect to be able to get the metadata. Is that a bug in the JDBC
>> driver ?
>>
>> I tested this behavior on several flavours of the 9.3 driver version,
>> including 9.3-1100-jdbc4 .
>>
>> Regards,
>> - Emmanuel
>>
>>
>> package test;
>>
>> import java.sql.Connection;
>> import java.sql.DriverManager;
>> import java.sql.PreparedStatement;
>> import java.sql.ResultSet;
>> import java.sql.ResultSetMetaData;
>> import java.sql.SQLException;
>> import junit.framework.Assert;
>> import org.testng.annotations.Test;
>>
>> /**
>> * Test on the PostgreSql JDBC driver.
>> */
>> public class JdbcTest
>> {
>> /**
>> * This test shows a driver issue on the PreparedStatement management.
>> *
>> * @throws ClassNotFoundException On driver initialization failure.
>> * @throws SQLException On datbase operation failure.
>> */
>> @Test
>> public void test2()
>> throws SQLException, ClassNotFoundException
>> {
>> Class.forName("org.postgresql.Driver");
>> final Connection connection =
>> DriverManager.getConnection("jdbc:postgresql://server/db_name", "username",
>> "password");
>>
>> final String query = "SELECT 1 AS toto, 2 AS titi";
>>
>> final PreparedStatement pstmt =
>> connection.prepareStatement(query);
>> final ResultSetMetaData rsMetadata = pstmt.getMetaData();
>> Assert.assertNotNull(rsMetadata);
>>
>> final ResultSetMetaData rsMetadata2 = pstmt.getMetaData();
>> Assert.assertNotNull(rsMetadata2);
>>
>> final ResultSet rs = pstmt.executeQuery();
>> Assert.assertNotNull(rs);
>> rs.close();
>>
>> Assert.assertFalse(pstmt.isClosed());
>> try
>> {
>> // Guess what ? The next line throws an exception !
>> final ResultSetMetaData rsMetadata3 = pstmt.getMetaData();
>> Assert.assertNotNull(rsMetadata3);
>> }
>> catch (final RuntimeException exception)
>> {
>> Assert.fail(exception.getMessage());
>> }
>> pstmt.close();
>>
>> connection.close();
>> }
>> }
>>
>>
>>
>>
>> *Emmanuel GUITON*
>>
>> *Ingénieur Développement *
>> Fixe : +33170928416 l Standard : +33141917777
>>
>> 215, avenue Georges Clemenceau l 92024 Nanterre
>>
>> <http://http://www.forum-fic.com/2014/fr/inscription-fic/>
>> <http://www.intrinsec.com/>
>>
>>
>>
>>
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2013-12-20 12:27:47 Re: "This ResultSet is closed" exception on a PreparedStatement getMetaData()
Previous Message Emmanuel GUITON 2013-12-19 14:06:52 Re: "This ResultSet is closed" exception on a PreparedStatement getMetaData()