ResultSet.next on empty result set

1,182 views
Skip to first unread message

steve.ebersole

unread,
May 7, 2010, 10:30:09 PM5/7/10
to H2 Database
I could not see an issue tracker listed on the website, so I apologize
if this is already covered, fixed, discussed or otherwise known :)

I found what I think is a bug in the H2 ResultSet implementation. If
a ResultSet is empty, calling next() on it returns true. According to
the javadocs "@return <code>true</code> if the new current row is
valid; <code>false</code> if there are no more rows". In an empty
ResultSet there are no more rows.

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Thomas Mueller

unread,
May 8, 2010, 1:37:14 AM5/8/10
to h2-da...@googlegroups.com
Hi,

Do you have a test case? Just something simple. For example, the
following prints "false" for me:

import java.sql.*;
public class TestSimple {
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:h2:mem:");
ResultSet rs = conn.createStatement().executeQuery(
"select * from dual where 1=0");
System.out.println(rs.next());
conn.close();
}
}

Regards,
Thomas

steve.ebersole

unread,
May 8, 2010, 5:43:01 PM5/8/10
to H2 Database
Hey Thomas,

I was using 1.2.124 which is where this was happening. Upgrading to
1.2.134 (135 was not available in maven repo atm) fixes it. I was
seeing this from the Hibernate testsuite. The test at this line was
failing:
http://fisheye.jboss.org/browse/Hibernate/core/trunk/testsuite/src/test/java/org/hibernate/test/hql/ScrollableCollectionFetchingTest.java?r=19412#l68

Like I said, upgrading to 1.2.134 made the issue go away.

Thomas Mueller

unread,
May 9, 2010, 1:59:08 PM5/9/10
to h2-da...@googlegroups.com
Hi,

I really don't know what the reason could have been. But most likely
it's not a bug in the ResultSet implementation, but when processing
the query. Tell me if you are still interested to find out what it
was.

Regards,
Thomas

Steve Ebersole

unread,
May 9, 2010, 7:58:18 PM5/9/10
to h2-da...@googlegroups.com
Our test passes for now so it's not critical.  I'll keep an eye on it and if we see it again I'll report back here.

If you wanted to track it down though I'll try to help out.  Not sure what you mean by thinking it's an issue processing the statement.  That test has 2 assertions pertaining to the ResultSet prior to this first next() call that failed.  I also debugged into the ResultSet calls and it showed no results.  I stopped and updated the driver version before I tried troubleshooting any further.

-- Sent from my Palm Pre

st...@hibernate.org
http://hibernate.org

Thomas Mueller

unread,
May 11, 2010, 3:36:34 PM5/11/10
to h2-da...@googlegroups.com
Hi,

> Not sure what
> you mean by thinking it's an issue processing the statement.  That test has
> 2 assertions pertaining to the ResultSet prior to this first next() call
> that failed.  I also debugged into the ResultSet calls and it showed no
> results.  I stopped and updated the driver version before I tried
> troubleshooting any further.

I think it is very very unlikely that the result set is empty, but
calling ResultSet.next() returns true. Most likely the result set is
in fact not empty. That's all I can say from looking at the source
code.

steve.ebersole

unread,
May 11, 2010, 4:08:04 PM5/11/10
to H2 Database
Not sure if you looked at the Hibernate test, but the very first thing
this test does is to execute an initial query which simply checks the
number of results ensuring we got back zero.

That assertion passed in both scenarios.

And the only change between the failure and the pass was to increment
the H2 version as I mentioned.

Not sure what to tell you.

Christian Peter

unread,
May 11, 2010, 4:15:51 PM5/11/10
to H2 Database
Hi,

maybe it is related to this one:

http://code.google.com/p/h2database/source/detail?r=2484

Regards

Christian

On May 11, 10:08 pm, "steve.ebersole" <steven.ebers...@gmail.com>
wrote:

Thomas Mueller

unread,
May 11, 2010, 4:46:59 PM5/11/10
to h2-da...@googlegroups.com
Hi,

I guess there is a misunderstanding. Most likely the bug was that H2
did return rows for the query, but it shouldn't. Due to some other bug
or incompatibility which was fixed later on.

>> Not sure if you looked at the Hibernate test, but the very first thing
>> this test does is to execute an initial query which simply checks the
>> number of results ensuring we got back zero.

As far as I see, the first line where this Hibernate test checks it
got back zero rows is line 68. The test does:

63: ScrollableResults results = s.createQuery( query ).setString(
"desc", "root%" ).scroll();
65: assertFalse( results.isFirst() );
66: assertFalse( results.isLast() );
68: assertFalse( results.next() );

As far as I see, ScrollableResultsImpl.isFirst() just calls
ResultSet.isFirst(), and ScrollableResultsImpl.isLast() just calls
ResultSet.isLast(). Both methods return false no matter how many rows
are in the result set, as long as the results.next() is not yet
called. My test case:

public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:");
ResultSet rs = conn.createStatement().executeQuery("select * from dual");
System.out.println(rs.isFirst());
System.out.println(rs.isLast());
System.out.println(rs.next());
conn.close();
}

Result:
false
false
true

Regards,
Thomas
Reply all
Reply to author
Forward
0 new messages